Skip to content

string pad doesn't work with escapes #7784

Closed

Description

@IlanCosman
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.

Activity

  1. faho commented on Mar 6, 2021

    @faho
    Member

    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.

  2. added this to the fish-future milestone on Mar 6, 2021
  3. IlanCosman commented on Mar 6, 2021

    @IlanCosman
    ContributorAuthor

    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 😂

  4. IlanCosman commented on Mar 23, 2021

    @IlanCosman
    ContributorAuthor

    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. 👍

  5. krobelus commented on Mar 23, 2021

    @krobelus
    Contributor

    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?

  6. IlanCosman commented on Mar 23, 2021

    @IlanCosman
    ContributorAuthor

    Good point, it's not fixed.

    Maybe there should be a different command to remove escapes?

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

  7. faho commented on Jul 17, 2021

    @faho
    Member

    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)

  8. modified the milestones: fish-future, fish 3.4.0 on Aug 4, 2021
  9. faho commented on Mar 24, 2022

    @faho
    Member

    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.

  10. locked as resolved and limited conversation to collaborators on Jun 22, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions