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

Zero width format #912

Closed
wants to merge 3 commits into from
Closed

Conversation

mathstuf
Copy link
Contributor

@mathstuf mathstuf commented Feb 2, 2015

I decided to not do %0{ and %} because the logic for control sequences more than just a single character is a messier. I can also change it over to %{ and %} if separate open/close sequences are preferable. It is not (currently) an error to have mismatched open/close sequences, but it is also not a zero-width section and %0 will be literal in that case.


Fixes #713.

@mathstuf
Copy link
Contributor Author

Ping? @martine? @nico?

@evmar
Copy link
Collaborator

evmar commented Feb 23, 2015

I'm not so familiar with this corner of terminal IO. What is the normal syntax for zero-width markers in other similar languages? (bash/zsh?)

@mathstuf
Copy link
Contributor Author

zsh uses %0{ and %} which is messy due to the two-character escape. bash seems to use \[ and \].

@nico
Copy link
Collaborator

nico commented Feb 26, 2015

The motivation here is to let eliding be correct if NINJA_STATUS contains ansi codes, right? Maybe we could have the eliding function not count ansi codes when computing widths?

@mathstuf
Copy link
Contributor Author

I could for see using it to change tmux's title using its escape codes, but right now I'd be fine with color. Does the color stripping understand 256color sequences?

@nico
Copy link
Collaborator

nico commented Feb 27, 2015

Are they special? Currently it does this: https://github.com/martine/ninja/blob/master/src/util.cc#L477

@mathstuf
Copy link
Contributor Author

Here's the command which does the work of setting the title for me:

printf "\033]2;%s\033\\" "$title"

@nico
Copy link
Collaborator

nico commented Feb 27, 2015

I meant the 256color sequences.

(Do I understand you correctly that you want your ninja status to modify your tmux title? Why?)

@mathstuf
Copy link
Contributor Author

Ah, sorry, missed the context. Well, I don't personally, but this would also support that seamlessly (possibly counting down the targets?). Here's my 256-color code:

for color in {0..255}; do
    ZSH_COLOR[$color]=$'\033[38;5;'$color'm'
    ZSH_COLOR_PS1[$color]="%0{${ZSH_COLOR[$color]}%}"
done

It looks like the existing code should work.

@evmar
Copy link
Collaborator

evmar commented Feb 27, 2015

@nico modifying your terminal title is commonly a hack for long-running processes to display status in a more visible place like alt-tab lists. Some WMs even let you "shade" a window (collapsing it to its title bar) as a substitute for minimization.

@nico
Copy link
Collaborator

nico commented Feb 28, 2015

Right, but is this something one would want to do every time a ninja command updates? (I honestly don't know.)

If we end up doing #486 (which I currently do want to do, to get colors from clang diagnostics on Windows), ninja will have some knowledge about ansi codes anyhow, so it doesn't seem unreasonable for Elide() to learn about them too maybe.

Are there other use cases for zero-width markers other than ansi codes?

@evmar
Copy link
Collaborator

evmar commented Mar 1, 2015

From a pedantic perspective it's hard to know in advance the full
possibility space of escapes, see e.g.
http://tldp.org/HOWTO/Xterm-Title-6.html

(PS: there was actually a vulnerability in one of these in the terminal
escape parser -- it allowed you to e.g. hack someone by sending them a
specially formed string via IRC if they were running their client in a
shell!)

However it might be the case that everything these days uses ANSI-style
escapes.

On Sat, Feb 28, 2015 at 1:35 PM, Nico Weber [email protected]
wrote:

Right, but is this something one would want to do every time a ninja
command updates? (I honestly don't know.)

If we end up doing #486 #486
(which I currently do want to do, to get colors from clang diagnostics on
Windows), ninja will have some knowledge about ansi codes anyhow, so it
doesn't seem unreasonable for Elide() to learn about them too maybe.

Are there other use cases for zero-width markers other than ansi codes?


Reply to this email directly or view it on GitHub
#912 (comment).

@mathstuf
Copy link
Contributor Author

%0 is output literally with -v, so that path needs to strip things as well.

@mathstuf
Copy link
Contributor Author

Fixed the -v flag issue.

mathstuf added 3 commits May 24, 2016 11:31
If the string fits, just use it. If we need the ellipses, *then* we need
to compute the width based on that.
This is useful for color control sequences so that they do not cause
unnecessary elision (or get elided themselves causing either color
leakage or split control sequences).
@mathstuf mathstuf force-pushed the zero-width-format branch from 371c16b to 59fe29e Compare May 24, 2016 15:31
mqudsi added a commit to mqudsi/ninja that referenced this pull request Jul 30, 2018
Adds support for ANSI escape sequences used for colorization to the job
status indicator, and updates `ElideMiddle()` to account for the
non-obvious change in text width.

As an example, the following will now emit the job status in green:

   env NINJA_STATUS="\\033[32m[%f/%t] \\033[0m" ninja;

Invalid color escape codes are treated as plain text (as before). ANSI
escape sequences apart from `\033...m` are not supported.

Closes ninja-build#713. Closes ninja-build#912.
mqudsi added a commit to mqudsi/ninja that referenced this pull request Jul 30, 2018
Adds support for ANSI escape sequences used for colorization to the job
status indicator, and updates `ElideMiddle()` to account for the
non-obvious change in text width.

As an example, the following will now emit the job status in green:

   env NINJA_STATUS="\\033[32m[%f/%t] \\033[0m" ninja;

Invalid color escape codes are treated as plain text (as before). ANSI
escape sequences apart from `\033...m` are not supported.

Closes ninja-build#713. Closes ninja-build#912.
@mathstuf
Copy link
Contributor Author

Some parts of this are still necessary. It looks like the width of escape sequences aren't removed when calculating elision. This can cause color leakage if the "clear color" sequence is elided.

@jhasse
Copy link
Collaborator

jhasse commented Nov 13, 2018

https://github.com/ninja-build/ninja/pull/1454/files#diff-005d118f141929270b3e1c4150887ae2R573 implements the stripping for ElideMiddle. I think we need to adjust this, so that it falls back to result.size() when there aren't any escapes sequences in NINJA_STATUS. This way there wouldn't be a slow down for users who aren't using colors in NINJA_STATUS (besides the one time parsing at the start, which should be negligible).

Also #1210 might be the better solution, to keep Ninja simple.

@mathstuf
Copy link
Contributor Author

Working on a patch now. It does ANSI detection by "spans" so that there's only a single string.find for escape-less sequences.

@mathstuf
Copy link
Contributor Author

Also, the linked patch isn't right because it detects elision properly, but doesn't split the string using the escape sequence information to not slice in the middle of one.

@jhasse
Copy link
Collaborator

jhasse commented Nov 13, 2018

Yeah, getting this right can be tricky, as you also don't want to skip any escape sequences even when they are in the elided area. I've already implemented it in Python here: https://github.com/jhasse/ja/blob/6603076e84083ed9155404268336b5ce5a771bc4/ja/native.py#L244

@mathstuf
Copy link
Contributor Author

Almost have it. PR up shortly.

@mathstuf
Copy link
Contributor Author

Closing; this is handled elsewhere (see #1491 and #1494).

@mathstuf mathstuf closed this Nov 13, 2018
mqudsi added a commit to mqudsi/ninja that referenced this pull request Jan 1, 2019
Adds support for ANSI escape sequences used for colorization to the job
status indicator, and updates `ElideMiddle()` to account for the
non-obvious change in text width.

As an example, the following will now emit the job status in green:

   env NINJA_STATUS="\\033[32m[%f/%t] \\033[0m" ninja;

Invalid color escape codes are treated as plain text (as before). ANSI
escape sequences apart from `\033...m` are not supported.

Closes ninja-build#713. Closes ninja-build#912.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Support color in NINJA_STATUS
4 participants