Skip to content

Commit d4b0850

Browse files
committed
builtin completion
1 parent c7a467a commit d4b0850

File tree

4 files changed

+27
-51
lines changed

4 files changed

+27
-51
lines changed

app/EchoCommand.js

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,11 @@ class EchoCommand extends Command {
1111
// Only write to stdout, never to stderr
1212
if (!this.outputHandler.isStderr) {
1313
this.outputHandler.write(output, true);
14-
} else {
14+
} else if (this.outputHandler.isStderr && args.length > 0) {
1515
// If stderr and redirection file and there is an error, write to ouput file
16-
if(args.length > 0) {
17-
process.stdout.write(output + '\n');
18-
} else {
19-
this.outputHandler.writeError(output, true);
20-
}
16+
process.stdout.write(output + '\n');
17+
} else {
18+
this.outputHandler.writeError(output, true);
2119
}
2220
return { shouldContinue: true };
2321
}

app/OutputHandler.js

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,11 @@ const fs = require('fs');
22
const path = require('path');
33

44
class OutputHandler {
5-
constructor(outputFile = null, isStderr = false, append = false) {
5+
constructor(outputFile = null, isStderr = false, append = false, writeToFile = false) {
66
this.outputFile = outputFile;
77
this.isStderr = isStderr;
88
this.append = append;
9+
this.writeToFile = writeToFile;
910
this.fileInitialized = false;
1011
}
1112

@@ -35,20 +36,18 @@ class OutputHandler {
3536
}
3637

3738
write(message, newLine = true) {
38-
if (this.outputFile && this.append) {
39-
try {
40-
// Ensure file is initialized first
41-
this._initializeFile();
42-
43-
// Only write if initialization succeeded
44-
if (this.fileInitialized) {
45-
const content = newLine ? message + '\n' : message;
46-
fs.appendFileSync(this.outputFile, content);
47-
} else {
48-
// Fall back to console
49-
process.stdout.write(newLine ? message + '\n' : message);
50-
}
51-
} catch (error) {
39+
if (this.outputFile) {
40+
// Ensure file is initialized first
41+
this._initializeFile();
42+
43+
// Only write if initialization succeeded
44+
if (this.fileInitialized && this.append) {
45+
const content = newLine ? message + '\n' : message;
46+
fs.appendFileSync(this.outputFile, content);
47+
} else if (this.fileInitialized && this.writeToFile) {
48+
// If not appending, clear the file
49+
fs.writeFileSync(this.outputFile, message + '\n');
50+
} else {
5251
// Fall back to console
5352
process.stdout.write(newLine ? message + '\n' : message);
5453
}
@@ -72,14 +71,16 @@ class OutputHandler {
7271
this._initializeFile();
7372

7473
// Only write if initialization succeeded
75-
if (this.fileInitialized) {
74+
if (this.fileInitialized && this.append) {
7675
const content = newLine ? message + '\n' : message;
7776
fs.appendFileSync(this.outputFile, content);
77+
} else if (this.fileInitialized && this.writeToFile) {
78+
// If not appending, clear the file
79+
fs.writeFileSync(this.outputFile, message + '\n');
7880
} else {
7981
// Fall back to stderr console
8082
process.stderr.write(newLine ? message + '\n' : message);
8183
}
82-
8384
} else {
8485
if(this.outputFile) {
8586
this._initializeFile();

app/main.js

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -145,44 +145,26 @@ class Shell {
145145

146146
async parseCommand(input) {
147147
const { commandName, args, redirection } = this.getCommandNameAndArgs(input);
148-
149-
// if (redirection) {
150-
// // Handle redirections to /tmp directories
151-
// if (redirection.file.startsWith('/tmp/') && redirection.operator === '>>') {
152-
// const dirPath = path.dirname(redirection.file);
153-
// const fs = require('fs');
154-
155-
// // Create parent directories and touch the file
156-
// try {
157-
// fs.mkdirSync(dirPath, { recursive: true });
158-
159-
// // Create empty file if it doesn't exist (like 'touch')
160-
// if (!fs.existsSync(redirection.file)) {
161-
// fs.writeFileSync(redirection.file, '');
162-
// }
163-
// } catch (err) {
164-
// // Silently continue if we can't create the file
165-
// }
166-
// }
167-
// }
168148

169149
// Check if it's a builtin command
170150
const command = this.commandRegistry.getCommand(commandName);
171151
if (command) {
172152
if (redirection) {
173153
const append = redirection?.operator === '2>>' || redirection?.operator === '1>>' || redirection?.operator === '>>';
154+
const writeToFile = redirection?.operator === '1>' || redirection?.operator === '2>';
174155
const isStderr = redirection?.operator.startsWith('2');
175156
const outputHandler = redirection
176-
? new OutputHandler(redirection.file, isStderr, append)
157+
? new OutputHandler(redirection.file, isStderr, append, writeToFile)
177158
: this.outputHandler;
178159
command.outputHandler = outputHandler; // Assign the output handler to the command
179160
}
180161
return { command, args };
181162
} else if (this.commandRegistry.getCommandType(commandName) === CommandRegistry.COMMAND_TYPE.EXTERNAL) {
182163
const append = redirection?.operator === '2>>' || redirection?.operator === '1>>' || redirection?.operator === '>>';
164+
const writeToFile = redirection?.operator === '1>' || redirection?.operator === '2>';
183165
const isStderr = redirection?.operator.startsWith('2');
184166
const outputHandler = redirection
185-
? new OutputHandler(redirection.file, isStderr, append)
167+
? new OutputHandler(redirection.file, isStderr, append, writeToFile)
186168
: this.outputHandler;
187169

188170
const externalCommand = this.commandRegistry.createExternalCommand(

foo.md

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1 @@
1-
2-
davefelkf
3-
cat: nonexistent: No such file or directory
4-
ls: nonexistent: No such file or directory
5-
fkdjf
6-
ls: nonexistent: No such file or directory
1+
jdfl

0 commit comments

Comments
 (0)