Debian Codegraph @

cli-color/slice.js

raw
1 /* eslint max-lines: "off" */
2
3 "use strict";
4
5 var reAnsi = require("./regex-ansi")
6 , stringifiable = require("es5-ext/object/validate-stringifiable-value")
7 , length = require("./get-stripped-length")
8 , sgr = require("./lib/sgr")
9 , max = Math.max;
10
11 var Token = function (token) { this.token = token; };
12
13 var tokenize = function (str) {
14 var match = reAnsi().exec(str);
15
16 if (!match) {
17 return [str];
18 }
19
20 var index = match.index, head, prehead, tail;
21
22 if (index === 0) {
23 head = match[0];
24 tail = str.slice(head.length);
25
26 return [new Token(head)].concat(tokenize(tail));
27 }
28
29 prehead = str.slice(0, index);
30 head = match[0];
31 tail = str.slice(index + head.length);
32
33 return [prehead, new Token(head)].concat(tokenize(tail));
34 };
35
36 var isChunkInSlice = function (chunk, index, begin, end) {
37 var endIndex = chunk.length + index;
38
39 if (begin > endIndex) return false;
40 if (end < index) return false;
41 return true;
42 };
43
44 // eslint-disable-next-line max-lines-per-function
45 var sliceSeq = function (seq, begin, end) {
46 var sliced = seq.reduce(
47 function (state, chunk) {
48 var index = state.index;
49
50 if (chunk instanceof Token) {
51 var code = sgr.extractCode(chunk.token);
52
53 if (index <= begin) {
54 if (code in sgr.openers) {
55 sgr.openStyle(state.preOpeners, code);
56 }
57 if (code in sgr.closers) {
58 sgr.closeStyle(state.preOpeners, code);
59 }
60 } else if (index < end) {
61 if (code in sgr.openers) {
62 sgr.openStyle(state.inOpeners, code);
63 state.seq.push(chunk);
64 } else if (code in sgr.closers) {
65 state.inClosers.push(code);
66 state.seq.push(chunk);
67 }
68 }
69 } else {
70 var nextChunk = "";
71
72 if (isChunkInSlice(chunk, index, begin, end)) {
73 var relBegin = Math.max(begin - index, 0)
74 , relEnd = Math.min(end - index, chunk.length);
75
76 nextChunk = chunk.slice(relBegin, relEnd);
77 }
78
79 state.seq.push(nextChunk);
80 state.index = index + chunk.length;
81 }
82
83 return state;
84 },
85 {
86 index: 0,
87 seq: [],
88
89 // preOpeners -> [ mod ]
90 // preOpeners must be prepended to the slice if they wasn't closed til the end of it
91 // preOpeners must be closed if they wasn't closed til the end of the slice
92 preOpeners: [],
93
94 // inOpeners -> [ mod ]
95 // inOpeners already in the slice and must not be prepended to the slice
96 // inOpeners must be closed if they wasn't closed til the end of the slice
97 inOpeners: [], // opener CSI inside slice
98
99 // inClosers -> [ code ]
100 // closer CSIs for determining which pre/in-Openers must be closed
101 inClosers: []
102 }
103 );
104
105 sliced.seq = [].concat(
106 sgr.prepend(sliced.preOpeners), sliced.seq,
107 sgr.complete([].concat(sliced.preOpeners, sliced.inOpeners), sliced.inClosers)
108 );
109
110 return sliced.seq;
111 };
112
113 module.exports = function (str /*, begin, end*/) {
114 var seq, begin = Number(arguments[1]), end = Number(arguments[2]), len;
115
116 str = stringifiable(str);
117 len = length(str);
118
119 if (isNaN(begin)) {
120 begin = 0;
121 }
122 if (isNaN(end)) {
123 end = len;
124 }
125 if (begin < 0) {
126 begin = max(len + begin, 0);
127 }
128 if (end < 0) {
129 end = max(len + end, 0);
130 }
131
132 seq = tokenize(str);
133 seq = sliceSeq(seq, begin, end);
134 return seq
135 .map(function (chunk) {
136 if (chunk instanceof Token) {
137 return chunk.token;
138 }
139
140 return chunk;
141 })
142 .join("");
143 };