xshell 0.0.9 → 0.0.13

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/prototype.js CHANGED
@@ -1,18 +1,19 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.is_codepoint_fullwidth = exports.to_json_safely = exports.to_json = exports.BRACKETS = exports.QUOTES = exports.CJK = exports.to_getter_property_descriptors = exports.to_method_property_descriptors = exports.colors = exports.EMOJI_REGEX = void 0;
3
+ exports.is_codepoint_fullwidth = exports.to_json_safely = exports.to_json = exports.brackets = exports.quotes = exports.cjk = exports.to_getter_property_descriptors = exports.to_method_property_descriptors = exports.chalk = exports.emoji_regex = void 0;
4
4
  const tslib_1 = require("tslib");
5
- const fs_1 = tslib_1.__importDefault(require("fs"));
6
- const upath_1 = tslib_1.__importDefault(require("upath"));
5
+ const fs_1 = (0, tslib_1.__importDefault)(require("fs"));
6
+ const upath_1 = (0, tslib_1.__importDefault)(require("upath"));
7
7
  // @ts-ignore
8
- const byte_size_1 = tslib_1.__importDefault(require("byte-size"));
9
- const emoji_regex_1 = tslib_1.__importDefault(require("emoji-regex"));
10
- const strip_ansi_1 = tslib_1.__importDefault(require("strip-ansi"));
11
- const safe_1 = tslib_1.__importDefault(require("colors/safe"));
12
- exports.colors = safe_1.default;
13
- exports.EMOJI_REGEX = emoji_regex_1.default();
8
+ const byte_size_1 = (0, tslib_1.__importDefault)(require("byte-size"));
9
+ const emoji_regex_1 = (0, tslib_1.__importDefault)(require("emoji-regex"));
10
+ const strip_ansi_1 = (0, tslib_1.__importDefault)(require("strip-ansi"));
11
+ const chalk_1 = (0, tslib_1.__importDefault)(require("chalk"));
12
+ exports.chalk = chalk_1.default;
13
+ exports.emoji_regex = (0, emoji_regex_1.default)();
14
14
  function to_method_property_descriptors(methods) {
15
- return Object.fromEntries(Object.entries(methods).map(([name, value]) => ([name, {
15
+ return Object.fromEntries(Object.entries(methods)
16
+ .map(([name, value]) => ([name, {
16
17
  configurable: true,
17
18
  writable: true,
18
19
  enumerable: false,
@@ -21,42 +22,44 @@ function to_method_property_descriptors(methods) {
21
22
  }
22
23
  exports.to_method_property_descriptors = to_method_property_descriptors;
23
24
  function to_getter_property_descriptors(getters) {
24
- return Object.fromEntries(Object.entries(getters).map(([name, get]) => ([name, {
25
+ return Object.fromEntries(Object.entries(getters)
26
+ .map(([name, get]) => ([name, {
25
27
  configurable: true,
26
28
  enumerable: false,
27
29
  get,
28
30
  }])));
29
31
  }
30
32
  exports.to_getter_property_descriptors = to_getter_property_descriptors;
31
- exports.CJK = '([\u2e80-\u9fff\uf900-\ufaff])';
32
- exports.QUOTES = {
33
- SINGLE: "'",
34
- DOUBLE: '"',
35
- BACKTICK: '`',
33
+ exports.cjk = '([\u2e80-\u9fff\uf900-\ufaff])';
34
+ exports.quotes = {
35
+ single: "'",
36
+ double: '"',
37
+ backtick: '`',
36
38
  };
37
- exports.BRACKETS = {
38
- ROUND: ['(', ')'],
39
- SQUARE: ['[', ']'],
40
- CURLY: ['{', '}'],
41
- POINTY: ['<', '>'],
42
- CORNER: ['「', '」'],
43
- FAT: ['【', '】'],
44
- TORTOISE_SHELL: ['〔', '〕'],
39
+ exports.brackets = {
40
+ round: ['(', ')'],
41
+ square: ['[', ']'],
42
+ curly: ['{', '}'],
43
+ pointy: ['<', '>'],
44
+ corner: ['「', '」'],
45
+ fat: ['【', '】'],
46
+ tortoise_shell: ['〔', '〕'],
45
47
  };
48
+ const color_map = Object.fromEntries(['red_', 'green_', 'yellow_', 'blue_', 'magenta_', 'cyan_'].map(color => [color, `${color.slice(0, -1)}Bright`]));
46
49
  // ------------------------------------ String.prototype
47
50
  Object.defineProperties(String.prototype, {
48
51
  ...to_getter_property_descriptors({
49
52
  width() {
50
- const s = strip_ansi_1.default(this.replace(exports.EMOJI_REGEX, ' '));
53
+ const s = (0, strip_ansi_1.default)(this.replace(exports.emoji_regex, ' '));
51
54
  let width = 0;
52
55
  for (let i = 0; i < s.length; i++) {
53
56
  const code = s.codePointAt(i);
54
- if ((code <= 0x1F || (code >= 0x7F && code <= 0x9F)) || // Ignore control characters
55
- code >= 0x300 && code <= 0x36F // Ignore combining characters
57
+ if ((code <= 0x1f || (code >= 0x7f && code <= 0x9f)) || // ignore control characters
58
+ code >= 0x300 && code <= 0x36f // ignore combining characters
56
59
  )
57
60
  continue;
58
- // Surrogates
59
- if (code > 0xFFFF)
61
+ // surrogates
62
+ if (code > 0xffff)
60
63
  i++;
61
64
  width += is_codepoint_fullwidth(code) ? 2 : 1;
62
65
  }
@@ -68,7 +71,7 @@ Object.defineProperties(String.prototype, {
68
71
  /** truncate string with fixed width and preserve color */
69
72
  truncate(width) {
70
73
  const color_bak = this.startsWith('\u001b') ? this.slice(0, 5) : '';
71
- const s = strip_ansi_1.default(this);
74
+ const s = (0, strip_ansi_1.default)(this);
72
75
  if (width <= 2)
73
76
  return this.slice(0, width);
74
77
  let i_fitted = 0;
@@ -80,7 +83,7 @@ Object.defineProperties(String.prototype, {
80
83
  code >= 0x300 && code <= 0x36F // Ignore combining characters
81
84
  )
82
85
  continue;
83
- // Surrogates (codepoint need two UTF-16 encoding units, thus here skip the first in order to prevent repeated counting)
86
+ // surrogates (codepoint need two utf-16 encoding units, thus here skip the first in order to prevent repeated counting)
84
87
  if (code > 0xFFFF)
85
88
  i++;
86
89
  const w = is_codepoint_fullwidth(code) ? 2 : 1;
@@ -111,8 +114,7 @@ Object.defineProperties(String.prototype, {
111
114
  to_regx(preservations, flags = '') {
112
115
  const preserved_chars = new Set(preservations);
113
116
  const replace_chars = Array.prototype.filter.call('|\\{}()[]^$+*?.-', (c) => !preserved_chars.has(c))
114
- .map((c) => c === ']' ? '\\]' : c)
115
- .join('');
117
+ .map((c) => c === ']' ? '\\]' : c).join('');
116
118
  return new RegExp(this.replace(new RegExp(`[${replace_chars}]`, 'g'), '\\$&'), flags);
117
119
  },
118
120
  to_bool() {
@@ -139,7 +141,10 @@ Object.defineProperties(String.prototype, {
139
141
  placeholder_name = placeholder_name.slice(0, -1);
140
142
  optional = true;
141
143
  }
142
- $placeholders[placeholder_name] = regx_parts.push(placeholder_pattern ? placeholder_pattern.bracket() + (optional ? '?' : '') : '(.*?)');
144
+ $placeholders[placeholder_name] = regx_parts.push(placeholder_pattern ?
145
+ `${placeholder_pattern.bracket()}${optional ? '?' : ''}`
146
+ :
147
+ '(.*?)');
143
148
  return '';
144
149
  });
145
150
  add_part(last_end);
@@ -152,11 +157,13 @@ Object.defineProperties(String.prototype, {
152
157
  const matches = pattern_regx.exec(this);
153
158
  if (!matches)
154
159
  return this;
155
- const placeholders = Object.fromEntries(Object.entries($placeholders).map(([name, $i]) => ([
160
+ const placeholders = Object.fromEntries(Object.entries($placeholders)
161
+ .map(([name, $i]) => [
156
162
  [name, matches[$i]],
157
- [name + '.before', matches[$i - 1] || ''],
158
- [name + '.after', matches[$i + 1] || ''],
159
- ])).flat());
163
+ [`${name}.before`, matches[$i - 1] || ''],
164
+ [`${name}.after`, matches[$i + 1] || ''],
165
+ ])
166
+ .flat());
160
167
  // --- convert pattern_ to replacement_str, if transformer exists then apply on placeholder
161
168
  last_end = 0;
162
169
  let replacement_parts = [];
@@ -191,28 +198,32 @@ Object.defineProperties(String.prototype, {
191
198
  placeholder_name = placeholder_name.slice(0, -1);
192
199
  optional = true;
193
200
  }
194
- $placeholders[placeholder_name] = regx_parts.push(placeholder_pattern ? placeholder_pattern.bracket() + (optional ? '?' : '') : '(.*?)');
201
+ $placeholders[placeholder_name] = regx_parts.push(placeholder_pattern ?
202
+ `${placeholder_pattern.bracket()}${optional ? '?' : ''}`
203
+ :
204
+ '(.*?)');
195
205
  return '';
196
206
  });
197
207
  add_part(last_end);
198
208
  // convert last (.*?) to greedy, to make .{suffix} work
199
209
  regx_parts = regx_parts.filter(part => part);
200
- if (regx_parts.last === '(.*?)')
210
+ if (regx_parts[regx_parts.length - 1] === '(.*?)')
201
211
  regx_parts[regx_parts.length - 1] = '(.*)';
202
212
  const pattern_regx = new RegExp(regx_parts.join(''), flags);
203
213
  // --- match original string based on pattern_regx, and get result to build placeholders dict
204
214
  const matches = pattern_regx.exec(this);
205
215
  if (!matches)
206
216
  return {};
207
- return Object.fromEntries(Object.entries($placeholders).map(([name, $i]) => ([name, matches[$i] || ''])));
217
+ return Object.fromEntries(Object.entries($placeholders)
218
+ .map(([name, $i]) => [name, matches[$i] || '']));
208
219
  },
209
- quote(type = 'SINGLE') {
210
- if (type === 'PSH')
211
- return '& ' + this.quote();
212
- return this.surround(exports.QUOTES[type]);
220
+ quote(type = 'single') {
221
+ if (type === 'psh')
222
+ return `& ${this.quote()}`;
223
+ return this.surround(exports.quotes[type]);
213
224
  },
214
- bracket(shape = 'ROUND') {
215
- return this.surround(...exports.BRACKETS[shape]);
225
+ bracket(shape = 'round') {
226
+ return this.surround(...exports.brackets[shape]);
216
227
  },
217
228
  surround(left, right) {
218
229
  return left + this + (right || left);
@@ -232,7 +243,10 @@ Object.defineProperties(String.prototype, {
232
243
  return this.replace(pattern, '');
233
244
  },
234
245
  split_lines(delimiter = /\r?\n/) {
235
- return this.split(delimiter);
246
+ let lines = this.split(delimiter);
247
+ if (lines.last === '')
248
+ lines.pop();
249
+ return lines;
236
250
  },
237
251
  split_indent() {
238
252
  let i = 0;
@@ -255,41 +269,48 @@ Object.defineProperties(String.prototype, {
255
269
  to_base64() {
256
270
  return Buffer.from(this).toString('base64');
257
271
  },
258
- decode_base64() {
259
- return Buffer.from(this, 'base64').toString();
272
+ decode_base64(buffer = false) {
273
+ const buf = Buffer.from(this, 'base64');
274
+ if (buffer)
275
+ return buf;
276
+ return buf.toString();
260
277
  },
261
278
  strip_ansi() {
262
- return strip_ansi_1.default(this);
279
+ return (0, strip_ansi_1.default)(this);
263
280
  },
264
281
  space() {
265
282
  if (!this)
266
283
  return this;
267
284
  let text_;
268
285
  text_ = this
269
- .replace(new RegExp(exports.CJK + `(['"])`, 'g'), '$1 $2')
270
- .replace(new RegExp(`(['"])` + exports.CJK, 'g'), '$1 $2')
286
+ .replace(new RegExp(exports.cjk + `(['"])`, 'g'), '$1 $2')
287
+ .replace(new RegExp(`(['"])` + exports.cjk, 'g'), '$1 $2')
271
288
  .replace(/(["']+)\s*(.+?)\s*(["']+)/g, '$1$2$3')
272
- .replace(new RegExp(exports.CJK + '([\\+\\-\\*\\/=&\\\\\\|<>])([A-Za-z0-9])', 'g'), '$1 $2 $3')
273
- .replace(new RegExp('([A-Za-z0-9])([\\+\\-\\*\\/=&\\\\\\|<>])' + exports.CJK, 'g'), '$1 $2 $3');
289
+ .replace(new RegExp(exports.cjk + '([\\+\\-\\*\\/=&\\\\\\|<>])([A-Za-z0-9])', 'g'), '$1 $2 $3')
290
+ .replace(new RegExp('([A-Za-z0-9])([\\+\\-\\*\\/=&\\\\\\|<>])' + exports.cjk, 'g'), '$1 $2 $3');
274
291
  const textBak = text_;
275
- text_ = text_.replace(new RegExp(exports.CJK + '([\\(\\[\\{<\u201c]+(.*?)[\\)\\]\\}>\u201d]+)' + exports.CJK, 'g'), '$1 $2 $4');
292
+ text_ = text_.replace(new RegExp(exports.cjk + '([\\(\\[\\{<\u201c]+(.*?)[\\)\\]\\}>\u201d]+)' + exports.cjk, 'g'), '$1 $2 $4');
276
293
  if (text_ === textBak)
277
294
  text_ = text_
278
- .replace(new RegExp(exports.CJK + '([\\(\\[\\{<\u201c>])', 'g'), '$1 $2')
279
- .replace(new RegExp('([\\)\\]\\}>\u201d<])' + exports.CJK, 'g'), '$1 $2');
295
+ .replace(new RegExp(exports.cjk + '([\\(\\[\\{<\u201c>])', 'g'), '$1 $2')
296
+ .replace(new RegExp('([\\)\\]\\}>\u201d<])' + exports.cjk, 'g'), '$1 $2');
280
297
  return text_
281
298
  // eslint-disable-next-line no-useless-escape
282
299
  .replace(/([\(\[\{<\u201c]+)(\s*)(.+?)(\s*)([\)\]\}>\u201d]+)/g, '$1$3$5')
283
- .replace(new RegExp(exports.CJK + '([~!;:,\\.\\?\u2026])([A-Za-z0-9])', 'g'), '$1$2 $3')
284
- .replace(new RegExp(exports.CJK + '([A-Za-z0-9`\\$%\\^&\\*\\-=\\+\\\\\\|\\/@\u00a1-\u00ff\u2022\u2027\u2150-\u218f])', 'g'), '$1 $2')
285
- .replace(new RegExp('([A-Za-z0-9`\\$%\\^&\\*\\-=\\+\\\\\\|\\/@\u00a1-\u00ff\u2022\u2027\u2150-\u218f])' + exports.CJK, 'g'), '$1 $2');
300
+ .replace(new RegExp(exports.cjk + '([~!;:,\\.\\?\u2026])([A-Za-z0-9])', 'g'), '$1$2 $3')
301
+ .replace(new RegExp(exports.cjk + '([A-Za-z0-9`\\$%\\^&\\*\\-=\\+\\\\\\|\\/@\u00a1-\u00ff\u2022\u2027\u2150-\u218f])', 'g'), '$1 $2')
302
+ .replace(new RegExp('([A-Za-z0-9`\\$%\\^&\\*\\-=\\+\\\\\\|\\/@\u00a1-\u00ff\u2022\u2027\u2150-\u218f])' + exports.cjk, 'g'), '$1 $2');
286
303
  }
287
304
  }),
288
- // ------------ colors
289
- ...Object.fromEntries(['red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'grey', 'underline'].map(color => ([color, {
305
+ // ------------ chalk colors
306
+ ...Object.fromEntries([
307
+ 'red', 'green', 'yellow', 'blue', 'magenta', 'cyan', 'grey',
308
+ 'red_', 'green_', 'yellow_', 'blue_', 'magenta_', 'cyan_',
309
+ 'underline',
310
+ ].map(color => ([color, {
290
311
  configurable: true,
291
312
  get() {
292
- return safe_1.default[color](this);
313
+ return chalk_1.default[color_map[color] || color](this);
293
314
  }
294
315
  }]))),
295
316
  // ------------ file path ops
@@ -367,14 +388,14 @@ Object.defineProperties(Date.prototype, to_method_property_descriptors({
367
388
  // ------------------------------------ Number.prototype
368
389
  Object.defineProperties(Number.prototype, to_method_property_descriptors({
369
390
  to_fsize_str(units = 'iec') {
370
- const { value, unit } = byte_size_1.default(this, { units });
371
- return `${value} ${unit.rm('i').toUpperCase()}`;
391
+ const { value, unit } = (0, byte_size_1.default)(this, { units });
392
+ return `${value} ${unit.rm('i')}`;
372
393
  },
373
394
  to_bin_str() {
374
395
  return `0b${this.toString(2)}`;
375
396
  },
376
397
  to_hex_str(length) {
377
- const s = this.toString(16).toUpperCase();
398
+ const s = this.toString(16);
378
399
  if (!length)
379
400
  length = Math.ceil(s.length / 4) * 4;
380
401
  return `0x${'0'.repeat(length - s.length)}${s}`;
@@ -404,7 +425,6 @@ Object.defineProperties(Array.prototype, {
404
425
  trim_lines({ trim_line = true, rm_empty_lines = true, rm_last_empty_lines = false } = {}) {
405
426
  if (!this.length)
406
427
  return this;
407
- // eslint-disable-next-line @typescript-eslint/no-this-alias
408
428
  let lines = this;
409
429
  if (trim_line)
410
430
  lines = lines.map(line => line.trim());
@@ -435,15 +455,16 @@ Object.defineProperties(Array.prototype, {
435
455
  return this.map(line => character.repeat(width) + line);
436
456
  },
437
457
  indent2to4() {
438
- return this.split_indents().map(line => ' '.repeat(Math.floor(line.indent / 2) * 4) + line.text);
458
+ return this.split_indents()
459
+ .map(line => ' '.repeat(Math.floor(line.indent / 2) * 4) + line.text);
439
460
  },
440
- join_lines() {
441
- return this.join('\n');
461
+ join_lines(append = true) {
462
+ return `${this.join('\n')}${append ? '\n' : ''}`;
442
463
  }
443
464
  })
444
465
  });
445
466
  function to_json(obj, replacer) {
446
- return JSON.stringify(obj, replacer, 4);
467
+ return JSON.stringify(obj, replacer, 4) + '\n';
447
468
  }
448
469
  exports.to_json = to_json;
449
470
  function to_json_safely(obj, replacer) {
@@ -454,48 +475,49 @@ function to_json_safely(obj, replacer) {
454
475
  }
455
476
  exports.to_json_safely = to_json_safely;
456
477
  function is_codepoint_fullwidth(codepoint) {
457
- // Code points are derived from:
478
+ // code points are derived from:
458
479
  // http://www.unix.org/Public/UNIDATA/EastAsianWidth.txt
459
480
  return (!Number.isNaN(codepoint) &&
460
481
  codepoint >= 0x1100 &&
461
- (codepoint <= 0x115F || // Hangul Jamo
482
+ (codepoint <= 0x115f || // hangul jamo
483
+ codepoint === 0x201c || codepoint === 0x201d || //
462
484
  codepoint === 0x2026 || // …
463
- codepoint === 0x203B || // ※
464
- // Arrows
485
+ codepoint === 0x203b || // ※
486
+ // arrows
465
487
  (0x2190 <= codepoint && codepoint <= 0x21FF) ||
466
- codepoint === 0x2329 || // LEFT-POINTING ANGLE BRACKET
467
- codepoint === 0x232A || // RIGHT-POINTING ANGLE BRACKET
488
+ codepoint === 0x2329 || // left-pointing angle bracket
489
+ codepoint === 0x232a || // right-pointing angle bracket
468
490
  // ①
469
- (0x2460 <= codepoint && codepoint <= 0x24FF) ||
470
- // Box Drawing
471
- (0x2500 <= codepoint && codepoint <= 0x257F) ||
472
- // Shapes, Symbols, …
473
- (0x2580 <= codepoint && codepoint <= 0x2BEF) ||
474
- // CJK Radicals Supplement .. Enclosed CJK Letters and Months
475
- (0x2E80 <= codepoint && codepoint <= 0x3247 && codepoint !== 0x303F) ||
476
- // Enclosed CJK Letters and Months .. CJK Unified Ideographs Extension A
477
- (0x3250 <= codepoint && codepoint <= 0x4DBF) ||
478
- // CJK Unified Ideographs .. Yi Radicals
491
+ (0x2460 <= codepoint && codepoint <= 0x24ff) ||
492
+ // box drawing
493
+ (0x2500 <= codepoint && codepoint <= 0x257f) ||
494
+ // shapes, symbols, …
495
+ (0x2580 <= codepoint && codepoint <= 0x2bef) ||
496
+ // cjk radicals supplement .. enclosed cjk letters and months
497
+ (0x2e80 <= codepoint && codepoint <= 0x3247 && codepoint !== 0x303f) ||
498
+ // enclosed cjk letters and months .. cjk unified ideographs extension a
499
+ (0x3250 <= codepoint && codepoint <= 0x4dbf) ||
500
+ // cjk unified ideographs .. yi radicals
479
501
  (0x4E00 <= codepoint && codepoint <= 0xA4C6) ||
480
- // Hangul Jamo Extended-A
481
- (0xA960 <= codepoint && codepoint <= 0xA97C) ||
482
- // Hangul Syllables
483
- (0xAC00 <= codepoint && codepoint <= 0xD7A3) ||
484
- // CJK Compatibility Ideographs
485
- (0xF900 <= codepoint && codepoint <= 0xFAFF) ||
486
- // Vertical Forms
487
- (0xFE10 <= codepoint && codepoint <= 0xFE19) ||
488
- // CJK Compatibility Forms .. Small Form Variants
489
- (0xFE30 <= codepoint && codepoint <= 0xFE6B) ||
490
- // Halfwidth and Fullwidth Forms
491
- (0xFF01 <= codepoint && codepoint <= 0xFF60) ||
492
- (0xFFE0 <= codepoint && codepoint <= 0xFFE6) ||
493
- // Kana Supplement
494
- (0x1B000 <= codepoint && codepoint <= 0x1B001) ||
495
- // Enclosed Ideographic Supplement
496
- (0x1F200 <= codepoint && codepoint <= 0x1F251) ||
497
- // CJK Unified Ideographs Extension B .. Tertiary Ideographic Plane
498
- (0x20000 <= codepoint && codepoint <= 0x3FFFD)));
502
+ // hangul jamo extended-a
503
+ (0xa960 <= codepoint && codepoint <= 0xa97c) ||
504
+ // hangul syllables
505
+ (0xac00 <= codepoint && codepoint <= 0xd7a3) ||
506
+ // cjk compatibility ideographs
507
+ (0xf900 <= codepoint && codepoint <= 0xfaff) ||
508
+ // vertical forms
509
+ (0xfe10 <= codepoint && codepoint <= 0xfe19) ||
510
+ // cjk compatibility forms .. small form variants
511
+ (0xfe30 <= codepoint && codepoint <= 0xfe6b) ||
512
+ // halfwidth and fullwidth forms
513
+ (0xff01 <= codepoint && codepoint <= 0xff60) ||
514
+ (0xffe0 <= codepoint && codepoint <= 0xffe6) ||
515
+ // kana supplement
516
+ (0x1b000 <= codepoint && codepoint <= 0x1b001) ||
517
+ // enclosed ideographic supplement
518
+ (0x1f200 <= codepoint && codepoint <= 0x1f251) ||
519
+ // cjk unified ideographs extension b .. tertiary ideographic plane
520
+ (0x20000 <= codepoint && codepoint <= 0x3fffd)));
499
521
  }
500
522
  exports.is_codepoint_fullwidth = is_codepoint_fullwidth;
501
523
  //# sourceMappingURL=prototype.js.map