|
1 | 1 | import { proxy, ref } from "valtio"; |
2 | 2 | import { Stream, StreamSignal } from "./stream"; |
3 | 3 | import { CommandsManager } from "./commands"; |
4 | | -import { ANSI, Ansi, clamp, removeFromArray, Vector2 } from "@prozilla-os/shared"; |
| 4 | +import { ANSI, Ansi, clamp, getLongestCommonPrefix, removeFromArray, Vector2 } from "@prozilla-os/shared"; |
5 | 5 | import { EXIT_CODE, HOSTNAME, USERNAME, WELCOME_MESSAGE } from "../../constants/shell.const"; |
6 | 6 | import { App } from "../apps/app"; |
7 | 7 | import { VirtualFolder, VirtualRoot } from "../virtual-drive"; |
@@ -416,6 +416,81 @@ export class Shell { |
416 | 416 | this.updatePrefix(); |
417 | 417 | } |
418 | 418 |
|
| 419 | + /** |
| 420 | + * Provides completions based on the current input value. |
| 421 | + */ |
| 422 | + getCompletions() { |
| 423 | + const words = this.state.inputValue.split(" "); |
| 424 | + const lastWord = words[words.length - 1] ?? ""; |
| 425 | + const isFirstWord = words.length <= 1; |
| 426 | + |
| 427 | + let completions: string[] = []; |
| 428 | + |
| 429 | + if (isFirstWord && !lastWord.includes("/")) { |
| 430 | + completions = CommandsManager.COMMANDS |
| 431 | + .filter((command) => command.name.startsWith(lastWord)) |
| 432 | + .map((command) => command.name); |
| 433 | + } |
| 434 | + |
| 435 | + if (!isFirstWord || lastWord.includes("/") || lastWord.startsWith(".")) { |
| 436 | + const pathParts = lastWord.split("/"); |
| 437 | + const searchTerm = pathParts.pop() ?? ""; |
| 438 | + const path = pathParts.join("/") || (lastWord.startsWith("/") ? "/" : "."); |
| 439 | + |
| 440 | + const directory = this.state.workingDirectory.navigateToFolder(path); |
| 441 | + if (directory) { |
| 442 | + const entries = [...directory.getSubFolders(true), ...directory.getFiles(true)]; |
| 443 | + const pathCompletions = entries |
| 444 | + .map((entry) => entry.name + (entry.isFolder() ? "/" : "")) |
| 445 | + .filter((name) => name.startsWith(searchTerm)); |
| 446 | + |
| 447 | + completions = [...completions, ...pathCompletions]; |
| 448 | + } |
| 449 | + } |
| 450 | + |
| 451 | + return completions; |
| 452 | + } |
| 453 | + |
| 454 | + /** |
| 455 | + * Auto-completes the current input value or displays possible completions. |
| 456 | + */ |
| 457 | + autoComplete() { |
| 458 | + const completions = this.getCompletions(); |
| 459 | + if (!completions.length) return; |
| 460 | + |
| 461 | + const parts = this.state.inputValue.split(" "); |
| 462 | + const lastWord = parts.pop() ?? ""; |
| 463 | + const commonPrefix = getLongestCommonPrefix(completions); |
| 464 | + |
| 465 | + // If there's a common prefix longer than the current input, complete it |
| 466 | + const pathParts = lastWord.split("/"); |
| 467 | + const searchTerm = pathParts.pop() ?? ""; |
| 468 | + |
| 469 | + if (commonPrefix.length > searchTerm.length) { |
| 470 | + pathParts.push(commonPrefix); |
| 471 | + parts.push(pathParts.join("/")); |
| 472 | + this.setInputValue(parts.join(" ")); |
| 473 | + return; |
| 474 | + } |
| 475 | + |
| 476 | + // Otherwise, handle single match or multiple options |
| 477 | + if (completions.length === 1) { |
| 478 | + pathParts.push(completions[0]); |
| 479 | + parts.push(pathParts.join("/")); |
| 480 | + this.setInputValue(parts.join(" ")); |
| 481 | + } else { |
| 482 | + this.pushHistory({ |
| 483 | + text: this.state.prefix + this.state.inputValue, |
| 484 | + isInput: true, |
| 485 | + value: this.state.inputValue, |
| 486 | + }); |
| 487 | + this.pushHistory({ |
| 488 | + text: completions.join(" "), |
| 489 | + isInput: false, |
| 490 | + }); |
| 491 | + } |
| 492 | + } |
| 493 | + |
419 | 494 | static parseCommand(input: string): string[] { |
420 | 495 | return input.match(/(?:[^\s"']+|"(?:[^"\\]|\\.)*"|'[^']*')+/g) ?? []; |
421 | 496 | } |
|
0 commit comments