src.elv.sh@v0.21.0-dev.0.20240515223629-06979efb9a2a/pkg/eval/builtin_fn_io.d.elv (about) 1 # Takes arbitrary arguments and write them to the structured stdout. 2 # 3 # Examples: 4 # 5 # ```elvish-transcript 6 # ~> put a 7 # ▶ a 8 # ~> put lorem ipsum [a b] [&k=v] 9 # ▶ lorem 10 # ▶ ipsum 11 # ▶ [a b] 12 # ▶ [&k=v] 13 # ``` 14 # 15 # **Note**: It is almost never necessary to use `put (...)` - just write the 16 # `...` part. For example, `put (eq a b)` is the equivalent to just `eq a b`. 17 # 18 # Etymology: Various languages, in particular 19 # [C](https://manpages.debian.org/stretch/manpages-dev/puts.3.en.html) and 20 # [Ruby](https://ruby-doc.org/core-2.2.2/IO.html#method-i-puts) as `puts`. 21 fn put {|@value| } 22 23 # Output `$value` for `$n` times. Example: 24 # 25 # ```elvish-transcript 26 # ~> repeat 0 lorem 27 # ~> repeat 4 NAN 28 # ▶ NAN 29 # ▶ NAN 30 # ▶ NAN 31 # ▶ NAN 32 # ``` 33 # 34 # Etymology: [Clojure](https://clojuredocs.org/clojure.core/repeat). 35 fn repeat {|n value| } 36 37 # Reads `$n` bytes, or until end-of-file, and outputs the bytes as a string 38 # value. The result may not be a valid UTF-8 string. 39 # 40 # Examples: 41 # 42 # ```elvish-transcript 43 # ~> echo "a,b" | read-bytes 2 44 # ▶ 'a,' 45 # ~> echo "a,b" | read-bytes 10 46 # ▶ "a,b\n" 47 # ``` 48 fn read-bytes {|n| } 49 50 # Reads byte input until `$terminator` or end-of-file is encountered. It outputs the part of the 51 # input read as a string value. The output contains the trailing `$terminator`, unless `read-upto` 52 # terminated at end-of-file. 53 # 54 # The `$terminator` must be a single ASCII character such as `"\x00"` (NUL). 55 # 56 # Examples: 57 # 58 # ```elvish-transcript 59 # ~> echo "a,b,c" | read-upto "," 60 # ▶ 'a,' 61 # ~> echo "foo\nbar" | read-upto "\n" 62 # ▶ "foo\n" 63 # ~> echo "a.elv\x00b.elv" | read-upto "\x00" 64 # ▶ "a.elv\x00" 65 # ~> print "foobar" | read-upto "\n" 66 # ▶ foobar 67 # ``` 68 fn read-upto {|terminator| } 69 70 # Reads a single line from byte input, and writes the line to the value output, 71 # stripping the line ending. A line can end with `"\r\n"`, `"\n"`, or end of 72 # file. Examples: 73 # 74 # ```elvish-transcript 75 # ~> print line | read-line 76 # ▶ line 77 # ~> print "line\n" | read-line 78 # ▶ line 79 # ~> print "line\r\n" | read-line 80 # ▶ line 81 # ~> print "line-with-extra-cr\r\r\n" | read-line 82 # ▶ "line-with-extra-cr\r" 83 # ``` 84 fn read-line { } 85 86 # Like `echo`, just without the newline. 87 # 88 # See also [`echo`](). 89 # 90 # Etymology: Various languages, in particular 91 # [Perl](https://perldoc.perl.org/functions/print.html) and 92 # [zsh](http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html), whose 93 # `print`s do not print a trailing newline. 94 fn print {|&sep=' ' @value| } 95 96 # Prints values to the byte stream according to a template. 97 # 98 # The template may contain "formatting verbs", sequences that start with `%`. 99 # Each verb corresponds to an argument, and specifies how that argument will be 100 # converted and formatted: 101 # 102 # - `%s`, `%q` and `%v` convert the argument to a string: 103 # 104 # - `%s`: use [to-string](#to-string) 105 # - `%q`: use [repr](#repr) 106 # - `%v`: equivalent to `%s` 107 # - `%#v`: equivalent to `%q`. 108 # 109 # - `%t` converts the argument to a boolean using [bool](#bool), and prints it 110 # as either `true` or `false`. 111 # 112 # - `%b`, `%c`, `%d`, `%o`, `%O`, `%x`, `%X` and `%U` convert the argument to an 113 # integer, and then print it as follows: 114 # 115 # - `%b`: base 2 116 # - `%c`: the character represented by the Unicode codepoint 117 # - `%d`: base 10 118 # - `%o`: base 8 119 # - `%O`: base 8, with `0o` prefix 120 # - `%x`: base 16, with lower-case letters for a-f 121 # - `%X`: base 16, with upper-case letters for A-F 122 # - `%U`: Unicode format like U+1234; same as `U+%04X` 123 # 124 # - `%e`, `%E`, `%f`, `%F`, `%g` and `%G` convert the argument to a 125 # floating-point number using [`inexact-num`](), and print it as follows: 126 # 127 # - `%e`: scientific notation like -1.234456e+78 128 # - `%E`: scientific notation like -1.234456E+78 129 # - `%f`: decimal point but no exponent, like 123.456 130 # - `%F`: same as `%f` 131 # - `%g`: `%e` for large exponents, `%f` otherwise 132 # - `%G`: `%E` for large exponents, `%F` otherwise 133 # 134 # - `%%` prints a literal `%` and consumes no argument. 135 # 136 # Unsupported verbs not documented don't cause exceptions, but the output will 137 # contain content like `%!Z(unsupported formatting verb)`. 138 # 139 # Flags may be added between `%` and the verb to modify behavior like precision 140 # and padding. See Go's [`fmt`](https://golang.org/pkg/fmt/#hdr-Printing) 141 # package for details. 142 # 143 # This command does not add a trailing newline. Include it explicitly in the 144 # template, like `printf "Hello %s\n" world`. 145 # 146 # Examples: 147 # 148 # ```elvish-transcript 149 # ~> use math 150 # ~> printf ": %10s %.3f\n" Pi $math:pi 151 # printf ": %10s %.3f\n" E $math:e 152 # : Pi 3.142 153 # : E 2.718 154 # ~> printf "%-10s %.2f %s\n" Pi $math:pi $math:pi 155 # Pi 3.14 3.141592653589793 156 # ~> printf "%d\n" 0b11100111 157 # 231 158 # ~> printf "%08b\n" 231 159 # 11100111 160 # ~> printf "list is: %q\n" [foo bar 'foo bar'] 161 # list is: [foo bar 'foo bar'] 162 # ``` 163 # 164 # Since `printf` writes to the byte stream, capturing its output will generate 165 # one value per line. To capture it into one string value, use it together with 166 # [`slurp`](), as in `var s = (printf ... | slurp)`. 167 # 168 # **Note**: Compared to the [POSIX `printf` 169 # command](https://pubs.opengroup.org/onlinepubs/007908799/xcu/printf.html) 170 # found in other shells, there are 3 key differences: 171 # 172 # - The behavior of the formatting verbs are based on Go's 173 # [`fmt`](https://golang.org/pkg/fmt/) package instead of the POSIX 174 # specification. 175 # 176 # - The number of arguments after the formatting template must match the number 177 # of formatting verbs. The POSIX command will repeat the template string to 178 # consume excess values; this command does not have that behavior. 179 # 180 # - This command does not interpret escape sequences such as `\n`; just use 181 # [double-quoted strings](language.html#double-quoted-string). 182 # 183 # See also [`print`](), [`echo`](), [`pprint`](), and [`repr`](). 184 fn printf {|template @value| } 185 186 # Print all arguments, joined by the `sep` option, and followed by a newline. 187 # 188 # Examples: 189 # 190 # ```elvish-transcript 191 # ~> echo Hello elvish 192 # Hello elvish 193 # ~> echo "Hello elvish" 194 # Hello elvish 195 # ~> echo &sep=, lorem ipsum 196 # lorem,ipsum 197 # ``` 198 # 199 # Notes: The `echo` builtin does not treat `-e` or `-n` specially. For instance, 200 # `echo -n` just prints `-n`. Use double-quoted strings to print special 201 # characters, and `print` to suppress the trailing newline. 202 # 203 # See also [`print`](). 204 # 205 # Etymology: Bourne sh. 206 fn echo {|&sep=' ' @value| } 207 208 # Pretty-print representations of Elvish values. Examples: 209 # 210 # ```elvish-transcript 211 # ~> pprint [foo bar] 212 # [ 213 # foo 214 # bar 215 # ] 216 # ~> pprint [&k1=v1 &k2=v2] 217 # [ 218 # &k1= v1 219 # &k2= v2 220 # ] 221 # ``` 222 # 223 # The output format is subject to change. 224 # 225 # See also [`repr`](). 226 fn pprint {|@value| } 227 228 # Writes representation of `$value`s, separated by space and followed by a 229 # newline. Example: 230 # 231 # ```elvish-transcript 232 # ~> repr [foo 'lorem ipsum'] "aha\n" 233 # [foo 'lorem ipsum'] "aha\n" 234 # ``` 235 # 236 # See also [`pprint`](). 237 # 238 # Etymology: [Python](https://docs.python.org/3/library/functions.html#repr). 239 fn repr {|@value| } 240 241 # Shows the value to the output, which is assumed to be a VT-100-compatible 242 # terminal. 243 # 244 # Currently, the only type of value that can be showed is exceptions, but this 245 # will likely expand in future. 246 # 247 # Example: 248 # 249 # ```elvish-transcript 250 # ~> var e = ?(fail lorem-ipsum) 251 # ~> show $e 252 # Exception: lorem-ipsum 253 # [tty]:1:11-26: var e = ?(fail lorem-ipsum) 254 # ``` 255 fn show {|e| } 256 257 # Passes byte input to output, and discards value inputs. 258 # 259 # Example: 260 # 261 # ```elvish-transcript 262 # ~> { put value; echo bytes } | only-bytes 263 # bytes 264 # ``` 265 fn only-bytes { } 266 267 # Passes value input to output, and discards byte inputs. 268 # 269 # Example: 270 # 271 # ```elvish-transcript 272 # ~> { put value; echo bytes } | only-values 273 # ▶ value 274 # ``` 275 fn only-values { } 276 277 # Reads bytes input into a single string, and put this string on structured 278 # stdout. 279 # 280 # Example: 281 # 282 # ```elvish-transcript 283 # ~> echo "a\nb" | slurp 284 # ▶ "a\nb\n" 285 # ``` 286 # 287 # Etymology: Perl, as 288 # [`File::Slurp`](http://search.cpan.org/~uri/File-Slurp-9999.19/lib/File/Slurp.pm). 289 fn slurp { } 290 291 # Splits byte input into lines, and writes them to the value output. Value 292 # input is ignored. 293 # 294 # ```elvish-transcript 295 # ~> { echo a; echo b } | from-lines 296 # ▶ a 297 # ▶ b 298 # ~> { echo a; put b } | from-lines 299 # ▶ a 300 # ``` 301 # 302 # See also [`from-terminated`](), [`read-upto`](), and [`to-lines`](). 303 fn from-lines { } 304 305 # Takes bytes stdin, parses it as JSON and puts the result on structured stdout. 306 # The input can contain multiple JSONs, and whitespace between them are ignored. 307 # 308 # Numbers in JSON are parsed as follows: 309 # 310 # - Numbers without fractional parts are parsed as exact integers, with 311 # support for arbitrary precision. 312 # 313 # - Numbers with fractional parts (even if it's `.0`) are parsed as 314 # [inexact](language.html#exactness) floating-point numbers, and the parsing 315 # may fail if the number can't be represented. 316 # 317 # Examples: 318 # 319 # ```elvish-transcript 320 # ~> echo '"a"' | from-json 321 # ▶ a 322 # ~> echo '["lorem", "ipsum"]' | from-json 323 # ▶ [lorem ipsum] 324 # ~> echo '{"lorem": "ipsum"}' | from-json 325 # ▶ [&lorem=ipsum] 326 # ~> # multiple JSONs running together 327 # echo '"a""b"["x"]' | from-json 328 # ▶ a 329 # ▶ b 330 # ▶ [x] 331 # ~> # multiple JSONs separated by newlines 332 # echo '"a" 333 # {"k": "v"}' | from-json 334 # ▶ a 335 # ▶ [&k=v] 336 # ~> echo '[42, 100000000000000000000, 42.0, 42.2]' | from-json 337 # ▶ [(num 42) (num 100000000000000000000) (num 42.0) (num 42.2)] 338 # ``` 339 # 340 # See also [`to-json`](). 341 fn from-json { } 342 343 # Splits byte input into lines at each `$terminator` character, and writes 344 # them to the value output. If the byte input ends with `$terminator`, it is 345 # dropped. Value input is ignored. 346 # 347 # The `$terminator` must be a single ASCII character such as `"\x00"` (NUL). 348 # 349 # ```elvish-transcript 350 # ~> { echo a; echo b } | from-terminated "\x00" 351 # ▶ "a\nb\n" 352 # ~> print "a\x00b" | from-terminated "\x00" 353 # ▶ a 354 # ▶ b 355 # ~> print "a\x00b\x00" | from-terminated "\x00" 356 # ▶ a 357 # ▶ b 358 # ``` 359 # 360 # See also [`from-lines`](), [`read-upto`](), and [`to-terminated`](). 361 fn from-terminated {|terminator| } 362 363 # Writes each input to a separate line in the byte output. 364 # 365 # ```elvish-transcript 366 # ~> put a b | to-lines 367 # a 368 # b 369 # ~> to-lines [a b] 370 # a 371 # b 372 # ``` 373 # 374 # See also [`from-lines`]() and [`to-terminated`](). 375 fn to-lines {|inputs?| } 376 377 # Writes each input to the byte output with the specified terminator character. 378 # 379 # The `$terminator` must be a single ASCII character such as `"\x00"` (NUL). 380 # Using NUL is useful for feeding output to programs that expect NUL-terminated 381 # strings, such as `grep -z` or `xargs -0`. 382 # 383 # ```elvish-transcript 384 # ~> put a b | to-terminated "\x00" | slurp 385 # ▶ "a\x00b\x00" 386 # ~> to-terminated "\x00" [a b] | slurp 387 # ▶ "a\x00b\x00" 388 # ``` 389 # 390 # See also [`from-terminated`]() and [`to-lines`](). 391 fn to-terminated {|terminator inputs?| } 392 393 # Takes structured stdin, convert it to JSON and puts the result on bytes stdout. 394 # 395 # ```elvish-transcript 396 # ~> put a | to-json 397 # "a" 398 # ~> put [lorem ipsum] | to-json 399 # ["lorem","ipsum"] 400 # ~> put [&lorem=ipsum] | to-json 401 # {"lorem":"ipsum"} 402 # ``` 403 # 404 # See also [`from-json`](). 405 fn to-json { }