-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathautocomplete.ts
More file actions
232 lines (201 loc) · 7.11 KB
/
Copy pathautocomplete.ts
File metadata and controls
232 lines (201 loc) · 7.11 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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
import { invoke } from '@tauri-apps/api/core';
// Common shell commands for autocomplete
const COMMON_COMMANDS = [
'ls', 'cd', 'mkdir', 'rmdir', 'touch', 'rm', 'cp', 'mv',
'cat', 'echo', 'grep', 'find', 'ps', 'kill', 'sudo',
'chmod', 'chown', 'df', 'du', 'tar', 'zip', 'unzip',
'ssh', 'scp', 'ping', 'wget', 'curl', 'apt', 'apt-get',
'yum', 'dnf', 'pacman', 'brew', 'git', 'npm', 'yarn',
'node', 'python', 'python3', 'pip', 'pip3', 'java', 'javac', 'jar',
'docker', 'kubectl', 'systemctl', 'journalctl', 'clear', 'history',
'man', 'help', 'exit', 'perl', 'ruby', 'go', 'rustc', 'cargo',
'make', 'cmake', 'gcc', 'g++', 'clang', 'nvim', 'vim', 'nano',
'top', 'htop', 'screen', 'tmux', 'service', 'ifconfig', 'ip',
'whoami', 'env', 'export', 'alias', 'unalias', 'which', 'whereis',
'locate', 'mount', 'umount', 'df', 'free', 'uptime', 'date', 'cal',
'passwd', 'su', 'adduser', 'useradd', 'deluser', 'userdel', 'groupadd',
'groups', 'hostname', 'uname', 'lsblk', 'fdisk', 'parted', 'dd',
'xargs', 'awk', 'sed', 'sort', 'uniq', 'cut', 'tr', 'tee', 'less', 'more'
];
let cachedCurrentDir: string | null = null;
let cachedHomeDir: string | null = null;
/**
* Refresh cached current directory after `cd` command
*/
export async function refreshCurrentDir(): Promise<string> {
try {
cachedCurrentDir = await invoke<string>('get_current_dir');
return cachedCurrentDir;
} catch (error) {
return cachedCurrentDir || '';
}
}
/**
* Get the user's current directory
*/
async function getCurrentDirectory(): Promise<string> {
if (cachedCurrentDir) return cachedCurrentDir
return await refreshCurrentDir();
}
/**
* Get the user's home directory
*/
async function getHomeDirectory(): Promise<string> {
if (cachedHomeDir) return cachedHomeDir;
try {
cachedHomeDir = await invoke<string>('get_home_dir');
return cachedHomeDir;
} catch {
return '';
}
}
/**
* Expands paths with ~ to use the home directory
*/
async function expandPath(path: string): Promise<string> {
if (path.startsWith('~')) {
const home = await getHomeDirectory();
return path.replace(/^~/, home);
}
return path;
}
/**
* Parse the input to extract path components
*/
function parsePathInput(input: string): { dirToSearch: string, prefix: string, searchPattern: string } {
if (input.startsWith('/')) {
const lastSlashIndex = input.lastIndexOf('/');
if (lastSlashIndex === 0) {
return {
dirToSearch: '/',
prefix: '/',
searchPattern: input.substring(1).toLowerCase()
};
} else {
return {
dirToSearch: input.substring(0, lastSlashIndex),
prefix: input.substring(0, lastSlashIndex + 1),
searchPattern: input.substring(lastSlashIndex + 1).toLowerCase()
};
}
}
else if (input.includes('/')) {
const lastSlashIndex = input.lastIndexOf('/');
return {
dirToSearch: input.substring(0, lastSlashIndex) || '.',
prefix: input.substring(0, lastSlashIndex + 1),
searchPattern: input.substring(lastSlashIndex + 1).toLowerCase()
};
}
else {
return {
dirToSearch: '.',
prefix: '',
searchPattern: input.toLowerCase()
};
}
}
export interface AutocompleteResult {
suggestions: string[];
replacement?: string;
commonPrefix?: string;
}
/**
* Find common prefix among a list of strings
*/
function findCommonPrefix(strings: string[]): string {
if (strings.length === 0) return '';
if (strings.length === 1) return strings[0];
let prefix = strings[0];
for (let i = 1; i < strings.length; i++) {
let j = 0;
while (
j < prefix.length &&
j < strings[i].length &&
prefix[j].toLowerCase() === strings[i][j].toLowerCase()
) {
j++;
}
prefix = prefix.substring(0, j);
if (prefix === '') break;
}
return prefix;
}
/**
* Get autocompletion suggestions for the current input
*/
export async function getAutocompleteSuggestions(input: string): Promise<AutocompleteResult> {
if (!input.trim()) {
return { suggestions: [] };
}
const currentDir = await getCurrentDirectory();
const words = input.split(' ');
const lastWord = words[words.length - 1];
const isFirstWord = words.length === 1;
const beforeLastWord = input.substring(0, input.length - lastWord.length);
// Special case: command with space at the end - suggest files in current directory
if (input.endsWith(' ')) {
const files = await invoke<string[]>('list_directory_contents', { path: currentDir });
return { suggestions: files };
}
if (isFirstWord) {
const matchingCommands = COMMON_COMMANDS.filter(cmd =>
cmd.toLowerCase().startsWith(lastWord.toLowerCase())
);
if (matchingCommands.length > 0) {
if (matchingCommands.length === 1) {
return {
suggestions: matchingCommands,
replacement: matchingCommands[0]
};
}
const commonPrefix = findCommonPrefix(matchingCommands);
if (commonPrefix.length > lastWord.length) {
return {
suggestions: matchingCommands,
commonPrefix: commonPrefix
};
}
return { suggestions: matchingCommands };
}
}
// File path completion
const { dirToSearch, prefix, searchPattern } = parsePathInput(lastWord);
try {
let dirPath = dirToSearch;
if (dirPath === '.') {
dirPath = await getCurrentDirectory();
}
if (dirPath.startsWith('~')) {
dirPath = await expandPath(dirPath);
}
const files = await invoke<string[]>('list_directory_contents', { path: dirPath });
const matchingFiles = files.filter(file => {
const fileName = file.includes('/') ?
file.substring(file.lastIndexOf('/') + 1) : file;
const cleanName = fileName.endsWith('/') || fileName.endsWith('*') ?
fileName.slice(0, -1) : fileName;
return cleanName.toLowerCase().startsWith(searchPattern);
});
const completions = matchingFiles.map(file => prefix + file);
if (completions.length === 1) {
return {
suggestions: completions,
replacement: beforeLastWord + completions[0]
};
}
if (completions.length > 1) {
const commonPrefix = findCommonPrefix(completions);
if (commonPrefix.length > prefix.length) {
return {
suggestions: completions,
commonPrefix: beforeLastWord + commonPrefix
};
}
return { suggestions: completions };
}
} catch (error) {
console.error('Error completing file path:', error);
}
return { suggestions: [] };
}