MCPcopy
hub / github.com/vercel-labs/wterm / handleInput

Method handleInput

packages/@wterm/just-bash/src/index.ts:76–218  ·  view source on GitHub ↗
(data: string)

Source from the content-addressed store, hash-verified

74 }
75
76 async handleInput(data: string): Promise<void> {
77 if (!this._write || this._busy) return;
78 const write = this._write;
79
80 if (data === "\t") {
81 await this._tabComplete();
82 return;
83 }
84
85 if (data === "\r") {
86 const cur = this._line;
87 this._line = "";
88 this._cursor = 0;
89 write("\r\n");
90
91 if (cur.endsWith("\\")) {
92 this._buffer += cur + "\n";
93 write("> ");
94 return;
95 }
96
97 const cmd = this._buffer + cur;
98 this._buffer = "";
99
100 if (cmd.trim() && this._bash) {
101 this._history.push(cmd);
102 this._historyPos = -1;
103 this._busy = true;
104
105 try {
106 const wrapped = `cd ${JSON.stringify(this._cwd)} && ${cmd}`;
107 const result = await this._bash.exec(wrapped);
108 if (result.stdout) {
109 write(result.stdout.replace(/\n/g, "\r\n"));
110 if (!result.stdout.endsWith("\n")) write("\r\n");
111 }
112 if (result.stderr) {
113 write(`\x1b[31m${result.stderr.replace(/\n/g, "\r\n")}\x1b[0m`);
114 if (!result.stderr.endsWith("\n")) write("\r\n");
115 }
116 const pwdResult = await this._bash.exec(
117 `cd ${JSON.stringify(this._cwd)} 2>/dev/null; ${cmd} >/dev/null 2>&1; pwd`,
118 );
119 const lines = pwdResult.stdout?.trim().split("\n") ?? [];
120 const lastLine = lines[lines.length - 1]?.trim();
121 if (lastLine?.startsWith("/")) this._cwd = lastLine;
122 } catch (err) {
123 const msg = err instanceof Error ? err.message : "Unknown error";
124 write(`\x1b[31m${msg}\x1b[0m\r\n`);
125 } finally {
126 this._busy = false;
127 }
128 }
129
130 write(this._prompt(this._cwd));
131 } else if (data === "\x7f" || data === "\b") {
132 if (this._cursor > 0) {
133 const tail = this._line.slice(this._cursor);

Callers 1

bash-shell.test.tsFile · 0.45

Calls 3

_tabCompleteMethod · 0.95
pushMethod · 0.80
writeFunction · 0.50

Tested by

no test coverage detected