-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.ts
More file actions
134 lines (111 loc) · 3.43 KB
/
Copy pathutils.ts
File metadata and controls
134 lines (111 loc) · 3.43 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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
import { statSync } from "node:fs";
import { ANSI, Commands } from "./types";
export function style(
text: string,
styles: string[],
restoreStyles: string[] = [],
): string {
if (styles.length === 0) {
return text;
}
return `${styles.join("")}${text}${ANSI.reset}${restoreStyles.join("")}`;
}
export function clearScreen(): void {
process.stderr.write(ANSI.clearScreen);
process.stderr.write(ANSI.cursorHome);
process.stderr.write(ANSI.cursorHide);
}
export function cleanUp(): void {
process.stdin.setRawMode(false);
process.stdin.removeAllListeners("data");
process.stdin.pause();
process.stderr.write(ANSI.cursorShow);
}
export function showHelpMessage(command: string): void {
switch (command) {
case Commands.CREATE:
process.stderr.write(`
Usage: stash create
Open interactive TUI to create a new file/directory
Flags:
-h, --help Show help message and exit
`);
break;
default:
process.stderr.write(`
Usage: stash [command] [query]
Run an interactive TUI to browse, search, and manage stashed files/directories.
Commands:
create Open interactive TUI to create a new file/directory
Arguments:
[query] Open interactive TUI with this query as the initial search
Options:
-h, --help Display this message and exit
Examples:
stash Browse and search all items
stash notes Browse and search with "notes" as the initial search
stash create Create a new file or directory
`);
break;
}
}
export function writeLine(text: string = ""): void {
process.stderr.write(`${text}\n`);
}
export function write(text: string): void {
process.stderr.write(text);
}
export function getTerminalSize() {
return {
rows: process.stderr.rows || process.stdout.rows || 24,
cols: process.stderr.columns || process.stdout.columns || 80,
};
}
export function isDirectory(path: string) {
return statSync(path).isDirectory();
}
/** Match ANSI CSI sequences (e.g. ESC[36m, ESC[0m) so we can measure the visible length. */
const ESC = "\u001B";
const ANSI_CSI_RE = new RegExp(`${ESC}\\[[0-9;]*[a-zA-Z]`, "g");
export function visibleLength(str: string): number {
return str.replace(ANSI_CSI_RE, "").length;
}
export function padEnd(
str: string,
length: number,
char: string = " ",
): string {
if (str.length >= length) {
return str;
}
return str + char.repeat(length - str.length);
}
/** Pads string so its visible (display) length is at least `length`. We can use this for strings that may contain ANSI codes :) */
export function padEndVisible(
str: string,
length: number,
char: string = " ",
): string {
const visible = visibleLength(str);
if (visible >= length) {
return str;
}
return str + char.repeat(length - visible);
}
export function relativeTime(date: Date) {
const diff = Date.now() - date.getTime();
const units: [number, string][] = [
[31536000000, "y"],
[2592000000, "mo"],
[86400000, "d"],
[3600000, "h"],
[60000, "m"],
[1000, "s"],
];
for (const [ms, unit] of units) {
if (diff >= ms) {
return `${Math.floor(diff / ms)}${unit} ago`;
}
}
return "just now";
}