You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

264 lines
6.3 KiB

3 months ago
  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. ```
  10. $ npm install ora
  11. ```
  12. ## Usage
  13. ```js
  14. const ora = require('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. ##### spinner
  34. Type: `string | object`\
  35. Default: `'dots'` <img src="screenshot-spinner.gif" width="14">
  36. Name of one of the [provided spinners](https://github.com/sindresorhus/cli-spinners/blob/main/spinners.json). 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.
  37. Or an object like:
  38. ```js
  39. {
  40. interval: 80, // Optional
  41. frames: ['-', '+', '-']
  42. }
  43. ```
  44. ##### color
  45. Type: `string`\
  46. Default: `'cyan'`\
  47. Values: `'black' | 'red' | 'green' | 'yellow' | 'blue' | 'magenta' | 'cyan' | 'white' | 'gray'`
  48. Color of the spinner.
  49. ##### hideCursor
  50. Type: `boolean`\
  51. Default: `true`
  52. Set to `false` to stop Ora from hiding the cursor.
  53. ##### indent
  54. Type: `number`\
  55. Default: `0`
  56. Indent the spinner with the given number of spaces.
  57. ##### interval
  58. Type: `number`\
  59. Default: Provided by the spinner or `100`
  60. Interval between each frame.
  61. Spinners provide their own recommended interval, so you don't really need to specify this.
  62. ##### stream
  63. Type: `stream.Writable`\
  64. Default: `process.stderr`
  65. Stream to write the output.
  66. You could for example set this to `process.stdout` instead.
  67. ##### isEnabled
  68. Type: `boolean`
  69. 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.
  70. 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.
  71. ##### isSilent
  72. Type: `boolean`\
  73. Default: `false`
  74. Disable the spinner and all log text. All output is suppressed and `isEnabled` will be considered `false`.
  75. ##### discardStdin
  76. Type: `boolean`\
  77. Default: `true`
  78. 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.
  79. This has no effect on Windows as there's no good way to implement discarding stdin properly there.
  80. ### Instance
  81. #### .start(text?)
  82. Start the spinner. Returns the instance. Set the current text if `text` is provided.
  83. #### .stop()
  84. Stop and clear the spinner. Returns the instance.
  85. #### .succeed(text?)
  86. Stop the spinner, change it to a green `✔` and persist the current text, or `text` if provided. Returns the instance. See the GIF below.
  87. #### .fail(text?)
  88. Stop the spinner, change it to a red `✖` and persist the current text, or `text` if provided. Returns the instance. See the GIF below.
  89. #### .warn(text?)
  90. Stop the spinner, change it to a yellow `⚠` and persist the current text, or `text` if provided. Returns the instance.
  91. #### .info(text?)
  92. Stop the spinner, change it to a blue `ℹ` and persist the current text, or `text` if provided. Returns the instance.
  93. #### .isSpinning
  94. A boolean of whether the instance is currently spinning.
  95. #### .stopAndPersist(options?)
  96. Stop the spinner and change the symbol or text. Returns the instance. See the GIF below.
  97. ##### options
  98. Type: `object`
  99. ###### symbol
  100. Type: `string`\
  101. Default: `' '`
  102. Symbol to replace the spinner with.
  103. ###### text
  104. Type: `string`\
  105. Default: Current `'text'`
  106. Text to be persisted after the symbol
  107. ###### prefixText
  108. Type: `string`\
  109. Default: Current `prefixText`
  110. Text to be persisted before the symbol. No prefix text will be displayed if set to an empty string.
  111. <img src="screenshot-2.gif" width="480">
  112. #### .clear()
  113. Clear the spinner. Returns the instance.
  114. #### .render()
  115. Manually render a new frame. Returns the instance.
  116. #### .frame()
  117. Get a new frame.
  118. #### .text
  119. Change the text after the spinner.
  120. #### .prefixText
  121. Change the text before the spinner. No prefix text will be displayed if set to an empty string.
  122. #### .color
  123. Change the spinner color.
  124. #### .spinner
  125. Change the spinner.
  126. #### .indent
  127. Change the spinner indent.
  128. ### ora.promise(action, text)
  129. ### ora.promise(action, options)
  130. Starts a spinner for a promise. The spinner is stopped with `.succeed()` if the promise fulfills or with `.fail()` if it rejects. Returns the spinner instance.
  131. #### action
  132. Type: `Promise`
  133. ## FAQ
  134. ### How do I change the color of the text?
  135. Use [Chalk](https://github.com/chalk/chalk):
  136. ```js
  137. const ora = require('ora');
  138. const chalk = require('chalk');
  139. const spinner = ora(`Loading ${chalk.red('unicorns')}`).start();
  140. ```
  141. ### Why does the spinner freeze?
  142. JavaScript is single-threaded, so synchronous operations blocks the thread, including the spinner animation. Prefer asynchronous operations whenever possible.
  143. ## Related
  144. - [cli-spinners](https://github.com/sindresorhus/cli-spinners) - Spinners for use in the terminal
  145. - [listr](https://github.com/SamVerschueren/listr) - Terminal task list
  146. - [CLISpinner](https://github.com/kiliankoe/CLISpinner) - Terminal spinner library for Swift
  147. - [halo](https://github.com/ManrajGrover/halo) - Python port
  148. - [spinners](https://github.com/FGRibreau/spinners) - Terminal spinners for Rust
  149. - [marquee-ora](https://github.com/joeycozza/marquee-ora) - Scrolling marquee spinner for Ora
  150. - [briandowns/spinner](https://github.com/briandowns/spinner) - Terminal spinner/progress indicator for Go
  151. - [tj/go-spin](https://github.com/tj/go-spin) - Terminal spinner package for Go
  152. - [observablehq.com/@victordidenko/ora](https://observablehq.com/@victordidenko/ora) - Ora port to Observable notebooks
  153. - [spinnies](https://github.com/jcarpanelli/spinnies) - Terminal multi-spinner library for Node.js
  154. - [kia](https://github.com/HarryPeach/kia) - Simple terminal spinners for Deno 🦕