Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/goto-bus-stop/ansi-scrollbox
a basic scrollable area for terminal apps
https://github.com/goto-bus-stop/ansi-scrollbox
Last synced: 27 days ago
JSON representation
a basic scrollable area for terminal apps
- Host: GitHub
- URL: https://github.com/goto-bus-stop/ansi-scrollbox
- Owner: goto-bus-stop
- License: other
- Created: 2018-04-18T12:13:14.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-01-24T04:03:07.000Z (almost 2 years ago)
- Last Synced: 2024-10-05T16:25:42.643Z (about 1 month ago)
- Language: JavaScript
- Size: 21.5 KB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.md
Awesome Lists containing this project
README
# ansi-scrollbox
a basic scrollable area for terminal apps
[![npm][npm-image]][npm-url]
[![travis][travis-image]][travis-url]
[![standard][standard-image]][standard-url][npm-image]: https://img.shields.io/npm/v/ansi-scrollbox.svg?style=flat-square
[npm-url]: https://www.npmjs.com/package/ansi-scrollbox
[travis-image]: https://img.shields.io/travis/goto-bus-stop/ansi-scrollbox.svg?style=flat-square
[travis-url]: https://travis-ci.org/goto-bus-stop/ansi-scrollbox
[standard-image]: https://img.shields.io/badge/code%20style-standard-brightgreen.svg?style=flat-square
[standard-url]: http://npm.im/standard## Install
```
npm install ansi-scrollbox
```## Usage
```js
var scrollbox = require('ansi-scrollbox')
var differ = require('ansi-diff')()
var lorem = require('@jamen/lorem')var box = scrollbox({
width: 20,
height: 20
})box.setContent(lorem(6000))
function render () {
process.stdout.write(differ.update(box.toString()))
}
setInterval(render, 100)require('keypress')(process.stdin)
process.stdin.setRawMode(true)
process.stdin.resume()
// Optionally add default scroll keybindings (things like up, down, home, end)
process.stdin.on('keypress', box.keypress)
```## API
### `box = scrollbox(opts={})`
Prepare a new scrollbox.
- `opts.width` - Width in columns of the scrollbox.
- `opts.height` - Height in rows of the scrollbox.### `box.setContent(content)`, `box.content = content`
Set the content of the scrollbox.
### `box.content`
Get the content of the scrollbox.
### `box.scroll(offset)`
Set the scroll position. `offset` is the number of rows from the start of the content. When `offset` is negative, it's the number of rows from the end of the content. Use -1 to scroll to the very end; -1 is "sticky" so it will stay at the end when the content updates.
### `box.resize(opts)`
Update the size of the scrollbox.
- `opts.width` - Width in columns of the scrollbox.
- `opts.height` - Height in rows of the scrollbox.### `box.toString()`
Get the visible contents of the box. Use with something like [ansi-diff](https://github.com/mafintosh/ansi-diff) for efficient updates on screene.
### `unsub = box.subscribe(listener)`
Call `listener` whenever the box's content, size, or scroll position change. Do `unsub()` to stop listening.
### `box.keypress`
An event listener that implements basic keyboard controls:
- `up` or `k` to scroll 1 row up
- `down` or `j` to scroll 1 row down
- `home` to scroll to the start
- `end` to scroll to the startUse it with the [keypress](https://github.com/TooTallNate/keypress) module:
```js
require('keypress')(process.stdin)
process.stdin.on('keypress', box.keypress)
```## License
[Apache-2.0](LICENSE.md)