mirror of
https://github.com/balena-io/balena-cli.git
synced 2025-02-20 17:33:18 +00:00
Convert lib/utils/tty to typescript
Change-type: patch
This commit is contained in:
parent
fe0ad92b43
commit
9fda165d34
@ -1,66 +0,0 @@
|
||||
|
||||
windowSize = {}
|
||||
|
||||
updateWindowSize = ->
|
||||
size = require('window-size')?.get()
|
||||
windowSize.width = size?.width
|
||||
windowSize.height = size?.height
|
||||
|
||||
process.stdout.on('resize', updateWindowSize)
|
||||
|
||||
module.exports = (stream = process.stdout) ->
|
||||
# make sure we get initial metrics
|
||||
updateWindowSize()
|
||||
|
||||
currentWindowSize = ->
|
||||
# always return a copy.
|
||||
# width/height can be undefined if no TTY.
|
||||
width: windowSize.width
|
||||
height: windowSize.height
|
||||
|
||||
hideCursor = ->
|
||||
stream.write('\u001B[?25l')
|
||||
|
||||
showCursor = ->
|
||||
stream.write('\u001B[?25h')
|
||||
|
||||
cursorUp = (rows = 0) ->
|
||||
stream.write("\u001B[#{rows}A")
|
||||
|
||||
cursorDown = (rows = 0) ->
|
||||
stream.write("\u001B[#{rows}B")
|
||||
|
||||
cursorHidden = ->
|
||||
Promise = require('bluebird')
|
||||
Promise.try(hideCursor).disposer(showCursor)
|
||||
|
||||
write = (str) ->
|
||||
stream.write(str)
|
||||
|
||||
writeLine = (str) ->
|
||||
stream.write("#{str}\n")
|
||||
|
||||
clearLine = ->
|
||||
stream.write('\u001B[2K\r')
|
||||
|
||||
replaceLine = (str) ->
|
||||
clearLine()
|
||||
write(str)
|
||||
|
||||
deleteToEnd = ->
|
||||
stream.write('\u001b[0J')
|
||||
|
||||
return {
|
||||
stream
|
||||
currentWindowSize
|
||||
hideCursor
|
||||
showCursor
|
||||
cursorHidden
|
||||
cursorUp
|
||||
cursorDown
|
||||
write
|
||||
writeLine
|
||||
clearLine
|
||||
replaceLine
|
||||
deleteToEnd
|
||||
}
|
68
lib/utils/tty.ts
Normal file
68
lib/utils/tty.ts
Normal file
@ -0,0 +1,68 @@
|
||||
import * as Bluebird from 'bluebird';
|
||||
|
||||
const windowSize: { width?: number; height?: number } = {};
|
||||
|
||||
const updateWindowSize = () => {
|
||||
const size = require('window-size')?.get();
|
||||
windowSize.width = size?.width;
|
||||
windowSize.height = size?.height;
|
||||
};
|
||||
|
||||
process.stdout.on('resize', updateWindowSize);
|
||||
|
||||
export = (stream: NodeJS.WriteStream = process.stdout) => {
|
||||
// make sure we get initial metrics
|
||||
updateWindowSize();
|
||||
|
||||
const currentWindowSize = () => {
|
||||
// always return a copy.
|
||||
// width/height can be undefined if no TTY.
|
||||
return {
|
||||
width: windowSize.width,
|
||||
height: windowSize.height,
|
||||
};
|
||||
};
|
||||
|
||||
const hideCursor = () => stream.write('\u001B[?25l');
|
||||
|
||||
const showCursor = () => stream.write('\u001B[?25h');
|
||||
|
||||
const cursorUp = (rows: number = 0) => stream.write(`\u001B[${rows}A`);
|
||||
|
||||
const cursorDown = (rows: number = 0) => stream.write(`\u001B[${rows}B`);
|
||||
|
||||
const cursorHidden = () => {
|
||||
const Promise = require('bluebird') as typeof Bluebird;
|
||||
return Promise.try(hideCursor).disposer(() => {
|
||||
showCursor();
|
||||
});
|
||||
};
|
||||
|
||||
const write = (str: string) => stream.write(str);
|
||||
|
||||
const writeLine = (str: string) => stream.write(`${str}\n`);
|
||||
|
||||
const clearLine = () => stream.write('\u001B[2K\r');
|
||||
|
||||
const replaceLine = (str: string) => {
|
||||
clearLine();
|
||||
return write(str);
|
||||
};
|
||||
|
||||
const deleteToEnd = () => stream.write('\u001b[0J');
|
||||
|
||||
return {
|
||||
stream,
|
||||
currentWindowSize,
|
||||
hideCursor,
|
||||
showCursor,
|
||||
cursorHidden,
|
||||
cursorUp,
|
||||
cursorDown,
|
||||
write,
|
||||
writeLine,
|
||||
clearLine,
|
||||
replaceLine,
|
||||
deleteToEnd,
|
||||
};
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user