file.d.elv 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. #elvdoc:fn is-tty
  2. #
  3. # ```elvish
  4. # file:is-tty $file
  5. # ```
  6. #
  7. # Outputs whether `$file` is a terminal device.
  8. #
  9. # The `$file` can be a file object or a number. If it's a number, it's
  10. # interpreted as a numerical file descriptor.
  11. #
  12. # ```elvish-transcript
  13. # ~> var f = (file:open /dev/tty)
  14. # ~> file:is-tty $f
  15. # ▶ $true
  16. # ~> file:close $f
  17. # ~> var f = (file:open /dev/null)
  18. # ~> file:is-tty $f
  19. # ▶ $false
  20. # ~> file:close $f
  21. # ~> var p = (file:pipe)
  22. # ~> file:is-tty $p[r]
  23. # ▶ $false
  24. # ~> file:is-tty $p[w]
  25. # ▶ $false
  26. # ~> file:close $p[r]
  27. # ~> file:close $p[w]
  28. # ~> file:is-tty 0
  29. # ▶ $true
  30. # ~> file:is-tty 1
  31. # ▶ $true
  32. # ~> file:is-tty 2
  33. # ▶ $true
  34. # ```
  35. #elvdoc:fn open
  36. #
  37. # ```elvish
  38. # file:open $filename
  39. # ```
  40. #
  41. # Opens a file. Currently, `open` only supports opening a file for reading.
  42. # File must be closed with `close` explicitly. Example:
  43. #
  44. # ```elvish-transcript
  45. # ~> cat a.txt
  46. # This is
  47. # a file.
  48. # ~> use file
  49. # ~> var f = (file:open a.txt)
  50. # ~> cat < $f
  51. # This is
  52. # a file.
  53. # ~> close $f
  54. # ```
  55. #
  56. # @cf file:close
  57. #elvdoc:fn close
  58. #
  59. # ```elvish
  60. # file:close $file
  61. # ```
  62. #
  63. # Closes a file opened with `open`.
  64. #
  65. # @cf file:open
  66. #elvdoc:fn pipe
  67. #
  68. # ```elvish
  69. # file:pipe
  70. # ```
  71. #
  72. # Create a new pipe that can be used in redirections. A pipe contains a read-end and write-end.
  73. # Each pipe object is a [pseudo-map](language.html#pseudo-map) with fields `r` (the read-end [file
  74. # object](./language.html#file)) and `w` (the write-end).
  75. #
  76. # When redirecting command input from a pipe with `<`, the read-end is used. When redirecting
  77. # command output to a pipe with `>`, the write-end is used. Redirecting both input and output with
  78. # `<>` to a pipe is not supported.
  79. #
  80. # Pipes have an OS-dependent buffer, so writing to a pipe without an active reader
  81. # does not necessarily block. Pipes **must** be explicitly closed with `file:close`.
  82. #
  83. # Putting values into pipes will cause those values to be discarded.
  84. #
  85. # Examples (assuming the pipe has a large enough buffer):
  86. #
  87. # ```elvish-transcript
  88. # ~> var p = (file:pipe)
  89. # ~> echo 'lorem ipsum' > $p
  90. # ~> head -n1 < $p
  91. # lorem ipsum
  92. # ~> put 'lorem ipsum' > $p
  93. # ~> file:close $p[w] # close the write-end
  94. # ~> head -n1 < $p # blocks unless the write-end is closed
  95. # ~> file:close $p[r] # close the read-end
  96. # ```
  97. #
  98. # @cf file:close
  99. #elvdoc:fn truncate
  100. #
  101. # ```elvish
  102. # file:truncate $filename $size
  103. # ```
  104. #
  105. # changes the size of the named file. If the file is a symbolic link, it
  106. # changes the size of the link's target. The size must be an integer between 0
  107. # and 2^64-1.