版博士V2.0程序
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

readme.md 7.6 KiB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. # ora
  2. > Elegant terminal spinner
  3. <p align="center">
  4. <br>
  5. <img src="screenshot.svg" width="500">
  6. <br>
  7. </p>
  8. ## Install
  9. ```sh
  10. npm install ora
  11. ```
  12. ## Usage
  13. ```js
  14. import ora from 'ora';
  15. const spinner = ora('Loading unicorns').start();
  16. setTimeout(() => {
  17. spinner.color = 'yellow';
  18. spinner.text = 'Loading rainbows';
  19. }, 1000);
  20. ```
  21. ## API
  22. ### ora(text)
  23. ### ora(options)
  24. If a string is provided, it is treated as a shortcut for [`options.text`](#text).
  25. #### options
  26. Type: `object`
  27. ##### text
  28. Type: `string`
  29. Text to display after the spinner.
  30. ##### prefixText
  31. Type: `string | () => string`
  32. Text or a function that returns text to display before the spinner. No prefix text will be displayed if set to an empty string.
  33. ##### suffixText
  34. Type: `string | () => string`
  35. Text or a function that returns text to display after the spinner text. No suffix text will be displayed if set to an empty string.
  36. ##### spinner
  37. Type: `string | object`\
  38. Default: `'dots'` <img src="screenshot-spinner.gif" width="14">
  39. Name of one of the [provided spinners](#spinners). See `example.js` in this repo if you want to test out different spinners. On Windows, it will always use the `line` spinner as the Windows command-line doesn't have proper Unicode support.
  40. Or an object like:
  41. ```js
  42. {
  43. interval: 80, // Optional
  44. frames: ['-', '+', '-']
  45. }
  46. ```
  47. ##### color
  48. Type: `string`\
  49. Default: `'cyan'`\
  50. Values: `'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white' | 'gray'`
  51. The color of the spinner.
  52. ##### hideCursor
  53. Type: `boolean`\
  54. Default: `true`
  55. Set to `false` to stop Ora from hiding the cursor.
  56. ##### indent
  57. Type: `number`\
  58. Default: `0`
  59. Indent the spinner with the given number of spaces.
  60. ##### interval
  61. Type: `number`\
  62. Default: Provided by the spinner or `100`
  63. Interval between each frame.
  64. Spinners provide their own recommended interval, so you don't really need to specify this.
  65. ##### stream
  66. Type: `stream.Writable`\
  67. Default: `process.stderr`
  68. Stream to write the output.
  69. You could for example set this to `process.stdout` instead.
  70. ##### isEnabled
  71. Type: `boolean`
  72. Force enable/disable the spinner. If not specified, the spinner will be enabled if the `stream` is being run inside a TTY context (not spawned or piped) and/or not in a CI environment.
  73. Note that `{isEnabled: false}` doesn't mean it won't output anything. It just means it won't output the spinner, colors, and other ansi escape codes. It will still log text.
  74. ##### isSilent
  75. Type: `boolean`\
  76. Default: `false`
  77. Disable the spinner and all log text. All output is suppressed and `isEnabled` will be considered `false`.
  78. ##### discardStdin
  79. Type: `boolean`\
  80. Default: `true`
  81. Discard stdin input (except Ctrl+C) while running if it's TTY. This prevents the spinner from twitching on input, outputting broken lines on <kbd>Enter</kbd> key presses, and prevents buffering of input while the spinner is running.
  82. This has no effect on Windows as there is no good way to implement discarding stdin properly there.
  83. ### Instance
  84. #### .text <sup>get/set</sup>
  85. Change the text after the spinner.
  86. #### .prefixText <sup>get/set</sup>
  87. Change the text before the spinner.
  88. No prefix text will be displayed if set to an empty string.
  89. #### .suffixText <sup>get/set</sup>
  90. Change the text after the spinner text.
  91. No suffix text will be displayed if set to an empty string.
  92. #### .color <sup>get/set</sup>
  93. Change the spinner color.
  94. #### .spinner <sup>get/set</sup>
  95. Change the spinner.
  96. #### .indent <sup>get/set</sup>
  97. Change the spinner indent.
  98. #### .isSpinning <sup>get</sup>
  99. A boolean of whether the instance is currently spinning.
  100. #### .interval <sup>get</sup>
  101. The interval between each frame.
  102. The interval is decided by the chosen spinner.
  103. #### .start(text?)
  104. Start the spinner. Returns the instance. Set the current text if `text` is provided.
  105. #### .stop()
  106. Stop and clear the spinner. Returns the instance.
  107. #### .succeed(text?)
  108. Stop the spinner, change it to a green `✔` and persist the current text, or `text` if provided. Returns the instance. See the GIF below.
  109. #### .fail(text?)
  110. Stop the spinner, change it to a red `✖` and persist the current text, or `text` if provided. Returns the instance. See the GIF below.
  111. #### .warn(text?)
  112. Stop the spinner, change it to a yellow `⚠` and persist the current text, or `text` if provided. Returns the instance.
  113. #### .info(text?)
  114. Stop the spinner, change it to a blue `ℹ` and persist the current text, or `text` if provided. Returns the instance.
  115. #### .stopAndPersist(options?)
  116. Stop the spinner and change the symbol or text. Returns the instance. See the GIF below.
  117. ##### options
  118. Type: `object`
  119. ###### symbol
  120. Type: `string`\
  121. Default: `' '`
  122. Symbol to replace the spinner with.
  123. ###### text
  124. Type: `string`\
  125. Default: Current `'text'`
  126. Text to be persisted after the symbol.
  127. ###### prefixText
  128. Type: `string`\
  129. Default: Current `prefixText`
  130. Text to be persisted before the symbol. No prefix text will be displayed if set to an empty string.
  131. ###### suffixText
  132. Type: `string`\
  133. Default: Current `suffixText`
  134. Text to be persisted after the text after the symbol. No suffix text will be displayed if set to an empty string.
  135. <img src="screenshot-2.gif" width="480">
  136. #### .clear()
  137. Clear the spinner. Returns the instance.
  138. #### .render()
  139. Manually render a new frame. Returns the instance.
  140. #### .frame()
  141. Get a new frame.
  142. ### oraPromise(action, text)
  143. ### oraPromise(action, options)
  144. Starts a spinner for a promise or promise-returning function. The spinner is stopped with `.succeed()` if the promise fulfills or with `.fail()` if it rejects. Returns the promise.
  145. ```js
  146. import {oraPromise} from 'ora';
  147. await oraPromise(somePromise);
  148. ```
  149. #### action
  150. Type: `Promise | ((spinner: ora.Ora) => Promise)`
  151. #### options
  152. Type: `object`
  153. All of the [options](#options) plus the following:
  154. ##### successText
  155. Type: `string | ((result: T) => string) | undefined`
  156. The new text of the spinner when the promise is resolved.
  157. Keeps the existing text if `undefined`.
  158. ##### failText
  159. Type: `string | ((error: Error) => string) | undefined`
  160. The new text of the spinner when the promise is rejected.
  161. Keeps the existing text if `undefined`.
  162. ### spinners
  163. Type: `Record<string, Spinner>`
  164. All [provided spinners](https://github.com/sindresorhus/cli-spinners/blob/main/spinners.json).
  165. ## FAQ
  166. ### How do I change the color of the text?
  167. Use [Chalk](https://github.com/chalk/chalk):
  168. ```js
  169. import ora from 'ora';
  170. import chalk from 'chalk';
  171. const spinner = ora(`Loading ${chalk.red('unicorns')}`).start();
  172. ```
  173. ### Why does the spinner freeze?
  174. JavaScript is single-threaded, so synchronous operations blocks the thread, including the spinner animation. Prefer asynchronous operations whenever possible.
  175. ## Related
  176. - [cli-spinners](https://github.com/sindresorhus/cli-spinners) - Spinners for use in the terminal
  177. - [listr](https://github.com/SamVerschueren/listr) - Terminal task list
  178. - [CLISpinner](https://github.com/kiliankoe/CLISpinner) - Terminal spinner library for Swift
  179. - [halo](https://github.com/ManrajGrover/halo) - Python port
  180. - [spinners](https://github.com/FGRibreau/spinners) - Terminal spinners for Rust
  181. - [marquee-ora](https://github.com/joeycozza/marquee-ora) - Scrolling marquee spinner for Ora
  182. - [briandowns/spinner](https://github.com/briandowns/spinner) - Terminal spinner/progress indicator for Go
  183. - [tj/go-spin](https://github.com/tj/go-spin) - Terminal spinner package for Go
  184. - [observablehq.com/@victordidenko/ora](https://observablehq.com/@victordidenko/ora) - Ora port to Observable notebooks
  185. - [spinnies](https://github.com/jcarpanelli/spinnies) - Terminal multi-spinner library for Node.js
  186. - [kia](https://github.com/HarryPeach/kia) - Simple terminal spinners for Deno 🦕