Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colorizing terminal output #224

Merged
merged 1 commit into from Feb 22, 2018
Merged

Colorizing terminal output #224

merged 1 commit into from Feb 22, 2018

Conversation

ghost
Copy link

@ghost ghost commented Feb 15, 2018

These changes enabled messages printed to standard out to be colorized when they are either a warning or an error.

I've moved all print methods (that were previously in VM.java) into a new Output.java module. The module contains print, warning, and error methods that are called whenever SOMns prints something to standard output or standard error. Each method uses the static AnsiColor8 class to prefix their messages with the corresponding ANSI color code: red for errors, yellow for warnings, and no coloring for regular messages.

To provide access to the printing methods from Newspeak, I've added a pair of new System primitives: PrintWarningPrim and PrintErrorPim. These can be invoked, via the System module.

Finally, the ANSI coloring is turned off by default (since it's not supported by Eclipse's Console widget). The argument --ansi-coloring, or the shorter version -ac, can be added when invoking SOMns from the command line to turn the ANSI coloring on.

Copy link
Owner

@smarr smarr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, I like it pretty :)

Could you please have a look at the comments?
I am also wondering whether this needs to have a flag to disable it, some java property.
When we might run in an environment without ansi term support, this might cause issues.
I checked whether that can be automatically detected, but that seems to be rather problematic.

@@ -25,7 +25,8 @@ class System usingVmMirror: vmMirror = Value (
(* Exiting *)
public exit: error = ( vmMirror exit: error )
public exit = ( self exit: 0 )
public error: msg = ( self printNewline: 'ERROR: ' + msg. self exit: 1 )
public warning: msg = ( vmMirror printWarning: msg. )
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

one space too much for alignment before =?

src/som/VM.java Outdated
@@ -315,17 +315,43 @@ public void errorExit(final String message) {
requestExit(1);
}

private static class AnsiColor8 {
private static String black = "\u001b[30m";
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there a specific reason why these are not final?
and, if they should be final, could we capitalize the names, e.g., BLACK to indicate them as constants?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's also getting unwieldily here.
Could you move all printing into something like an Output or Terminal class?
The som package is probably the right place for it.

Repository owner deleted a comment Feb 15, 2018
Repository owner deleted a comment Feb 15, 2018
Repository owner deleted a comment Feb 15, 2018
Repository owner deleted a comment Feb 15, 2018
Repository owner deleted a comment Feb 15, 2018
Repository owner deleted a comment Feb 15, 2018
@ghost
Copy link
Author

ghost commented Feb 15, 2018

Hi Stefan,

I've updated the changes to include a new argument, added to the ./som Python script and later VMSettings (I'm happy to change to a Java property if you like, but I thought this was a reasonable solution). When this flag is included, the ANSI coloring is disabled. I didn't know how to name the flag, so currently I've used -O for the short name and --no-ansi for the long name.

I've also changed the fields of the AnsiColor8 class to be public and finalized and refactored the print, warning, and error methods so that other colors can be used.

som Outdated
@@ -105,7 +105,8 @@ parser.add_argument('-T', '--no-trace', help='do not print truffle compilation i
parser.add_argument('--no-graph-pe', help='disable Graph PE',
dest='graph_pe', action='store_false', default=True)


parser.add_argument('-O', '--no-ansi', help='Turns off ANSI coloring for output from interperter',
dest='no_ansi_coloring', action='store_true', default=False)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please avoid negation where possible.
So, dest='ansi_coloring', action='store_false', default=True. This is much more explicit and easier to read.

som Outdated
@@ -224,6 +228,8 @@ if not args.interpreter and args.igv_to_file:
flags += ['-Dgraal.PrintIdealGraphFile=true']
if args.igv_methods:
flags += ['-Dsom.igvDumpAfterParsing=true']
if args.no_ansi_coloring:
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why are there two flags added?
-Dsom.noAnsiColoring=true and -Dsom.useANSIColor=false?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Haha, missed this during clean-up.

import som.vm.VmSettings;


public class Output {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a brief comment.

/**
* This method is used to print reports about the number of created artifacts.
* For example actors, messages and promises.
*/
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please first the comment, then the annotation.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, there should be a linter rule for that...

public static void print(final String msg, final String color) {
// Checkstyle: stop
if (VmSettings.NO_ANSI_COLOR_IN_OUTPUT) {
System.out.print(msg);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This isn't a correct patch.
We used to output to System.err when showing errors.

@@ -29,6 +29,8 @@

public static final boolean IGV_DUMP_AFTER_PARSING;

public static final boolean NO_ANSI_COLOR_IN_OUTPUT;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No negation, please!

@smarr
Copy link
Owner

smarr commented Feb 15, 2018

And, I can't see the logs currently because of Travis issues, but I am pretty sure they are failing.
We got tests for a reason :)

@ghost
Copy link
Author

ghost commented Feb 15, 2018

Hmm, it seems the Eclipse Console has ANSI support now (or perhaps I turned it off somewhere). Maybe the flag should be to turn the coloring on rather than off?

@smarr
Copy link
Owner

smarr commented Feb 15, 2018

Hmm, it seems the Eclipse Console has ANSI support now (or perhaps I turned it off somewhere). > Maybe the flag should be to turn the coloring on rather than off?

Say what? I don't understand, but this doesn't make sense to me.
Why would you disable it by default if the Eclipse console has support for it?

@ghost
Copy link
Author

ghost commented Feb 19, 2018

Why would you disable it by default if the Eclipse console has support for it?

Sorry, I had meant to say that Eclipse console does not have support for it.

@smarr smarr added the enhancement Improves the implementation with something noteworthy label Feb 20, 2018
@smarr smarr added this to the v0.6.0 - Black Diamonds milestone Feb 20, 2018
- move all output related code into Output class
- colorize print, warning, and error methods
- output errors to stderr
- add som.useAnsiColoring flag to control use of colors
- in launcher, determine use of colors based on whether it looks like a
  terminal

Co-authored-by: Stefan Marr <[email protected]>
Signed-off-by: Stefan Marr <[email protected]>
@smarr
Copy link
Owner

smarr commented Feb 22, 2018

Since this is a conceptually pretty simple change, I did condense the history.

@smarr smarr merged commit de36c88 into smarr:dev Feb 22, 2018
@smarr smarr changed the title Colorizing standard output Colorizing terminal output Feb 22, 2018
@ghost ghost deleted the somdev branch March 13, 2018 23:56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement Improves the implementation with something noteworthy
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant