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

Idiomatic label printing #2295

Closed
wants to merge 7 commits into from
Closed

Idiomatic label printing #2295

wants to merge 7 commits into from

Conversation

sharpvik
Copy link

Closes #2294
I looked at the Colorizer code and included an edge case for labels (which are pretty much all strings that end with a colon like "error:").

Colorizer prints pieces out one by one, so this will not have any unpredictable effects. The piece is only ever considered a label if it is a string that ends with a colon (so "error:" is a label but "this didn't work. usage: bla bla bla" is not a label even though "usage:" has a colon).

I ran all tests and they all pass as well as manually checked the result:

works

It works.

@sharpvik
Copy link
Author

Locally, all tests and checks pass, but in CI it can't deal with imports properly for some reason... Still checking why.

Copy link
Member

@ldm0 ldm0 left a comment

Choose a reason for hiding this comment

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

Thanks! Here are my suggestions.

}

buffer.set_color(&color_spec)?;
buffer.write_all(string.as_bytes())?;
Copy link
Member

Choose a reason for hiding this comment

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

Well, this only works if we assume the text pieces doesn't have a trailing : I guess. What about bubbleing the logic up? Editing the start_error() src/parse/errors.rs:400 to may also works.

@@ -108,6 +125,10 @@ impl Colorizer {
write!(stdout, "{}", self)
}
}

fn is_label(s: &str) -> bool {
Copy link
Member

Choose a reason for hiding this comment

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

BTW, this function is not feature gated so there are some warnings.

@sharpvik
Copy link
Author

sharpvik commented Jan 17, 2021

Thanks! Here are my suggestions.

It's hard to properly change due to the fact that the Colorizer is pretty much guessing what you want it terms of font weight. There is an if clause that sets all red text to be bold.

Do you think it might be better to somehow merge formatting together so that it can be better customized without having to add edge cases to the print method? I could do that.

You know something like passing text + color + width to print or even using a library like 'colored' that allows you to do this:

println!("{}", "this is my error message".red().bold())

@ldm0
Copy link
Member

ldm0 commented Jan 17, 2021

Thanks! Here are my suggestions.

It's hard to properly change due to the fact that the Colorizer is pretty much guessing what you want it terms of font weight. There is an if clause that sets all red text to be bold.

I think the original if clause means all red text needs to be bold rather than guessing(?).

Do you think it might be better to somehow merge formatting together so that it can be better customized without having to add edge cases to the print method? I could do that.

You know something like passing text + color + width to print or even using a library like 'colored' that allows you to do this:

println!("{}", "this is my error message".red().bold())

That would be convenient to use.

Actually my point is that you can change the code like this, which may also work:

diff --git a/src/parse/errors.rs b/src/parse/errors.rs      
index c901f890..4991b801 100644
--- a/src/parse/errors.rs
+++ b/src/parse/errors.rs
@@ -398,8 +398,8 @@ impl Display for Error {
 }

 fn start_error(c: &mut Colorizer, msg: impl Into<String>) {
-    c.error("error:");
-    c.none(" ");
+    c.error("error");
+    c.none(": ");
     c.none(msg);
 }

@sharpvik
Copy link
Author

Thanks! Here are my suggestions.

It's hard to properly change due to the fact that the Colorizer is pretty much guessing what you want it terms of font weight. There is an if clause that sets all red text to be bold.

I think the original if clause means all red text needs to be bold rather than guessing(?).

Do you think it might be better to somehow merge formatting together so that it can be better customized without having to add edge cases to the print method? I could do that.

You know something like passing text + color + width to print or even using a library like 'colored' that allows you to do this:

println!("{}", "this is my error message".red().bold())

That would be convenient to use.

Actually my point is that you can change the code like this, which may also work:

diff --git a/src/parse/errors.rs b/src/parse/errors.rs      
index c901f890..4991b801 100644
--- a/src/parse/errors.rs
+++ b/src/parse/errors.rs
@@ -398,8 +398,8 @@ impl Display for Error {
 }

 fn start_error(c: &mut Colorizer, msg: impl Into<String>) {
-    c.error("error:");
-    c.none(" ");
+    c.error("error");
+    c.none(": ");
     c.none(msg);
 }

Oh yeah, that was my initial idea but then I thought it looks weird and also the colon won't be bold in this case but it should be.

@ldm0
Copy link
Member

ldm0 commented Jan 17, 2021

@sharpvik Real perfectionist 😄 . In that case, I think adding extra parameter to indicate bold or not is better?

@sharpvik
Copy link
Author

@sharpvik Real perfectionist 😄 . In that case, I think adding extra parameter to indicate bold or not is better?

Yeah, sounds about right :) I'll do that in a bit.

Copy link
Member

@pksunkara pksunkara left a comment

Choose a reason for hiding this comment

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

This code honestly feels and looks like a hack. I wouldn't merge it as it is right now.

It is because this is the wrong place to fix it. The correct place is here

@sharpvik
Copy link
Author

This code honestly feels and looks like a hack. I wouldn't merge it as it is right now.

It is because this is the wrong place to fix it. The correct place is here

I know, I didn't feel good about it either. I'm not deeply familiar with the codebase and I didn't want to bother by asking, so I followed a trail of usages from c.error("error:") to formatter in fmt.rs and that's how I ended up there...

@pksunkara pksunkara closed this Jan 17, 2021
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.

Consider using idiomatic Rust errors
3 participants