src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/eval/builtin_fn_styled_test.elvts (about)

     1  // The transcript testing framework strips SGR sequences from stdout and stderr,
     2  // so we need to use to-string when testing them.
     3  
     4  //////////////////
     5  # styled-segment #
     6  //////////////////
     7  
     8  ## converting a string to a segment ##
     9  ~> to-string (styled-segment abc)
    10  ▶ "\e[mabc"
    11  
    12  ## styling a string ##
    13  ~> to-string (styled-segment abc &fg-color=red)
    14  ▶ "\e[;31mabc\e[m"
    15  
    16  ## overriding the style of an existing segment ##
    17  ~> to-string (styled-segment (styled-segment abc &fg-color=red) &fg-color=magenta)
    18  ▶ "\e[;35mabc\e[m"
    19  
    20  ## bad usage ##
    21  ~> styled-segment []
    22  Exception: argument to styled-segment must be a string or a styled segment
    23    [tty]:1:1-17: styled-segment []
    24  ~> styled-segment text &foo=bar
    25  Exception: unrecognized option 'foo'
    26    [tty]:1:1-28: styled-segment text &foo=bar
    27  
    28  ## introspection ##
    29  ~> put (styled-segment abc &italic=$true &fg-color=red)[bold]
    30  ▶ $false
    31  ~> put (styled-segment abc &italic=$true &fg-color=red)[italic]
    32  ▶ $true
    33  ~> put (styled-segment abc &italic=$true &fg-color=red)[fg-color]
    34  ▶ red
    35  
    36  //////////
    37  # styled #
    38  //////////
    39  
    40  ## converting and transforming strings ##
    41  ~> to-string (styled abc)
    42  ▶ "\e[mabc"
    43  ~> to-string (styled abc bold)
    44  ▶ "\e[;1mabc\e[m"
    45  
    46  ## converting and transforming styled segments ##
    47  ~> to-string (styled (styled-segment abc &fg-color=red))
    48  ▶ "\e[;31mabc\e[m"
    49  ~> to-string (styled (styled-segment abc &fg-color=red) bold)
    50  ▶ "\e[;1;31mabc\e[m"
    51  
    52  ## transforming another styled text ##
    53  ~> to-string (styled (styled abc red) bold)
    54  ▶ "\e[;1;31mabc\e[m"
    55  
    56  ## function as transformer ##
    57  ~> to-string (styled abc {|s| put $s })
    58  ▶ "\e[mabc"
    59  ~> to-string (styled abc {|s| styled-segment $s &bold=$true &italic=$false })
    60  ▶ "\e[;1mabc\e[m"
    61  
    62  ## mixed string and function transformers ##
    63  ~> to-string (styled abc italic {|s| styled-segment $s &bold=$true })
    64  ▶ "\e[;1;3mabc\e[m"
    65  
    66  ## error from function transformer ##
    67  ~> styled abc {|_| fail bad }
    68  Exception: bad
    69    [tty]:1:17-25: styled abc {|_| fail bad }
    70    [tty]:1:1-26: styled abc {|_| fail bad }
    71  ~> styled abc {|_| put a b }
    72  Exception: styling function must return a single segment; got 2 values
    73    [tty]:1:1-25: styled abc {|_| put a b }
    74  ~> styled abc {|_| put [] }
    75  Exception: styling function must return a segment; got list
    76    [tty]:1:1-24: styled abc {|_| put [] }
    77  
    78  ## bad usage ##
    79  ~> styled abc hopefully-never-exists
    80  Exception: hopefully-never-exists is not a valid style transformer
    81    [tty]:1:1-33: styled abc hopefully-never-exists
    82  ~> styled []
    83  Exception: expected string, styled segment or styled text; got list
    84    [tty]:1:1-9: styled []
    85  ~> styled abc []
    86  Exception: need string or callable; got list
    87    [tty]:1:1-13: styled abc []
    88  
    89  ## doesn't modify the argument ##
    90  
    91  ~> var x = (styled text)
    92     var y = (styled $x red)
    93     put $x[0][fg-color]
    94  ▶ default
    95  ~> var x = (styled-segment text)
    96     var y = (styled $x red)
    97     put $x[fg-color]
    98  ▶ default
    99  
   100  ## introspection ##
   101  
   102  ~> put (styled abc red)[0][bold]
   103  ▶ $false
   104  ~> put (styled abc red)[0][bg-color]
   105  ▶ default
   106  
   107  /////////////////////////////
   108  # concatenating styled text #
   109  /////////////////////////////
   110  
   111  ## segment + string ##
   112  ~> to-string (styled-segment abc &fg-color=red)abc
   113  ▶ "\e[;31mabc\e[mabc"
   114  
   115  ## segment + segment ##
   116  ~> to-string (styled-segment abc &bg-color=red)(styled-segment abc &fg-color=red)
   117  ▶ "\e[;41mabc\e[;31mabc\e[m"
   118  
   119  ## segment + text ##
   120  ~> to-string (styled-segment abc &underlined=$true)(styled abc bright-cyan)
   121  ▶ "\e[;4mabc\e[;96mabc\e[m"
   122  
   123  ## segment + num ##
   124  ~> to-string (styled-segment abc &blink)(num 44/3)
   125  ▶ "\e[;5mabc\e[m44/3"
   126  ~> to-string (styled-segment abc &blink)(num 42)
   127  ▶ "\e[;5mabc\e[m42"
   128  
   129  ## segment + unsupported ##
   130  ~> to-string (styled-segment abc){ }
   131  Exception: cannot concatenate ui:text-segment and fn
   132    [tty]:1:11-33: to-string (styled-segment abc){ }
   133  
   134  ## string + segment ##
   135  ~> to-string abc(styled-segment abc &fg-color=red)
   136  ▶ "\e[mabc\e[31mabc\e[m"
   137  
   138  ## num + segment ##
   139  ~> to-string (num 99.0)(styled-segment abc &blink)
   140  ▶ "\e[m99.0\e[5mabc\e[m"
   141  ~> to-string (num 66)(styled-segment abc &blink)
   142  ▶ "\e[m66\e[5mabc\e[m"
   143  ~> to-string (num 3/2)(styled-segment abc &blink)
   144  ▶ "\e[m3/2\e[5mabc\e[m"
   145  
   146  ## unsupported + segment ##
   147  ~> to-string { }(styled-segment abc)
   148  Exception: cannot concatenate fn and ui:text-segment
   149    [tty]:1:11-33: to-string { }(styled-segment abc)
   150  
   151  ## text + string ##
   152  ~> to-string (styled abc blink)abc
   153  ▶ "\e[;5mabc\e[mabc"
   154  
   155  ## text + number ##
   156  ~> to-string (styled abc blink)(num 13)
   157  ▶ "\e[;5mabc\e[m13"
   158  ~> to-string (styled abc blink)(num 3/4)
   159  ▶ "\e[;5mabc\e[m3/4"
   160  
   161  ## text + segment ##
   162  ~> to-string (styled abc inverse)(styled-segment abc &bg-color=white)
   163  ▶ "\e[;7mabc\e[;47mabc\e[m"
   164  
   165  ## text + text ##
   166  ~> to-string (styled abc bold)(styled abc dim)
   167  ▶ "\e[;1mabc\e[;2mabc\e[m"
   168  
   169  ## text + unsupported ##
   170  ~> to-string (styled abc){ }
   171  Exception: cannot concatenate ui:text and fn
   172    [tty]:1:11-25: to-string (styled abc){ }
   173  
   174  ## string + text ##
   175  ~> to-string abc(styled abc blink)
   176  ▶ "\e[mabc\e[5mabc\e[m"
   177  
   178  ## number + text ##
   179  ~> to-string (num 13)(styled abc blink)
   180  ▶ "\e[m13\e[5mabc\e[m"
   181  ~> to-string (num 4/3)(styled abc blink)
   182  ▶ "\e[m4/3\e[5mabc\e[m"
   183  
   184  ## unsupported + text ##
   185  ~> to-string { }(styled abc)
   186  Exception: cannot concatenate fn and ui:text
   187    [tty]:1:11-25: to-string { }(styled abc)
   188  
   189  ## introspecting concatenated text ##
   190  ~> var t = (styled-segment abc &underlined=$true)(styled abc bright-cyan)
   191     put $t[1][fg-color]
   192  ▶ bright-cyan
   193  ~> var t = (styled-segment abc &underlined=$true)(styled abc bright-cyan)
   194     put $t[1][underlined]
   195  ▶ $false