-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlib.js
More file actions
85 lines (79 loc) · 2.08 KB
/
lib.js
File metadata and controls
85 lines (79 loc) · 2.08 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
String.prototype.trimAnsi=function(){
let str = this
return str.slice(str.indexOf('m')+1,str.lastIndexOf('\x1b[0m'))
}
String.prototype.underline=function(){
let str =this
return str.ansi(4)
}
String.prototype.invert=function(){
let str =this
return str.ansi(7)
}
String.prototype.ansi=function(code){
// \x1b[a;b;cmCONTENT\x1b[0m
// Rewrite to suit every situation
// 1. there is no ansicode
// 2. there is ansicode with one property
// 3. there is ansicode with many property
let str =this
if (!str.includes('\x1b[0m')) return `\x1b[${code}m${str}\x1b[0m`
else {
//return `\x1b[${str}\x1b[0m`
// return str.insert(code+';',str.indexOf('[')+1)
return str.insert(';'+code,str.indexOf('m'))
}
}
String.prototype.green=function(){
let str =this
return str.ansi(32)
}
String.prototype.magenta=function(){
let str =this
return str.ansi(35)
}
String.prototype.blue=function(){
let str =this
return str.ansi(34)
}
String.prototype.yellow=function(){
let str =this
return str.ansi(33)
}
String.prototype.cyan=function(){
let str =this
return str.ansi(36)
}
String.prototype.grey=function(){
let str =this
return str.ansi(90)
}
String.prototype.red=function(){
let str =this
return str.ansi(31)
}
String.prototype.insert=function(str,position){
let origin = this;
if(origin.length>position){
return origin.slice(0,position)+str+origin.slice(position)
}
}
export function center(text){
let width = process.stdout.getWindowSize()[0]
let len = text.trimAnsi().length
let str = ' '.repeat(Math.round((width-len)/2))+text
return str
}
export function right(text){
let width = process.stdout.getWindowSize()[0]
// let len = text.trim().trimAnsi().length
let len = text.trimAnsi().length
let str = ' '.repeat(Math.round(width-len))+text
return str
}
// console.log('red and green'.invert().red().green().underline())
// console.log('green and red'.green().red().underline())
// console.log('\x1B[31m这是红色\x1B[0m'.ansi(97).ansi(42))
// console.log('\x1B[4;31m这是红色\x1B[0m')
// console.log("012345".insert("rose",3))
// console.log(trimAnsi('\x1B[31m这是红色\x1B[0m'))