Skip to content

Commit

Permalink
refactor(ansi-escape): add return types
Browse files Browse the repository at this point in the history
  • Loading branch information
c4spar committed Aug 7, 2020
1 parent 8b5fbdd commit 2bb165c
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 40 deletions.
48 changes: 24 additions & 24 deletions ansi-escape/ansi-escape.ts
Expand Up @@ -20,79 +20,79 @@ export class AnsiEscape {
*/

/** Move cursor to x, y, counting from the top left corner. */
public cursorTo( x: number, y?: number ) {
public cursorTo( x: number, y?: number ): this {
this.write( cursor.to( x, y ) );
return this;
}

/** Move cursor by offset. */
public cursorMove( x: number, y: number ) {
public cursorMove( x: number, y: number ): this {
this.write( cursor.move( x, y ) );
return this;
}

/** Move cursor up by n lines. */
public cursorUp( count: number = 1 ) {
public cursorUp( count: number = 1 ): this {
this.write( cursor.up( count ) );
return this;
}

/** Move cursor down by n lines. */
public cursorDown( count: number = 1 ) {
public cursorDown( count: number = 1 ): this {
this.write( cursor.down( count ) );
return this;
}

/** Move cursor right by n lines. */
public cursorForward( count: number = 1 ) {
public cursorForward( count: number = 1 ): this {
this.write( cursor.forward( count ) );
return this;
}

/** Move cursor left by n lines. */
public cursorBackward( count: number = 1 ) {
public cursorBackward( count: number = 1 ): this {
this.write( cursor.backward( count ) );
return this;
}

/** Move cursor to the beginning of the line n lines down. */
public cursorNextLine( count: number = 1 ) {
public cursorNextLine( count: number = 1 ): this {
this.write( cursor.nextLine( count ) );
return this;
}

/** Move cursor to the beginning of the line n lines up. */
public cursorPrevLine( count: number = 1 ) {
public cursorPrevLine( count: number = 1 ): this {
this.write( cursor.prevLine( count ) );
return this;
}

/** Move cursor to first column of current row. */
public cursorLeft() {
public cursorLeft(): this {
this.write( cursor.left );
return this;
}

/** Hide cursor. */
public cursorHide() {
public cursorHide(): this {
this.write( cursor.hide );
return this;
}

/** Show cursor. */
public cursorShow() {
public cursorShow(): this {
this.write( cursor.show );
return this;
}

/** Save cursor. */
public cursorSave() {
public cursorSave(): this {
this.write( cursor.save );
return this;
}

/** Restore cursor. */
public cursorRestore() {
public cursorRestore(): this {
this.write( cursor.restore );
return this;
}
Expand All @@ -102,13 +102,13 @@ export class AnsiEscape {
*/

/** Scroll window up by n lines. */
public scrollUp( count: number = 1 ) {
public scrollUp( count: number = 1 ): this {
this.write( scroll.up( count ) );
return this;
}

/** Scroll window down by n lines. */
public scrollDown( count: number = 1 ) {
public scrollDown( count: number = 1 ): this {
this.write( scroll.down( count ) );
return this;
}
Expand All @@ -118,43 +118,43 @@ export class AnsiEscape {
*/

/** Clear screen. */
public eraseScreen() {
public eraseScreen(): this {
this.write( erase.screen );
return this;
}

/** Clear screen up. */
public eraseUp( count: number = 1 ) {
public eraseUp( count: number = 1 ): this {
this.write( erase.up( count ) );
return this;
}

/** Clear screen down. */
public eraseDown( count: number = 1 ) {
public eraseDown( count: number = 1 ): this {
this.write( erase.down( count ) );
return this;
}

/** Clear current line. */
public eraseLine() {
public eraseLine(): this {
this.write( erase.line );
return this;
}

/** Clear to line end. */
public eraseLineEnd() {
public eraseLineEnd(): this {
this.write( erase.lineEnd );
return this;
}

/** Clear to line start. */
public eraseLineStart() {
public eraseLineStart(): this {
this.write( erase.lineStart );
return this;
}

/** Clear n line's up. */
public eraseLines( count: number ) {
public eraseLines( count: number ): this {
this.write( erase.lines( count ) );
return this;
}
Expand All @@ -164,13 +164,13 @@ export class AnsiEscape {
*/

/** Render link. */
public link( text: string, url: string ) {
public link( text: string, url: string ): this {
this.write( link( text, url ) );
return this;
}

/** Render image. */
public image( buffer: Uint8Array, options?: ImageOptions ) {
public image( buffer: Uint8Array, options?: ImageOptions ): this {
this.write( image( buffer, options ) );
return this;
}
Expand Down
31 changes: 15 additions & 16 deletions ansi-escape/csi.ts
Expand Up @@ -8,14 +8,14 @@ const SEP = ';';

export const cursor = {
/** Move cursor to x, y, counting from the top left corner. */
to( x: number, y?: number ) {
to( x: number, y?: number ): string {
if ( typeof y !== 'number' ) {
return `${ CSI }${ x }G`;
}
return `${ CSI }${ y };${ x }H`;
},
/** Move cursor by offset. */
move( x: number, y: number ) {
move( x: number, y: number ): string {
let ret = '';

if ( x < 0 ) {
Expand All @@ -33,17 +33,17 @@ export const cursor = {
return ret;
},
/** Move cursor up by n lines. */
up: ( count: number = 1 ) => `${ CSI }${ count }A`,
up: ( count: number = 1 ): string => `${ CSI }${ count }A`,
/** Move cursor down by n lines. */
down: ( count: number = 1 ) => `${ CSI }${ count }B`,
down: ( count: number = 1 ): string => `${ CSI }${ count }B`,
/** Move cursor right by n lines. */
forward: ( count: number = 1 ) => `${ CSI }${ count }C`,
forward: ( count: number = 1 ): string => `${ CSI }${ count }C`,
/** Move cursor left by n lines. */
backward: ( count: number = 1 ) => `${ CSI }${ count }D`,
backward: ( count: number = 1 ): string => `${ CSI }${ count }D`,
/** Move cursor to the beginning of the line n lines down. */
nextLine: ( count: number = 1 ) => `${ CSI }E`.repeat( count ),
nextLine: ( count: number = 1 ): string => `${ CSI }E`.repeat( count ),
/** Move cursor to the beginning of the line n lines up. */
prevLine: ( count: number = 1 ) => `${ CSI }F`.repeat( count ),
prevLine: ( count: number = 1 ): string => `${ CSI }F`.repeat( count ),
/** Move cursor to first column of current row. */
left: `${ CSI }G`,
/** Hide cursor. */
Expand All @@ -58,26 +58,26 @@ export const cursor = {

export const scroll = {
/** Scroll window up by n lines. */
up: ( count: number = 1 ) => `${ CSI }S`.repeat( count ),
up: ( count: number = 1 ): string => `${ CSI }S`.repeat( count ),
/** Scroll window down by n lines. */
down: ( count: number = 1 ) => `${ CSI }T`.repeat( count )
down: ( count: number = 1 ): string => `${ CSI }T`.repeat( count )
};

export const erase = {
/** Clear screen. */
screen: `${ CSI }2J`,
/** Clear screen up. */
up: ( count: number = 1 ) => `${ CSI }1J`.repeat( count ),
up: ( count: number = 1 ): string => `${ CSI }1J`.repeat( count ),
/** Clear screen down. */
down: ( count: number = 1 ) => `${ CSI }0J`.repeat( count ),
down: ( count: number = 1 ): string => `${ CSI }0J`.repeat( count ),
/** Clear current line. */
line: `${ CSI }2K`,
/** Clear to line end. */
lineEnd: `${ CSI }0K`,
/** Clear to line start. */
lineStart: `${ CSI }1K`,
/** Clear n line's up. */
lines( count: number ) {
lines( count: number ): string {
let clear = '';
for ( let i = 0; i < count; i++ ) {
clear += this.line + ( i < count - 1 ? cursor.up() : '' );
Expand All @@ -88,7 +88,7 @@ export const erase = {
};

/** Render link. */
export const link = ( text: string, url: string ) => [
export const link = ( text: string, url: string ): string => [
OSC,
'8',
SEP,
Expand All @@ -110,8 +110,7 @@ export interface ImageOptions {
}

/** Render image. */
export const image = ( buffer: Uint8Array, options?: ImageOptions ) => {

export const image = ( buffer: Uint8Array, options?: ImageOptions ): string => {
let ret = `${ OSC }1337;File=inline=1`;

if ( options?.width ) {
Expand Down

0 comments on commit 2bb165c

Please sign in to comment.