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

add syntax highlighting for output #52

Closed
wants to merge 1 commit into from

Conversation

karuppiah7890
Copy link

@karuppiah7890 karuppiah7890 commented Dec 30, 2020

fixes #39

  • Add code for syntax highlight for top level / root command
  • Add test for syntax highlight for top level / root command
  • Add code for syntax highlight for get sub command
  • Add test for syntax highlight for get sub command

@karuppiah7890
Copy link
Author

Consider this a draft. Of course we can first discuss and make any necessary changes and then merge :)

@karuppiah7890
Copy link
Author

It currently looks like this

Screen Shot 2020-12-30 at 7 51 05 PM

@karuppiah7890
Copy link
Author

karuppiah7890 commented Dec 30, 2020

With yaml, the output looks nice, but with JSON, it doesn't look that great actually, even though JSON is only a subset of YAML. I mean, the braces and the spaces have some highlighting. I have to check if this is really expected

Screen Shot 2020-12-30 at 7 53 08 PM

@karuppiah7890
Copy link
Author

And I haven't added any unit or E2E test. I was wondering if we should test it and what to test and how to do it.

Should we add just E2E test? Since it's more CLI output related. I can't think of anything related to unit test, but I can see that there are some unit tests around output. We could do that.

Any thoughts on testing?

@AssafKatz3
Copy link

And I haven't added any unit or E2E test. I was wondering if we should test it and what to test and how to do it.

Should we add just E2E test? Since it's more CLI output related. I can't think of anything related to unit test, but I can see that there are some unit tests around output. We could do that.

Any thoughts on testing?

  1. Check that the option has impact (different output) for JSON & YAML with/without this option
  2. Check that error code is consistent with and without this option
  3. Check that writing it twice pass correctly
  4. Check that the first line of both YAML & JSON output contains color sequence
  5. Check the color of YAML by reread the output and checking that all color sequences are in place (It is problematic for JSON since it will fail for numbers)

@karuppiah7890
Copy link
Author

karuppiah7890 commented Dec 30, 2020

Could you explain more about what points 2, 3 and 5 mean? I didn't understand them properly

@AssafKatz3
Copy link

Could you explain more about what points 2, 3 and 5 mean? I didn't understand them properly

2 - Ensure that if the command failed without color, it is failing with color also
3 - Call command with "-c -c" and see that it is working
5 - Call command with color, read the output and compare it to what should be the output (with color character sequences)

@karuppiah7890
Copy link
Author

What's the benefit of 4 (single line check) if 5 does a complete check?

@AssafKatz3
Copy link

What's the benefit of 4 (single line check) if 5 does a complete check?

4 is working for JSON also, I agree that for YAML is redundant if you will do also 5.

@karuppiah7890
Copy link
Author

About 2 - Why would the error code change because of this option? Only when highlighting has some issues (error), there will be a non-zero exit code

@AssafKatz3
Copy link

About 2 - Why would the error code change because of this option? Only when highlighting has some issues (error), there will be a non-zero exit code

It shouldn't - this is reason why need such test (I had such bug 20 years ago and it was really nagging)

@karuppiah7890
Copy link
Author

karuppiah7890 commented Dec 30, 2020

Makes sense. I'll add it.

But I was just wondering, so, the test strategy is -

For every feature flag that's present, test if the usage of any flag causes the exit code to change in an unexpected manner? Doesn't that sound tedious? Or it could be done as some sort of parameterized test, but still that would be a lot of combinations depending on the number of flags and possible error situations

This project doesn't have much flags, so all good. Just wondering in general 😅

@AssafKatz3
Copy link

This project doesn't have much flags, so all good. Just wondering in general 😅

I think that this is question for @itaysk and should be documented somewhere.

@karuppiah7890
Copy link
Author

Cool. I thought you would have an answer since you suggested the strategy

@karuppiah7890
Copy link
Author

karuppiah7890 commented Dec 30, 2020

Another thing is, I have only written code for the root command. I'm yet to understand and try out the "get" sub command. If it needs the color, we need to add for it too (looks like it does need it)

@karuppiah7890
Copy link
Author

Do we surely need the full output check? I think we can trust the library and just check that the option works - that is, invokes the library, by just checking the first line

@AssafKatz3
Copy link

Do we surely need the full output check? I think we can trust the library and just check that the option works - that is, invokes the library, by just checking the first line

I am not sure this why I split it to two different tests.

@karuppiah7890
Copy link
Author

I have finished all the stuff for this PR. cc @itaysk

@karuppiah7890
Copy link
Author

You can review and let me know how it looks and if there are any more changes to be done :)

@AssafKatz3
Copy link

@karuppiah7890
The library that this code rely on, which in turn depended on another library which isn't working well on Windows. I didn't check it personally, but it is something to check (manually at least) :-(

@karuppiah7890
Copy link
Author

@AssafKatz3 cool, I'll check it out! Currently we still don't have support for windows however (no artifacts) but yeah, I'll be sure to check it out

return nil
},
}

func printOutput(cmd *cobra.Command, out []byte) error {
Copy link
Owner

@itaysk itaysk Jan 10, 2021

Choose a reason for hiding this comment

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

instead of passing the entire cmd, can you please pass just cmd.OutOrStdout() as io.writer?

if err != nil {
return err
}
cmd.Println(highlightedOutput)
Copy link
Owner

Choose a reason for hiding this comment

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

please rebase and use Print based on #57

@karuppiah7890
Copy link
Author

Apparently the library may not work on Windows - #52 (comment) . I'm not working on this PR now, so I'll close it. I hope new contributors will have an opportunity to work on it and also check support for windows based on #6 , #53

@karuppiah7890
Copy link
Author

I'll resume working on this soon

@karuppiah7890 karuppiah7890 marked this pull request as draft October 3, 2021 16:44
@karuppiah7890
Copy link
Author

karuppiah7890 commented Oct 18, 2021

I'm gonna be checking about https://github.com/alecthomas/chroma library if that's okay.

cc @itaysk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

syntax highlight
3 participants