builtin_fn_io.d.elv 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. #elvdoc:fn put
  2. #
  3. # ```elvish
  4. # put $value...
  5. # ```
  6. #
  7. # Takes arbitrary arguments and write them to the structured stdout.
  8. #
  9. # Examples:
  10. #
  11. # ```elvish-transcript
  12. # ~> put a
  13. # ▶ a
  14. # ~> put lorem ipsum [a b] { ls }
  15. # ▶ lorem
  16. # ▶ ipsum
  17. # ▶ [a b]
  18. # ▶ <closure 0xc4202607e0>
  19. # ```
  20. #
  21. # **Note**: It is almost never necessary to use `put (...)` - just write the
  22. # `...` part. For example, `put (eq a b)` is the equivalent to just `eq a b`.
  23. #
  24. # Etymology: Various languages, in particular
  25. # [C](https://manpages.debian.org/stretch/manpages-dev/puts.3.en.html) and
  26. # [Ruby](https://ruby-doc.org/core-2.2.2/IO.html#method-i-puts) as `puts`.
  27. #elvdoc:fn repeat
  28. #
  29. # ```elvish
  30. # repeat $n $value
  31. # ```
  32. #
  33. # Output `$value` for `$n` times. Example:
  34. #
  35. # ```elvish-transcript
  36. # ~> repeat 0 lorem
  37. # ~> repeat 4 NAN
  38. # ▶ NAN
  39. # ▶ NAN
  40. # ▶ NAN
  41. # ▶ NAN
  42. # ```
  43. #
  44. # Etymology: [Clojure](https://clojuredocs.org/clojure.core/repeat).
  45. #elvdoc:fn read-upto
  46. #
  47. # ```elvish
  48. # read-upto $terminator
  49. # ```
  50. #
  51. # Reads byte input until `$terminator` or end-of-file is encountered. It outputs the part of the
  52. # input read as a string value. The output contains the trailing `$terminator`, unless `read-upto`
  53. # terminated at end-of-file.
  54. #
  55. # The `$terminator` must be a single ASCII character such as `"\x00"` (NUL).
  56. #
  57. # Examples:
  58. #
  59. # ```elvish-transcript
  60. # ~> echo "a,b,c" | read-upto ","
  61. # ▶ 'a,'
  62. # ~> echo "foo\nbar" | read-upto "\n"
  63. # ▶ "foo\n"
  64. # ~> echo "a.elv\x00b.elv" | read-upto "\x00"
  65. # ▶ "a.elv\x00"
  66. # ~> print "foobar" | read-upto "\n"
  67. # ▶ foobar
  68. # ```
  69. #elvdoc:fn read-line
  70. #
  71. # ```elvish
  72. # read-line
  73. # ```
  74. #
  75. # Reads a single line from byte input, and writes the line to the value output,
  76. # stripping the line ending. A line can end with `"\r\n"`, `"\n"`, or end of
  77. # file. Examples:
  78. #
  79. # ```elvish-transcript
  80. # ~> print line | read-line
  81. # ▶ line
  82. # ~> print "line\n" | read-line
  83. # ▶ line
  84. # ~> print "line\r\n" | read-line
  85. # ▶ line
  86. # ~> print "line-with-extra-cr\r\r\n" | read-line
  87. # ▶ "line-with-extra-cr\r"
  88. # ```
  89. #elvdoc:fn print
  90. #
  91. # ```elvish
  92. # print &sep=' ' $value...
  93. # ```
  94. #
  95. # Like `echo`, just without the newline.
  96. #
  97. # @cf echo
  98. #
  99. # Etymology: Various languages, in particular
  100. # [Perl](https://perldoc.perl.org/functions/print.html) and
  101. # [zsh](http://zsh.sourceforge.net/Doc/Release/Shell-Builtin-Commands.html), whose
  102. # `print`s do not print a trailing newline.
  103. #elvdoc:fn printf
  104. #
  105. # ```elvish
  106. # printf $template $value...
  107. # ```
  108. #
  109. # Prints values to the byte stream according to a template. If you need to inject the output into
  110. # the value stream use this pattern: `printf .... | slurp`. That ensures that any newlines in the
  111. # output of `printf` do not cause its output to be broken into multiple values, thus eliminating
  112. # the newlines, which will occur if you do `put (printf ....)`.
  113. #
  114. # Like [`print`](#print), this command does not add an implicit newline; include an explicit `"\n"`
  115. # in the formatting template instead. For example, `printf "%.1f\n" (/ 10.0 3)`.
  116. #
  117. # See Go's [`fmt`](https://golang.org/pkg/fmt/#hdr-Printing) package for
  118. # details about the formatting verbs and the various flags that modify the
  119. # default behavior, such as padding and justification.
  120. #
  121. # Unlike Go, each formatting verb has a single associated internal type, and
  122. # accepts any argument that can reasonably be converted to that type:
  123. #
  124. # - The verbs `%s`, `%q` and `%v` convert the corresponding argument to a
  125. # string in different ways:
  126. #
  127. # - `%s` uses [to-string](#to-string) to convert a value to string.
  128. #
  129. # - `%q` uses [repr](#repr) to convert a value to string.
  130. #
  131. # - `%v` is equivalent to `%s`, and `%#v` is equivalent to `%q`.
  132. #
  133. # - The verb `%t` first convert the corresponding argument to a boolean using
  134. # [bool](#bool), and then uses its Go counterpart to format the boolean.
  135. #
  136. # - The verbs `%b`, `%c`, `%d`, `%o`, `%O`, `%x`, `%X` and `%U` first convert
  137. # the corresponding argument to an integer using an internal algorithm, and
  138. # use their Go counterparts to format the integer.
  139. #
  140. # - The verbs `%e`, `%E`, `%f`, `%F`, `%g` and `%G` first convert the
  141. # corresponding argument to a floating-point number using
  142. # [float64](#float64), and then use their Go counterparts to format the
  143. # number.
  144. #
  145. # The special verb `%%` prints a literal `%` and consumes no argument.
  146. #
  147. # Verbs not documented above are not supported.
  148. #
  149. # Examples:
  150. #
  151. # ```elvish-transcript
  152. # ~> printf "%10s %.2f\n" Pi $math:pi
  153. # Pi 3.14
  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. # **Note**: Compared to the [POSIX `printf`
  165. # command](https://pubs.opengroup.org/onlinepubs/007908799/xcu/printf.html)
  166. # found in other shells, there are 3 key differences:
  167. #
  168. # - The behavior of the formatting verbs are based on Go's
  169. # [`fmt`](https://golang.org/pkg/fmt/) package instead of the POSIX
  170. # specification.
  171. #
  172. # - The number of arguments after the formatting template must match the number
  173. # of formatting verbs. The POSIX command will repeat the template string to
  174. # consume excess values; this command does not have that behavior.
  175. #
  176. # - This command does not interpret escape sequences such as `\n`; just use
  177. # [double-quoted strings](language.html#double-quoted-string).
  178. #
  179. # @cf print echo pprint repr
  180. #elvdoc:fn echo
  181. #
  182. # ```elvish
  183. # echo &sep=' ' $value...
  184. # ```
  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. # @cf print
  204. #
  205. # Etymology: Bourne sh.
  206. #elvdoc:fn pprint
  207. #
  208. # ```elvish
  209. # pprint $value...
  210. # ```
  211. #
  212. # Pretty-print representations of Elvish values. Examples:
  213. #
  214. # ```elvish-transcript
  215. # ~> pprint [foo bar]
  216. # [
  217. # foo
  218. # bar
  219. # ]
  220. # ~> pprint [&k1=v1 &k2=v2]
  221. # [
  222. # &k2=
  223. # v2
  224. # &k1=
  225. # v1
  226. # ]
  227. # ```
  228. #
  229. # The output format is subject to change.
  230. #
  231. # @cf repr
  232. #elvdoc:fn repr
  233. #
  234. # ```elvish
  235. # repr $value...
  236. # ```
  237. #
  238. # Writes representation of `$value`s, separated by space and followed by a
  239. # newline. Example:
  240. #
  241. # ```elvish-transcript
  242. # ~> repr [foo 'lorem ipsum'] "aha\n"
  243. # [foo 'lorem ipsum'] "aha\n"
  244. # ```
  245. #
  246. # @cf pprint
  247. #
  248. # Etymology: [Python](https://docs.python.org/3/library/functions.html#repr).
  249. #elvdoc:fn show
  250. #
  251. # ```elvish
  252. # show $e
  253. # ```
  254. #
  255. # Shows the value to the output, which is assumed to be a VT-100-compatible
  256. # terminal.
  257. #
  258. # Currently, the only type of value that can be showed is exceptions, but this
  259. # will likely expand in future.
  260. #
  261. # Example:
  262. #
  263. # ```elvish-transcript
  264. # ~> var e = ?(fail lorem-ipsum)
  265. # ~> show $e
  266. # Exception: lorem-ipsum
  267. # [tty 3], line 1: var e = ?(fail lorem-ipsum)
  268. # ```
  269. #elvdoc:fn only-bytes
  270. #
  271. # ```elvish
  272. # only-bytes
  273. # ```
  274. #
  275. # Passes byte input to output, and discards value inputs.
  276. #
  277. # Example:
  278. #
  279. # ```elvish-transcript
  280. # ~> { put value; echo bytes } | only-bytes
  281. # bytes
  282. # ```
  283. #elvdoc:fn only-values
  284. #
  285. # ```elvish
  286. # only-values
  287. # ```
  288. #
  289. # Passes value input to output, and discards byte inputs.
  290. #
  291. # Example:
  292. #
  293. # ```elvish-transcript
  294. # ~> { put value; echo bytes } | only-values
  295. # ▶ value
  296. # ```
  297. #elvdoc:fn slurp
  298. #
  299. # ```elvish
  300. # slurp
  301. # ```
  302. #
  303. # Reads bytes input into a single string, and put this string on structured
  304. # stdout.
  305. #
  306. # Example:
  307. #
  308. # ```elvish-transcript
  309. # ~> echo "a\nb" | slurp
  310. # ▶ "a\nb\n"
  311. # ```
  312. #
  313. # Etymology: Perl, as
  314. # [`File::Slurp`](http://search.cpan.org/~uri/File-Slurp-9999.19/lib/File/Slurp.pm).
  315. #elvdoc:fn from-lines
  316. #
  317. # ```elvish
  318. # from-lines
  319. # ```
  320. #
  321. # Splits byte input into lines, and writes them to the value output. Value
  322. # input is ignored.
  323. #
  324. # ```elvish-transcript
  325. # ~> { echo a; echo b } | from-lines
  326. # ▶ a
  327. # ▶ b
  328. # ~> { echo a; put b } | from-lines
  329. # ▶ a
  330. # ```
  331. #
  332. # @cf from-terminated read-upto to-lines
  333. #elvdoc:fn from-json
  334. #
  335. # ```elvish
  336. # from-json
  337. # ```
  338. #
  339. # Takes bytes stdin, parses it as JSON and puts the result on structured stdout.
  340. # The input can contain multiple JSONs, and whitespace between them are ignored.
  341. #
  342. # Note that JSON's only number type corresponds to Elvish's floating-point
  343. # number type, and is always considered [inexact](language.html#exactness).
  344. # It may be necessary to coerce JSON numbers to exact numbers using
  345. # [exact-num](#exact-num).
  346. #
  347. # Examples:
  348. #
  349. # ```elvish-transcript
  350. # ~> echo '"a"' | from-json
  351. # ▶ a
  352. # ~> echo '["lorem", "ipsum"]' | from-json
  353. # ▶ [lorem ipsum]
  354. # ~> echo '{"lorem": "ipsum"}' | from-json
  355. # ▶ [&lorem=ipsum]
  356. # ~> # multiple JSONs running together
  357. # echo '"a""b"["x"]' | from-json
  358. # ▶ a
  359. # ▶ b
  360. # ▶ [x]
  361. # ~> # multiple JSONs separated by newlines
  362. # echo '"a"
  363. # {"k": "v"}' | from-json
  364. # ▶ a
  365. # ▶ [&k=v]
  366. # ```
  367. #
  368. # @cf to-json
  369. #elvdoc:fn from-terminated
  370. #
  371. # ```elvish
  372. # from-terminated $terminator
  373. # ```
  374. #
  375. # Splits byte input into lines at each `$terminator` character, and writes
  376. # them to the value output. If the byte input ends with `$terminator`, it is
  377. # dropped. Value input is ignored.
  378. #
  379. # The `$terminator` must be a single ASCII character such as `"\x00"` (NUL).
  380. #
  381. # ```elvish-transcript
  382. # ~> { echo a; echo b } | from-terminated "\x00"
  383. # ▶ "a\nb\n"
  384. # ~> print "a\x00b" | from-terminated "\x00"
  385. # ▶ a
  386. # ▶ b
  387. # ~> print "a\x00b\x00" | from-terminated "\x00"
  388. # ▶ a
  389. # ▶ b
  390. # ```
  391. #
  392. # @cf from-lines read-upto to-terminated
  393. #elvdoc:fn to-lines
  394. #
  395. # ```elvish
  396. # to-lines $inputs?
  397. # ```
  398. #
  399. # Writes each [value input](#value-inputs) to a separate line in the byte
  400. # output. Byte input is ignored.
  401. #
  402. # ```elvish-transcript
  403. # ~> put a b | to-lines
  404. # a
  405. # b
  406. # ~> to-lines [a b]
  407. # a
  408. # b
  409. # ~> { put a; echo b } | to-lines
  410. # b
  411. # a
  412. # ```
  413. #
  414. # @cf from-lines to-terminated
  415. #elvdoc:fn to-terminated
  416. #
  417. # ```elvish
  418. # to-terminated $terminator $inputs?
  419. # ```
  420. #
  421. # Writes each [value input](#value-inputs) to the byte output with the
  422. # specified terminator character. Byte input is ignored. This behavior is
  423. # useful, for example, when feeding output into a program that accepts NUL
  424. # terminated lines to avoid ambiguities if the values contains newline
  425. # characters.
  426. #
  427. # The `$terminator` must be a single ASCII character such as `"\x00"` (NUL).
  428. #
  429. # ```elvish-transcript
  430. # ~> put a b | to-terminated "\x00" | slurp
  431. # ▶ "a\x00b\x00"
  432. # ~> to-terminated "\x00" [a b] | slurp
  433. # ▶ "a\x00b\x00"
  434. # ```
  435. #
  436. # @cf from-terminated to-lines
  437. #elvdoc:fn to-json
  438. #
  439. # ```elvish
  440. # to-json
  441. # ```
  442. #
  443. # Takes structured stdin, convert it to JSON and puts the result on bytes stdout.
  444. #
  445. # ```elvish-transcript
  446. # ~> put a | to-json
  447. # "a"
  448. # ~> put [lorem ipsum] | to-json
  449. # ["lorem","ipsum"]
  450. # ~> put [&lorem=ipsum] | to-json
  451. # {"lorem":"ipsum"}
  452. # ```
  453. #
  454. # @cf from-json