hopwire 0.4.0 → 0.5.0

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.
@@ -0,0 +1,182 @@
1
+ /**
2
+ * Text as the terminal shows it: how many columns it takes, cut and padded to a width, with the escape sequences for
3
+ * colour and links left whole. Everything the full-screen view draws passes through here, so a wide character or an
4
+ * emoji in a path or a body cannot push a line out of its place.
5
+ */
6
+ const RESET = '\u001b[0m';
7
+ const LINK_END = '\u001b]8;;\u001b\\';
8
+ /** A colour or style sequence, or the start or end of a link. */
9
+ // eslint-disable-next-line no-control-regex -- escape sequences start with ESC
10
+ const ESCAPE = /\u001b\[[0-9;?]*[A-Za-z]|\u001b\]8;;[^\u001b\u0007]*(?:\u001b\\|\u0007)/y;
11
+ const graphemes = new Intl.Segmenter(undefined, { granularity: 'grapheme' });
12
+ /** Ranges of code points a terminal draws two columns wide (East Asian wide and fullwidth, emoji). */
13
+ const WIDE = [
14
+ [0x1100, 0x115f],
15
+ [0x231a, 0x231b],
16
+ [0x23e9, 0x23ec],
17
+ [0x2e80, 0x303e],
18
+ [0x3041, 0x33ff],
19
+ [0x3400, 0x4dbf],
20
+ [0x4e00, 0x9fff],
21
+ [0xa000, 0xa4cf],
22
+ [0xa960, 0xa97f],
23
+ [0xac00, 0xd7a3],
24
+ [0xf900, 0xfaff],
25
+ [0xfe30, 0xfe4f],
26
+ [0xff00, 0xff60],
27
+ [0xffe0, 0xffe6],
28
+ [0x1f300, 0x1f64f],
29
+ [0x1f680, 0x1f6ff],
30
+ [0x1f900, 0x1f9ff],
31
+ [0x1fa70, 0x1faff],
32
+ [0x20000, 0x3fffd],
33
+ ];
34
+ /** Columns of one grapheme: 0 for what does not advance the cursor, 2 for wide characters and emoji, else 1. */
35
+ export function graphemeWidth(grapheme) {
36
+ const code = grapheme.codePointAt(0) ?? 0;
37
+ if (code < 0x20 || (code >= 0x7f && code < 0xa0)) {
38
+ return 0;
39
+ }
40
+ if (/^[\p{Mn}\p{Me}\p{Cf}]/u.test(grapheme)) {
41
+ return 0;
42
+ }
43
+ // A character with the emoji variation selector is drawn as an emoji, two columns wide
44
+ if (grapheme.includes('️')) {
45
+ return 2;
46
+ }
47
+ return WIDE.some(([from, to]) => code >= from && code <= to) ? 2 : 1;
48
+ }
49
+ /** A text in pieces: escape sequences, and graphemes with their width. */
50
+ function* pieces(text) {
51
+ let index = 0;
52
+ let run = '';
53
+ const flush = function* () {
54
+ if (run !== '') {
55
+ for (const { segment } of graphemes.segment(run)) {
56
+ yield { grapheme: segment, width: graphemeWidth(segment) };
57
+ }
58
+ run = '';
59
+ }
60
+ };
61
+ while (index < text.length) {
62
+ if (text.charCodeAt(index) === 0x1b) {
63
+ ESCAPE.lastIndex = index;
64
+ const match = ESCAPE.exec(text);
65
+ if (match !== null) {
66
+ yield* flush();
67
+ yield { escape: match[0] };
68
+ index += match[0].length;
69
+ continue;
70
+ }
71
+ }
72
+ run += text[index];
73
+ index += 1;
74
+ }
75
+ yield* flush();
76
+ }
77
+ /** Columns a text takes on the terminal; escape sequences take none. */
78
+ export function width(text) {
79
+ let columns = 0;
80
+ for (const piece of pieces(text)) {
81
+ columns += 'escape' in piece ? 0 : piece.width;
82
+ }
83
+ return columns;
84
+ }
85
+ /** The text without its escape sequences. */
86
+ export function plain(text) {
87
+ let result = '';
88
+ for (const piece of pieces(text)) {
89
+ result += 'escape' in piece ? '' : piece.grapheme;
90
+ }
91
+ return result;
92
+ }
93
+ /**
94
+ * The first `max` columns of a text. What is cut ends in an ellipsis; colours and a link that were open are closed, so
95
+ * nothing runs on into what is drawn after it.
96
+ */
97
+ export function clip(text, max, ellipsis = true) {
98
+ if (max <= 0) {
99
+ return '';
100
+ }
101
+ if (width(text) <= max) {
102
+ return text;
103
+ }
104
+ const room = ellipsis ? max - 1 : max;
105
+ let result = '';
106
+ let columns = 0;
107
+ let styled = false;
108
+ for (const piece of pieces(text)) {
109
+ if ('escape' in piece) {
110
+ result += piece.escape;
111
+ styled = true;
112
+ }
113
+ else if (columns + piece.width > room) {
114
+ break;
115
+ }
116
+ else {
117
+ result += piece.grapheme;
118
+ columns += piece.width;
119
+ }
120
+ }
121
+ return `${result}${ellipsis ? '…' : ''}${styled ? `${RESET}${LINK_END}` : ''}`;
122
+ }
123
+ /** A text cut or padded with spaces to exactly `columns`. */
124
+ export function fit(text, columns) {
125
+ const cut = clip(text, columns);
126
+ return `${cut}${' '.repeat(Math.max(columns - width(cut), 0))}`;
127
+ }
128
+ /** What is left of a text after its first `columns` columns, in the colours that were set until there. */
129
+ export function after(text, columns) {
130
+ let skipped = 0;
131
+ let styles = '';
132
+ let rest = '';
133
+ for (const piece of pieces(text)) {
134
+ if (skipped >= columns) {
135
+ rest += 'escape' in piece ? piece.escape : piece.grapheme;
136
+ }
137
+ else if ('escape' in piece) {
138
+ styles = piece.escape === RESET ? '' : `${styles}${piece.escape}`;
139
+ }
140
+ else {
141
+ skipped += piece.width;
142
+ // A wide character cut in half leaves a space
143
+ if (skipped > columns) {
144
+ rest += ' ';
145
+ }
146
+ }
147
+ }
148
+ return `${styles}${rest}`;
149
+ }
150
+ /** A plain text in lines of at most `columns` columns, cut wherever the line is full. */
151
+ export function wrapHard(text, columns) {
152
+ if (columns <= 0) {
153
+ return [text];
154
+ }
155
+ const lines = [];
156
+ let line = '';
157
+ let used = 0;
158
+ for (const { segment } of graphemes.segment(text)) {
159
+ const wide = graphemeWidth(segment);
160
+ if (used + wide > columns && line !== '') {
161
+ lines.push(line);
162
+ line = '';
163
+ used = 0;
164
+ }
165
+ line += segment;
166
+ used += wide;
167
+ }
168
+ lines.push(line);
169
+ return lines;
170
+ }
171
+ /** A line on a background: the background is set again after every reset inside the text. */
172
+ export function onBackground(text, background) {
173
+ const start = `\u001b[${background}m`;
174
+ return `${start}${text.replaceAll(RESET, `${RESET}${start}`)}${RESET}`;
175
+ }
176
+ /** Two texts on one line: the left one cut so that the right one always fits, spaces between them. */
177
+ export function sides(left, right, columns) {
178
+ const rightWidth = Math.min(width(right), columns);
179
+ const shownRight = clip(right, rightWidth);
180
+ const room = columns - rightWidth - (rightWidth > 0 ? 1 : 0);
181
+ return `${fit(left, Math.max(room, 0))}${rightWidth > 0 && room >= 0 ? ' ' : ''}${shownRight}`;
182
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "hopwire",
3
- "version": "0.4.0",
3
+ "version": "0.5.0",
4
4
  "mcpName": "to.hopwire/hopwire",
5
5
  "description": "Record, share and replay the HTTP traffic of your local services: private P2P sharing with teammates, and public HTTPS URLs.",
6
6
  "license": "Apache-2.0",