Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 53 additions & 8 deletions src/components/Terminal.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ const Terminal = ({ isOpen, onClose }) => {

let input = '';
let currentDir = 'home';
let cursorPos = 0;
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The cursorPos variable should be reset to 0 when input is cleared (after Enter key is pressed) to maintain proper cursor positioning for new commands.

Copilot uses AI. Check for mistakes.

term.onKey(({ key, domEvent }) => {
if (domEvent.key === 'Enter') {
term.write('\r\n');
Expand All @@ -107,10 +109,18 @@ const Terminal = ({ isOpen, onClose }) => {
input = '';
setTimeout(() => printPrompt(term, currentDir), 10);
} else if (domEvent.key === 'Backspace') {
if (input.length > 0) {
input = input.slice(0, -1);
term.write('\b \b');
}
if (cursorPos > 0) {
input = input.slice(0, cursorPos - 1) + input.slice(cursorPos);
cursorPos--;

term.write('\x1b[2K\r');
printPrompt(term, currentDir);
term.write(input);
const moveLeft = input.length - cursorPos;
if (moveLeft > 0) {
term.write(`\x1b[${moveLeft}D`);
}
}
} else if (domEvent.key === 'Tab') {
domEvent.preventDefault();
const completed = handleTabCompletion(input, currentDir);
Expand All @@ -121,10 +131,45 @@ const Terminal = ({ isOpen, onClose }) => {
input = completed;
term.write(input);
}
} else if (domEvent.key.length === 1) {
input += key;
term.write(key);
}
else if (domEvent.key.length === 1) {
input = input.slice(0, cursorPos) + key + input.slice(cursorPos);
cursorPos++;

term.write('\x1b[K');
term.write(input.slice(cursorPos - 1));

const charsToMoveBack = input.length - cursorPos;
if (charsToMoveBack > 0) {
term.write(`\x1b[${charsToMoveBack}D`);
}
}
else if(domEvent.key == 'ArrowLeft'){
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use strict equality (===) instead of loose equality (==) for comparison. This prevents type coercion issues and follows JavaScript best practices.

Suggested change
else if(domEvent.key == 'ArrowLeft'){
else if(domEvent.key === 'ArrowLeft'){

Copilot uses AI. Check for mistakes.
if(cursorPos > 0){
cursorPos--;
term.write('\x1b[D');
}
}
else if(domEvent.key == 'ArrowRight'){
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use strict equality (===) instead of loose equality (==) for comparison. This prevents type coercion issues and follows JavaScript best practices.

Suggested change
else if(domEvent.key == 'ArrowRight'){
else if(domEvent.key === 'ArrowRight'){

Copilot uses AI. Check for mistakes.
if(cursorPos < input.length){
cursorPos++;
term.write('\x1b[C');
}
}
Comment on lines +147 to 158
Copy link

Copilot AI Sep 24, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Inconsistent spacing around parentheses and braces. Add spaces after 'if' and around parentheses for consistency with the rest of the codebase.

Copilot uses AI. Check for mistakes.
else if (domEvent.key === ' ') {
input = input.slice(0, cursorPos) + ' ' + input.slice(cursorPos);
cursorPos++;

term.write('\x1b[K');

term.write(input.slice(cursorPos - 1));

const charsToMoveBack = input.length - cursorPos;
if (charsToMoveBack > 0) {
term.write(`\x1b[${charsToMoveBack}D`);
}
}

});
}

Expand Down Expand Up @@ -401,4 +446,4 @@ const Terminal = ({ isOpen, onClose }) => {
);
};

export default Terminal;
export default Terminal;