Skip to content

Instantly share code, notes, and snippets.

@Xananax
Last active June 22, 2016 15:47
Show Gist options
  • Save Xananax/e7460824fd4cba99d92ae3de4dd68af2 to your computer and use it in GitHub Desktop.
Save Xananax/e7460824fd4cba99d92ae3de4dd68af2 to your computer and use it in GitHub Desktop.
// Type definitions for Charm v1.0.1
// Project: https://github.com/substack/node-charm/
// Definitions by: Jad Sarout <https://github.com/Xananax/>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
declare module Charm{
type CharmColorName = 'red' | 'yellow' | 'green' | 'blue' | 'cyan' | 'magenta' | 'black' | 'white'
type CharmColorHex = number;
type CharmColor = CharmColorName | CharmColorHex;
type CharmAnyStream = NodeJS.WritableStream|NodeJS.ReadableStream|NodeJS.Process;
interface CharmInstance extends NodeJS.WritableStream{
/**
* Reset the entire screen, like the /usr/bin/reset command.
* @returns void
*/
reset():void;
/**
* Emit an "end" event downstream.
* @returns void
*/
destroy():void;
/**
* Emit an "end" event downstream.
* @returns void
*/
end():void;
/**
* Pass along `msg` to the output stream.
* @param {string|Buffer} msg
* @param {Function} cb? unused by charm, only there to comply to the WritableStream interface
* @returns boolean
*/
write(msg:string|Buffer,cb?:Function):boolean;
write(msgs:string|Buffer,encoding?:string,cb?:Function):boolean;
/**
* Set the cursor position to the absolute coordinates `x`, `y`.
* @param {number} x
* @param {number} y
* @returns void
*/
position(x:number,y:number):void;
/**
* Query the absolute cursor position from the input stream through the output stream
* (the shell does this automatically) and get the response back as `cb(x, y)`.
* @param {(x:number,y:number)=>void} callback
* @returns void
*/
position(callback:(x:number,y:number)=>void):void;
/**
* Move the cursor position by the relative coordinates `x`, `y`.
* @param {number} x
* @param {number} y
* @returns void
*/
move(x:number,y:number):void;
/**
* Move the cursor up by `y` rows.
* @param {number} y
* @returns void
*/
up(y:number):void;
/**
* Move the cursor down by `y` rows.
* @param {number} y
* @returns void
*/
down(y:number):void;
/**
* Move the cursor left by `x` columns.
* @param {number} x
* @returns void
*/
left(x:number):void;
/**
* Move the cursor right by `x` columns.
* @param {number} x
* @returns void
*/
right(x:number):void;
/**
* Push the cursor state and optionally the attribute state.
* @param {boolean} withAttributes?
* @returns void
*/
push(withAttributes?:boolean):void;
/**
* Pop the cursor state and optionally the attribute state.
* @param {boolean} withAttributes?
* @returns void
*/
pop(withAttributes?:boolean):void;
/**
* Erase a region defined by the string `s`.
*
* `s` can be:
*
* - end - erase from the cursor to the end of the line
* - start - erase from the cursor to the start of the line
* - line - erase the current line
* - down - erase everything below the current line
* - up - erase everything above the current line
* - screen - erase the entire screen
* @param {'end'|'start'|'line'|'down'|'up'|'screen'} s
* @returns void
*/
erase(s:'end'|'start'|'line'|'down'|'up'|'screen'):void;
/**
* Delete `'line'` or `'char'`s. delete differs from erase because it does not write over
* the deleted characters with whitesapce, but instead removes the deleted space.
*
* mode can be `'line'` or `'char'`. `n` is the number of items to be deleted.
* `n` must be a positive integer.
*
* The cursor position is not updated.
* @param {'line'|'char'} mode
* @param {number} n
* @returns void
*/
delete(mode:'line'|'char',n:number):void;
/**
* Insert space into the terminal. `insert` is the opposite of `delete`,
*
* mode can be `'line'` or `'char'`. `n` is the number of items to be deleted.
* `n` must be a positive integer.
*
* @param {'line'|'char'} mode
* @param {number} n
* @returns void
*/
insert(mode:'line'|'char',n:number):void;
/**
* Set the display mode with the string `attr.`
*
* `attr` can be:
*
* - reset
* - bright
* - dim
* - underscore
* - blink
* - reverse
* - hidden
*
* @param {'reset'|'bright'|'dim'|'underscore'|'blink'|'reverse'|'hidden'} attr
*/
display(attr:'reset'|'bright'|'dim'|'underscore'|'blink'|'reverse'|'hidden')
/**
* Set the foreground color with the string `color`, which can be:
*
* - red
* - yellow
* - green
* - blue
* - cyan
* - magenta
* - black
* - white
* - or `color` can be an integer from 0 to 255, inclusive.
* @param {CharmColor} color
*/
foreground(color:CharmColor)
/**
* Set the background color with the string `color`, which can be:
*
* - red
* - yellow
* - green
* - blue
* - cyan
* - magenta
* - black
* - white
* - or `color` can be an integer from 0 to 255, inclusive.
* @param {CharmColor} color
*/
background(color:CharmColor)
/**
* Set the cursor visibility with a boolean `visible`.
* @param {boolean} visible
*/
cursor(visible:boolean)
/**
* Pipes the output of Charm to a writeable stream `stream`
* @param {NodeJS.WritableStream} stream
* @returns void
*/
pipe(stream:NodeJS.WritableStream):void;
}
export interface CharmConstructor{
/**
* Create a new readable/writable charm stream.
*
* You can pass in readable or writable streams as parameters
* and they will be piped to or from accordingly.
* You can also pass `process` in which case
* `process.stdin` and `process.stdout` will be used.
*
* @param {CharmAnyStream} param
* @returns CharmInstance
*/
(param:CharmAnyStream):CharmInstance;
}
}
declare module 'charm'{
var charm:Charm.CharmConstructor;
export = charm;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment