DEV Community

Cover image for How the Terminal Should Work by Default; But Doesn't
Wynter Jones
Wynter Jones

Posted on

How the Terminal Should Work by Default; But Doesn't

So we use this Terminal/Console/Command Prompt thingy every day. The first time we used it we felt like we finally hacked into the matrix.

Back Story

Even the first computer I used which was some DOS system with green text. You enter a command a some magic would happen. Except I did not know any commands, nor did I know it could process commands. So it was used a way to leave messages on screen for my mom or brother.

How it worked then and how it works now is the same.

Enter command, it prints the output and enters a new line. After a while you have a tiny scroll bar and entire history of every command you entered.

Cool, I guess for some engineers I am sure, for me? I have never needed to look through history to see anything. So ultimately it's all left over noise.

So I clear screen and continue on.

A Side Story

A few years ago I was developing a desktop application that is basically a PostgreSQL database viewer which connected to Ruby on Rails. Most of the commands needed to be entered into a terminal.

This is when I really learned why they call them terminal emulators and got to understand the shell more. This is when I could control the entire layout of the terminal.

I realized I never needed any of that history and in fact I needed to see all the output clearly and easily.

This project has since been closed however.

The Point & The Code

Here's some bashrc code that will clear the screen after each command and provide the output in a much nicer way:

preexec () {
 printf "\033c"
 echo "$fg_bold[red]╭╼$fg[blue] $PWD\n$fg_bold[red]╰───╼$fg_bold[green] $ $history[$HISTCMD] $fg_bold[red]\n"
}
preexec_invoke_exec () {
    [ -n "$COMP_LINE" ] && return                     # do nothing if completing
    [ "$BASH_COMMAND" = "$PROMPT_COMMAND" ] && return # don't cause a preexec for $PROMPT_COMMAND
    local this_command=`history 1 | sed -e "s/^[ ]*[0-9]*[ ]*//g"`; # obtain the command from the history, removing the history number at the beginning
    preexec "$this_command"
}
Enter fullscreen mode Exit fullscreen mode

Alt Text

Disclaimer

Yes, you do lose the history and it always shows just the last command at a time.

Yes, it has some fancy stuff to format the directory and last command.

Yes, you should be using Starship addon for your terminal to format the node version, etc.

Yes, I think this is the way it should work. In fact I think the input should always stay at the top of the terminal and output below, however that seems to hard to hack into a .zshrc or .bashrc script.

Either way, thanks for reading.

Top comments (1)

Collapse
 
wynterjones profile image
Wynter Jones

Terminal used -> github.com/Eugeny/terminus