Skip to content

Commit

Permalink
Modify ls output to be unformatted on redirect.
Browse files Browse the repository at this point in the history
`ls` now outputs unformatted, line-separated when redirecting. This aligns
closer with how typical linux systems do it.

Additionally, the `clear` command now sends a special escape sequence
that tells the terminal to clear the history. This is a bit better than
the original solution, which had no implementation for `clear`, but
instead searched for whether the input contained the word "clear".
  • Loading branch information
cbloodsworth committed May 30, 2024
1 parent 5285660 commit 7ee035a
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 13 deletions.
9 changes: 6 additions & 3 deletions data/constants.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
export const CONSTANTS = {
server: 'pearterm',
defaultTheme: 'default',
shellVersion: '0.0.1-alpha'
SERVER: 'pearterm',
DEFAULT_THEME: 'default',
SHELL_VERSION: '0.0.1-alpha',
ESCAPE_CODES: {
RESET_TERM: '\\ec'
}
}
9 changes: 5 additions & 4 deletions src/components/terminal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,12 @@ interface DisplayLine {
const Terminal: React.FC<TerminalProps> = ({ user, pwd, setPwd, viewContent, setViewContent }) => {
// Current terminal metadata
const [currentEnvironment, modifyEnvironment] = useState<TerminalEnvironment>({
server: CONSTANTS.server,
server: CONSTANTS.SERVER,
user: user,
dir: pwd.filename,
});

const [termColors, setTermColors] = useState<TerminalColors>(themes[CONSTANTS.defaultTheme]);
const [termColors, setTermColors] = useState<TerminalColors>(themes[CONSTANTS.DEFAULT_THEME]);

// For storing what the user is actively typing.
const [input, setInput] = useState("");
Expand Down Expand Up @@ -177,6 +177,7 @@ const Terminal: React.FC<TerminalProps> = ({ user, pwd, setPwd, viewContent, set
viewContent, setViewContent,
termColors, setTermColors);


const commandHistoryEntry: CommandHistoryEntry =
{ command: command, rawInput: input, environment: {...currentEnvironment} };

Expand Down Expand Up @@ -208,8 +209,8 @@ const Terminal: React.FC<TerminalProps> = ({ user, pwd, setPwd, viewContent, set

addToDisplayHistory(terminalHistoryEntry);

// bit hacky but i think this is the best way we can do this...
if (input === "clear") { clearTerminal(); }
// If the result contains the RESET_TERM escape sequence, clear the terminal.
if (result.includes(CONSTANTS.ESCAPE_CODES.RESET_TERM)) { clearTerminal(); }

setInput("");
break;
Expand Down
15 changes: 9 additions & 6 deletions src/system/implementation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,8 @@ export const evaluateCommand = (command: Command,
if (/\s/.test(displayName)) { displayName = "'"+displayName+"'"; }

// This is for coloring logic. (Directories vs files)
if (child.isDirectory) {
// Don't color if being redirected.
if (child.isDirectory && !command.redirectTo) {
displayName = termColors.primary.formatted + displayName
+ termColors.default.formatted
}
Expand All @@ -82,7 +83,11 @@ export const evaluateCommand = (command: Command,
termColors.primary.formatted + ".." + termColors.default.formatted
);

return filenames.join(' '); // add two spaces inbetween
// If redirecting, do newlines. Otherwise, optimize for human readability
const delim = command.redirectTo
? "\n"
: " "
return filenames.join(delim); // add two spaces inbetween
}
case (CommandName.pwd): { return pwd.getFilepath(); }
case (CommandName.cd): {
Expand Down Expand Up @@ -127,9 +132,7 @@ export const evaluateCommand = (command: Command,
return output;
}
case (CommandName.clear): {
// Implementation for this is currently outside of this function, in the onKeyDown event
// I couldn't think of a better way to do it ;-;
break;
return CONSTANTS.ESCAPE_CODES.RESET_TERM;
}
case (CommandName.touch): {
const new_filename = command.parameters[0];
Expand Down Expand Up @@ -233,7 +236,7 @@ export const evaluateCommand = (command: Command,
case (CommandName.help): {
if (command.parameters.length === 0) {
const cmdList = Array.from(command_map, ([_, command]) => {return command.info!.usage}).sort().join("\n ");
return `pearSH, version ${CONSTANTS.shellVersion}\n`+
return `pearSH, version ${CONSTANTS.SHELL_VERSION}\n`+
`Type 'help NAME' to find out more about the function 'NAME'.\n`+
`Alternatively, type 'NAME -h'.\n \n `+
cmdList
Expand Down

0 comments on commit 7ee035a

Please sign in to comment.