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

string pad doesn't work with escapes #7784

Closed
IlanCosman opened this issue Mar 6, 2021 · 7 comments
Closed

string pad doesn't work with escapes #7784

IlanCosman opened this issue Mar 6, 2021 · 7 comments

Comments

@IlanCosman
Copy link
Contributor

Welcome to fish, the friendly interactive shell
Type help for instructions on how to use fish
ilan@arch ~> string pad --width 20 hello
               hello
ilan@arch ~> string pad --width 20 \ehello
ello
ilan@arch ~> string pad --width 20 (set_color red)hello
hello
ilan@arch ~> 

It would be really nice if set_color and string pad would work together.

@faho
Copy link
Member

faho commented Mar 6, 2021

In general, this is to be expected, interpreting arbitrary escape sequences here would require us to become a terminal.

We could probably handle colors and such.

@faho faho added this to the fish-future milestone Mar 6, 2021
@IlanCosman
Copy link
Contributor Author

Honestly, now that I think about it, it wouldn't be so insane to design a shell and a terminal together. Yah people couldn't use whatever their favorite terminal was or whatever, but you could probably do some pretty great stuff with that integration. Off topic though 😂

@IlanCosman
Copy link
Contributor Author

Huh, this seems to work now with Fish 3.2.1 on MacOS and Fish master on Arch. I guess something in 3.2.1 fixed it. 👍

@krobelus
Copy link
Member

I don't think it's fixed, see this:

$ string pad --width 20 (set_color red)hello hello
                     hello
               hello

The escape sequences take 6 characters.
We'll have to think about whether it makes sense to support colors here. Maybe there should be a different command to remove escapes?

@krobelus krobelus reopened this Mar 23, 2021
@IlanCosman
Copy link
Contributor Author

Good point, it's not fixed.

Maybe there should be a different command to remove escapes?

👀 yes please! I've been rolling my own.

@faho faho mentioned this issue Apr 27, 2021
5 tasks
@faho
Copy link
Member

faho commented Jul 17, 2021

Fun fact: This needs to handle multline strings, and there are some ambiguities.

If you have a string like

echo aaa\nbb

The width of that is 3 and then 2. Calling it "3" (the maximum) doesn't make sense, calling it 6 (the "length) makes even less. Really 3, 2 is the correct answer.

But if you have one like

echo aaa\rbb

The width of that is? "3" sorta makes sense because that is how wide it would show up in isolation (it would look like "bba"), but "2" would also make sense because, if you add anything to it it works like it's 2 cells wide (printing a "1" would make it look like "bb1", not "bba1").

And if the "aaa" wraps into the next line? All bets are off. We can't detect it, we can't express it.

I would probably call this "2" wide because that's what it is as soon as anything else happens. It's also easier to handle - as soon as you encounter a \r, reset the width of the current line to 0. (And if you encounter a \n, the line is done)

@faho faho modified the milestones: fish-future, fish 3.4.0 Aug 4, 2021
@faho faho closed this as completed Aug 4, 2021
@faho
Copy link
Member

faho commented Mar 24, 2022

I would probably call this "2" wide because that's what it is as soon as anything else happens. It's also easier to handle - as soon as you encounter a \r, reset the width of the current line to 0. (And if you encounter a \n, the line is done)

For posterity: string length -V aaa\rbb aa\rbbb prints "3" two times. It keeps the longest width. This is to make wrapping calculations work. It also makes \r\n not a problem.

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

No branches or pull requests

3 participants