Skip to content

Commit 872fc3d

Browse files
Added colors and improved menu.
1 parent 0c7a098 commit 872fc3d

File tree

3 files changed

+50
-7
lines changed

3 files changed

+50
-7
lines changed

app/src/main/java/com/github/gianlucanitti/expreval/ExprEval.java

Lines changed: 30 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import android.app.AlertDialog;
44
import android.content.DialogInterface;
5+
import android.graphics.Color;
56
import android.support.v7.app.AppCompatActivity;
67
import android.os.Bundle;
78
import android.support.v7.widget.Toolbar;
9+
import android.text.Html;
810
import android.text.method.ScrollingMovementMethod;
911
import android.view.Menu;
1012
import android.view.MenuItem;
@@ -15,10 +17,10 @@
1517
import com.github.gianlucanitti.javaexpreval.ExpressionContext;
1618
import com.github.gianlucanitti.javaexpreval.InteractiveExpressionContext;
1719
import com.github.gianlucanitti.javaexpreval.NullInputStream;
20+
import com.github.gianlucanitti.javaexpreval.NullOutputStream;
1821

1922
import java.io.IOException;
2023
import java.io.StringReader;
21-
import java.io.StringWriter;
2224

2325
public class ExprEval extends AppCompatActivity implements View.OnClickListener{
2426

@@ -28,9 +30,13 @@ public class ExprEval extends AppCompatActivity implements View.OnClickListener{
2830
private TextView out;
2931
private Button evalBtn;
3032

33+
private TextViewWriter inEchoWriter;
3134
private TextViewWriter outWriter;
35+
private TextViewWriter verboseWriter;
36+
private TextViewWriter errorWriter;
3237

3338
private ContextDialogFragment ctxDialog;
39+
private boolean echoInput = true;
3440

3541
public ExpressionContext getContext(){
3642
return ctx;
@@ -51,8 +57,11 @@ protected void onCreate(Bundle savedInstanceState) {
5157
evalBtn = (Button)findViewById(R.id.evalBtn);
5258
evalBtn.setOnClickListener(this);
5359
ctxDialog = new ContextDialogFragment();
54-
outWriter = new TextViewWriter(out);
55-
ctx = new InteractiveExpressionContext(NullInputStream.getReader(), outWriter, outWriter, outWriter, true);
60+
inEchoWriter = new TextViewWriter(out, Color.YELLOW);
61+
outWriter = new TextViewWriter(out, Color.GREEN);
62+
verboseWriter = new TextViewWriter(out, out.getTextColors().getDefaultColor());
63+
errorWriter = new TextViewWriter(out, Color.RED);
64+
ctx = new InteractiveExpressionContext(NullInputStream.getReader(), outWriter, verboseWriter, errorWriter, true);
5665
ctx.addObserver(ctxDialog);
5766
ctxDialog.update(ctx, null);
5867
}
@@ -88,9 +97,21 @@ public void onClick(DialogInterface dialogInterface, int i) {
8897
case R.id.action_clearout:
8998
out.setText("");
9099
return true;
100+
case R.id.action_verbose:
101+
item.setChecked(!item.isChecked());
102+
if(item.isChecked())
103+
ctx.setVerboseOutputWriter(verboseWriter, true);
104+
else
105+
ctx.setVerboseOutputWriter(NullOutputStream.getWriter(), true);
106+
return true;
107+
case R.id.action_input:
108+
item.setChecked(!item.isChecked());
109+
echoInput = item.isChecked();
110+
return true;
91111
case R.id.action_help:
92-
new AlertDialog.Builder(this).setMessage("In the input box you can type expressions, variable or function assignments (like \"a=5\" or \"log(x,b)=log(x)/log(b)\") and commands. " +
93-
"The available commands are \"help\", \"context\", \"clear\" and \"exit\". Type \"help\" in the input box for more detailed instructions. ").show();
112+
new AlertDialog.Builder(this).setMessage(Html.fromHtml("In the input box you can type expressions, variable or function assignments (like \"a=5\" or \"log(x,b)=log(x)/log(b)\") and commands. " +
113+
"The available commands are \"help\", \"context\", \"clear\" and \"exit\". Type \"help\" in the input box for more detailed instructions. In the output box, results are shown in <font color=\"green\">green</font> and errors in <font color=\"red\">red</font>. " +
114+
"Evaluation steps are shown in default color, and the input is echoed in <font color=\"yellow\">yellow</font> (both can be disabled from the menu).")).show();
94115
return true;
95116
default:
96117
return super.onOptionsItemSelected(item);
@@ -102,6 +123,10 @@ public void onClick(View view){
102123
ctx.setInputReader(new StringReader(in.getText().toString()));
103124
InteractiveExpressionContext.Status status = null;
104125
try {
126+
if(echoInput) {
127+
inEchoWriter.write("> " + in.getText().toString() + System.getProperty("line.separator"));
128+
inEchoWriter.flush();
129+
}
105130
status = ctx.update();
106131
}catch(IOException ex){}
107132
in.getText().clear();

app/src/main/java/com/github/gianlucanitti/expreval/TextViewWriter.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.github.gianlucanitti.expreval;
22

33
import android.support.annotation.NonNull;
4+
import android.text.SpannableString;
5+
import android.text.style.ForegroundColorSpan;
46
import android.widget.TextView;
57

68
import java.io.IOException;
@@ -10,9 +12,11 @@ public class TextViewWriter extends Writer {
1012

1113
private StringBuilder buffer;
1214
private TextView v;
15+
private int color;
1316

14-
public TextViewWriter(TextView target){
17+
public TextViewWriter(TextView target, int c){
1518
v = target;
19+
color = c;
1620
buffer = new StringBuilder();
1721
}
1822

@@ -23,7 +27,9 @@ public void close() throws IOException {
2327

2428
@Override
2529
public void flush() throws IOException {
26-
v.append(buffer.toString());
30+
SpannableString s = new SpannableString(buffer.toString());
31+
s.setSpan(new ForegroundColorSpan(color), 0, s.length(), 0);
32+
v.append(s);
2733
buffer = new StringBuilder();
2834
}
2935

app/src/main/res/menu/mainmenu.xml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,18 @@
1313
android:title="Clear output"
1414
tools:showAsAction="never"/>
1515

16+
<item android:id="@+id/action_verbose"
17+
android:title="Show steps"
18+
android:checkable="true"
19+
android:checked="true"
20+
tools:showAsAction="never"/>
21+
22+
<item android:id="@+id/action_input"
23+
android:title="Echo input"
24+
android:checkable="true"
25+
android:checked="true"
26+
tools:showAsAction="never"/>
27+
1628
<item android:id="@+id/action_help"
1729
android:title="Help"
1830
tools:showAsAction="never"/>

0 commit comments

Comments
 (0)