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

Expand tabs => configurable number of spaces to fix #150 #423

Merged
merged 3 commits into from Jul 6, 2022

Conversation

chris-laplante
Copy link
Collaborator

No description provided.

src/style.rs Show resolved Hide resolved
src/style.rs Outdated Show resolved Hide resolved
src/style.rs Outdated Show resolved Hide resolved
src/style.rs Outdated Show resolved Hide resolved
src/style.rs Outdated Show resolved Hide resolved
@djc
Copy link
Collaborator

djc commented Mar 31, 2022

Thinking about this some more, you replaced tabs at the line level, but most of the formatters can't actually emit tabs. Probably only from literals, message and prefix? So it might pay off to be precise about that.

@chris-laplante
Copy link
Collaborator Author

Thinking about this some more, you replaced tabs at the line level, but most of the formatters can't actually emit tabs. Probably only from literals, message and prefix? So it might pay off to be precise about that.

Yes, I agree. The code changes I am working on now will only replace inside those elements. Just wanted to check if you think it is acceptable complexity to also store away the original message & prefix and re-expand them when tab width changes?

@djc
Copy link
Collaborator

djc commented Mar 31, 2022

How about we only do that if message/prefix/template actually contained any tabs?

@chris-laplante
Copy link
Collaborator Author

How about we only do that if message/prefix/template actually contained any tabs?

So like this?

enum StringMaybeTabs {
    NoTabs(Cow<'static, str>),
    Tabs {
        original: Cow<'static, str>,
        expanded: String,
    }
}

@djc
Copy link
Collaborator

djc commented Mar 31, 2022

Something like that, yes. I'll need to see it in context.

@chris-laplante
Copy link
Collaborator Author

I've been working on this but it is turning out to be pretty ugly code. Trying to keep track of what strings are already expanded vs. not and then re-expanding everything when you change the tab size is unwieldly. To simplify things, what if I just added some mutually exclusive feature flags called "expand_tabs_four_spaces" and "expand_tabs_eight_spaces"? Realistically, I can't imagine why someone would change the size of a tab at runtime anyway. And if someone wants to do it, they can submit the PR :).

@djc
Copy link
Collaborator

djc commented Apr 26, 2022

I wonder if we can query the terminal for its tab size?

https://unix.stackexchange.com/questions/46368/set-tab-width-in-gui-terminal

@chris-laplante
Copy link
Collaborator Author

I wonder if we can query the terminal for its tab size?

https://unix.stackexchange.com/questions/46368/set-tab-width-in-gui-terminal

I looked into that but I don't think there's any way to query it unless you actually print a tab and measure the difference in cursor position (e.g. https://unix.stackexchange.com/a/389266). Plus, who knows how Windows / macOS work.

@djc
Copy link
Collaborator

djc commented Apr 26, 2022

Maybe open an issue against console? This seems like the kind of thing that it could provide.

@chris-laplante
Copy link
Collaborator Author

Maybe open an issue against console? This seems like the kind of thing that it could provide.

OK, will do.

@chris-laplante
Copy link
Collaborator Author

On further thought, I don't think that console are any better positioned to fix this than we are, so I'm going to push on and attempt to make a fix.

It makes more sense here anyway, plus it gets rid of the mem::swap stuff
@chris-laplante
Copy link
Collaborator Author

OK, here's my latest attempt at fixing #150. Still need to write tests for it.

@chris-laplante chris-laplante force-pushed the cpl/tab-handling branch 3 times, most recently from bbcf003 to eecba71 Compare June 23, 2022 22:06
@chris-laplante chris-laplante requested a review from djc June 24, 2022 00:35
@djc
Copy link
Collaborator

djc commented Jun 24, 2022

Sorry, it's been a very busy week, but this is definitely still on my list.

src/progress_bar.rs Show resolved Hide resolved
src/state.rs Outdated Show resolved Hide resolved
src/state.rs Outdated Show resolved Hide resolved
src/state.rs Outdated Show resolved Hide resolved
src/state.rs Outdated Show resolved Hide resolved
src/state.rs Outdated Show resolved Hide resolved
src/state.rs Outdated Show resolved Hide resolved
@@ -232,7 +238,7 @@ impl ProgressStyle {
} => {
buf.clear();
if let Some(formatter) = self.format_map.get(key.as_str()) {
buf.push_str(&formatter(state));
buf.push_str(&formatter(state).replace('\t', &" ".repeat(self.tab_width)));
Copy link
Collaborator

Choose a reason for hiding this comment

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

In #420 this is changed to pass in a Formatter directly. How do we change tab expansion in that case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I think we'll have to modify this line to apply the tab expansion on buf before handing over to the formatter: https://github.com/console-rs/indicatif/pull/420/files#diff-5452dc5fb15e73154c0ecc33e87a14379a97f817fe0cdb0fb9d17df712feb8fbR243

Copy link
Collaborator

@djc djc left a comment

Choose a reason for hiding this comment

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

Approved with the remaining comments addressed. This has turned out quite nice!

src/progress_bar.rs Outdated Show resolved Hide resolved
src/style.rs Outdated Show resolved Hide resolved
@chris-laplante
Copy link
Collaborator Author

OK, just going to wait for the tests to complete then I'll merge :)

@chris-laplante chris-laplante merged commit 4cbdcbf into console-rs:main Jul 6, 2022
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.

None yet

2 participants