UNPKG

2.34 kBJavaScriptView Raw
1'use strict'
2
3// These tables borrowed from `ansi`
4
5var prefix = '\x1b['
6
7exports.up = function up (num) {
8 return prefix + (num || '') + 'A'
9}
10
11exports.down = function down (num) {
12 return prefix + (num || '') + 'B'
13}
14
15exports.forward = function forward (num) {
16 return prefix + (num || '') + 'C'
17}
18
19exports.back = function back (num) {
20 return prefix + (num || '') + 'D'
21}
22
23exports.nextLine = function nextLine (num) {
24 return prefix + (num || '') + 'E'
25}
26
27exports.previousLine = function previousLine (num) {
28 return prefix + (num || '') + 'F'
29}
30
31exports.horizontalAbsolute = function horizontalAbsolute (num) {
32 if (num == null) throw new Error('horizontalAboslute requires a column to position to')
33 return prefix + num + 'G'
34}
35
36exports.eraseData = function eraseData () {
37 return prefix + 'J'
38}
39
40exports.eraseLine = function eraseLine () {
41 return prefix + 'K'
42}
43
44exports.goto = function (x, y) {
45 return prefix + y + ';' + x + 'H'
46}
47
48exports.gotoSOL = function () {
49 return '\r'
50}
51
52exports.beep = function () {
53 return '\x07'
54}
55
56exports.hideCursor = function hideCursor () {
57 return prefix + '?25l'
58}
59
60exports.showCursor = function showCursor () {
61 return prefix + '?25h'
62}
63
64var colors = {
65 reset: 0,
66// styles
67 bold: 1,
68 italic: 3,
69 underline: 4,
70 inverse: 7,
71// resets
72 stopBold: 22,
73 stopItalic: 23,
74 stopUnderline: 24,
75 stopInverse: 27,
76// colors
77 white: 37,
78 black: 30,
79 blue: 34,
80 cyan: 36,
81 green: 32,
82 magenta: 35,
83 red: 31,
84 yellow: 33,
85 bgWhite: 47,
86 bgBlack: 40,
87 bgBlue: 44,
88 bgCyan: 46,
89 bgGreen: 42,
90 bgMagenta: 45,
91 bgRed: 41,
92 bgYellow: 43,
93
94 grey: 90,
95 brightBlack: 90,
96 brightRed: 91,
97 brightGreen: 92,
98 brightYellow: 93,
99 brightBlue: 94,
100 brightMagenta: 95,
101 brightCyan: 96,
102 brightWhite: 97,
103
104 bgGrey: 100,
105 bgBrightBlack: 100,
106 bgBrightRed: 101,
107 bgBrightGreen: 102,
108 bgBrightYellow: 103,
109 bgBrightBlue: 104,
110 bgBrightMagenta: 105,
111 bgBrightCyan: 106,
112 bgBrightWhite: 107
113}
114
115exports.color = function color (colorWith) {
116 if (arguments.length !== 1 || !Array.isArray(colorWith)) {
117 colorWith = Array.prototype.slice.call(arguments)
118 }
119 return prefix + colorWith.map(colorNameToCode).join(';') + 'm'
120}
121
122function colorNameToCode (color) {
123 if (colors[color] != null) return colors[color]
124 throw new Error('Unknown color or style name: ' + color)
125}