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 support for colon-separated values #657

Merged
merged 2 commits into from
Nov 18, 2024
Merged

Add support for colon-separated values #657

merged 2 commits into from
Nov 18, 2024

Conversation

noborus
Copy link
Owner

@noborus noborus commented Nov 17, 2024

Separated SGR within CSI and supported colon-separated SGR. Also supported redundant SGR specifications.

The color specification of the SGR is invalidated outside the range, and the invalid string becomes an error.

Separated SGR within CSI and supported colon-separated SGR.
Also supported redundant SGR specifications.

The color specification of the SGR is invalidated outside the range,
and the invalid string becomes an error.
@ccoVeille
Copy link
Contributor

Hey @noborus

Can you explicit what the acronyms state for?

SGR
CSI

@@ -24,6 +24,18 @@ const (
oscURL
)

// Finalbyte is a character outside the escape sequence.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// Finalbyte is a character outside the escape sequence.
// FinalByte is a character outside the escape sequence.

I could suggest a linter for that

// ColorsRGB is the index of the RGB color. 24-bit colors. r:0-255 g:0-255 b:0-255.
ColorsRGB = 2
)

// escape sequence states.
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
// escape sequence states.
// escapeSequence states.

return tcell.StyleDefault.Normal(), nil
// sgrStyle returns tcell.Style from the SGR control sequence.
func sgrStyle(style tcell.Style, paramStr string) tcell.Style {
if paramStr == "0" || paramStr == "" || paramStr == ";" {
Copy link
Contributor

Choose a reason for hiding this comment

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

I would suggest using a switch with one case listing the 3 values

It will improve readability

Copy link
Owner Author

Choose a reason for hiding this comment

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

Thank you.

case 0:

switch sgr.code {
case 0: // Reset.
Copy link
Contributor

Choose a reason for hiding this comment

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

Use constants for the numbers, this way code is clearer and you improve readability, because then you don't need comment

Suggested change
case 0: // Reset.
case sgrCodeReset:

Copy link
Owner Author

Choose a reason for hiding this comment

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

I disagree with this.
Writing escape sequences with (uncommon) names makes them difficult to read.

i, color := csColor(fields[index:])
if i == 0 {
return s, nil
case 30, 31, 32, 33, 34, 35, 36, 37: // Foreground color
Copy link
Contributor

Choose a reason for hiding this comment

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

See here

colorForegroundBlack, colorForegroundRed ...

func toESCode(str string) (int, error) {
num, err := strconv.Atoi(str)
// toSGRCode converts the SGR parameter to a code.
// Supports both separators (:) and (;).
Copy link
Contributor

Choose a reason for hiding this comment

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

You said it supports both, but I only see a split on : 🤔

Copy link
Owner Author

Choose a reason for hiding this comment

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

This was not a good comment.
If there was no colon(:) separated string, the paramlist (which was semicolon(;;) separated) parameter would be used.

Comment on lines 457 to 458
func parseRGBColor(params []string) (string, error) {
if params[0] == "" || params[1] == "" || params[2] == "" {
Copy link
Contributor

@ccoVeille ccoVeille Nov 17, 2024

Choose a reason for hiding this comment

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

Either you use

Suggested change
func parseRGBColor(params []string) (string, error) {
if params[0] == "" || params[1] == "" || params[2] == "" {
func parseRGBColor(params [3]string) (string, error) {
if params[0] == "" || params[1] == "" || params[2] == "" {

Either you return an error if len(params) != 3)

Otherwise, your code will panic

Copy link
Owner Author

@noborus noborus Nov 17, 2024

Choose a reason for hiding this comment

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

It is checked before, so change the passing by slice.

}
}

func Test_parseSGR2(t *testing.T) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Why 2 ?

You could simply add two t.Run block in Test_parseSGR

Copy link
Owner Author

Choose a reason for hiding this comment

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

I want to separate them because there are too many, but it is hard to think of names...

@@ -426,8 +426,8 @@ var (
ErrNotAlignMode = errors.New("not an align mode")
// ErrNoColumnSelected indicates that no column is selected.
ErrNoColumnSelected = errors.New("no column selected")
// ErrInvalidCSI indicates that the CSI is invalid.
ErrInvalidCSI = errors.New("invalid CSI")
Copy link
Contributor

Choose a reason for hiding this comment

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

Are you sure no one use you as a lib?

You should set it to ErrInvalidSGR (or keep it as is)

And add a // Deprecated: flag in the godoc

Copy link
Owner Author

Choose a reason for hiding this comment

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

I added this 2 days ago, so I'm pretty sure no one is using this.

@noborus
Copy link
Owner Author

noborus commented Nov 17, 2024

Hey @noborus

Can you explicit what the acronyms state for?

SGR CSI

Thank you for your comment.
Fix comments.

@noborus noborus merged commit 8a41959 into master Nov 18, 2024
8 checks passed
@noborus noborus deleted the add-colon-separate branch November 25, 2024 02:21
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.

2 participants