.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / enquirer / lib / ansi.js
1 'use strict';
2
3 const isTerm = process.env.TERM_PROGRAM === 'Apple_Terminal';
4 const colors = require('ansi-colors');
5 const utils = require('./utils');
6 const ansi = module.exports = exports;
7 const ESC = '\u001b[';
8 const BEL = '\u0007';
9 let hidden = false;
10
11 const code = ansi.code = {
12   bell: BEL,
13   beep: BEL,
14   beginning: `${ESC}G`,
15   down: `${ESC}J`,
16   esc: ESC,
17   getPosition: `${ESC}6n`,
18   hide: `${ESC}?25l`,
19   line: `${ESC}2K`,
20   lineEnd: `${ESC}K`,
21   lineStart: `${ESC}1K`,
22   restorePosition: ESC + (isTerm ? '8' : 'u'),
23   savePosition: ESC + (isTerm ? '7' : 's'),
24   screen: `${ESC}2J`,
25   show: `${ESC}?25h`,
26   up: `${ESC}1J`
27 };
28
29 const cursor = ansi.cursor = {
30   get hidden() {
31     return hidden;
32   },
33
34   hide() {
35     hidden = true;
36     return code.hide;
37   },
38   show() {
39     hidden = false;
40     return code.show;
41   },
42
43   forward: (count = 1) => `${ESC}${count}C`,
44   backward: (count = 1) => `${ESC}${count}D`,
45   nextLine: (count = 1) => `${ESC}E`.repeat(count),
46   prevLine: (count = 1) => `${ESC}F`.repeat(count),
47
48   up: (count = 1) => count ? `${ESC}${count}A` : '',
49   down: (count = 1) => count ? `${ESC}${count}B` : '',
50   right: (count = 1) => count ? `${ESC}${count}C` : '',
51   left: (count = 1) => count ? `${ESC}${count}D` : '',
52
53   to(x, y) {
54     return y ? `${ESC}${y + 1};${x + 1}H` : `${ESC}${x + 1}G`;
55   },
56
57   move(x = 0, y = 0) {
58     let res = '';
59     res += (x < 0) ? cursor.left(-x) : (x > 0) ? cursor.right(x) : '';
60     res += (y < 0) ? cursor.up(-y) : (y > 0) ? cursor.down(y) : '';
61     return res;
62   },
63
64   restore(state = {}) {
65     let { after, cursor, initial, input, prompt, size, value } = state;
66     initial = utils.isPrimitive(initial) ? String(initial) : '';
67     input = utils.isPrimitive(input) ? String(input) : '';
68     value = utils.isPrimitive(value) ? String(value) : '';
69
70     if (size) {
71       let codes = ansi.cursor.up(size) + ansi.cursor.to(prompt.length);
72       let diff = input.length - cursor;
73       if (diff > 0) {
74         codes += ansi.cursor.left(diff);
75       }
76       return codes;
77     }
78
79     if (value || after) {
80       let pos = (!input && !!initial) ? -initial.length : -input.length + cursor;
81       if (after) pos -= after.length;
82       if (input === '' && initial && !prompt.includes(initial)) {
83         pos += initial.length;
84       }
85       return ansi.cursor.move(pos);
86     }
87   }
88 };
89
90 const erase = ansi.erase = {
91   screen: code.screen,
92   up: code.up,
93   down: code.down,
94   line: code.line,
95   lineEnd: code.lineEnd,
96   lineStart: code.lineStart,
97   lines(n) {
98     let str = '';
99     for (let i = 0; i < n; i++) {
100       str += ansi.erase.line + (i < n - 1 ? ansi.cursor.up(1) : '');
101     }
102     if (n) str += ansi.code.beginning;
103     return str;
104   }
105 };
106
107 ansi.clear = (input = '', columns = process.stdout.columns) => {
108   if (!columns) return erase.line + cursor.to(0);
109   let width = str => [...colors.unstyle(str)].length;
110   let lines = input.split(/\r?\n/);
111   let rows = 0;
112   for (let line of lines) {
113     rows += 1 + Math.floor(Math.max(width(line) - 1, 0) / columns);
114   }
115   return (erase.line + cursor.prevLine()).repeat(rows - 1) + erase.line + cursor.to(0);
116 };