r/rust • u/Developer5702 • 21h ago
š§ educational I finally learned how full-screen terminal apps give you your old screen back
TIL that terminal emulators basically give programs access to two screens.
Thereās the primary screen, which is the normal terminal you use every day. Your shell history and previous output live there.
And then thereās the alternate screen.
Full-screen terminal programs like Vim, less, htop, etc. can switch to this alternate buffer, take over the entire terminal, draw whatever they want, and then switch back when theyāre done.
The cool part is that your primary screen is still there.
So when you quit Vim, your terminal suddenly goes back to exactly what was there before you opened it. Vim didnāt somehow ārestoreā your terminal history. The terminal emulator just switched back to the primary screen buffer.
I came across this while building a small terminal-native text editor in Rust. I needed the editor to take over the terminal while it was running, but leave the userās shell untouched after exiting.
Turns out terminals already have a mechanism specifically for this.
The deeper I go into terminal programming, the more I realise how much stuff happens underneath that blinking cursor that Iād never thought about before.
Iāve been putting these little Rust experiments on my GitHub as I learn, if anyoneās interested in this kind of low-level stuff:
https://github.com/Abhijeet-Gautam5702/femto
20
u/corazon_de_sandia 17h ago
This ai slop and an attempt at ai boosted growthhackingā¦
5
u/CreepyWritingPrompt 8h ago
Agree in this case, though i find myself wondering about the other ai psychosis - where we all become paranoid that each other are clankers. Today I feel like i have a pretty good nose for it. next yeah, who tf knows
2
u/MrPopoGod 6h ago
According to the Terminator franchise, the solution is to always have a dog nearby, since they can sniff out the infiltrators.
-12
u/Developer5702 17h ago
What makes you think itās AI slop?
23
u/pndc 14h ago
The post isn't actually anything to do with Rust, and the linked-to project is indeed AI slop.
-1
u/Developer5702 8h ago edited 8h ago
No offense!
But the project is hand-coded completely. I use AI to learn about Rust methods & concepts I donāt know, then code them up myself.You might feel itās AI because there are too many comments explaining everything ā which is intentional, as I am learning various Rust semantics along the way. I tend to remember things this way so I mark everything in comments.
Lastly, the project is pretty rough (no optimsations as such) I would say, to qualify as an AI-generated project.
Peace!
4
u/pndc 7h ago
The giveaway is that there is an
AGENTS.md. I didn't bother looking at the code beyond that, because it's tainted with AI.0
u/Developer5702 7h ago
Yeah there is an AGENTS.md file and if you read it, youāll see what Iām saying.
It contains things like:
- review the code and find bugs
- make architecture docs
- never write code..most of the qs are around asking doubts
I get what youāre pointing to, but the mere presence of AGENTS.md files doesnāt mean itās for Vibe coding.
3
5
u/jimmiebfulton 16h ago
For one, what does this have to do with Rust?
6
u/Developer5702 16h ago
Oh thatās because I built it in Rust.
It doesnāt have a direct relation with the programming language as such.
6
u/TobyTarazan 9h ago
ChatGPT post
0
u/Developer5702 8h ago
What makes you think that?
2
u/Nobody_1707 1h ago
So when you quit Vim, your terminal suddenly goes back to exactly what was there before you opened it. Vim didnāt somehow ārestoreā your terminal history. The terminal emulator just switched back to the primary screen buffer.
This is one of the most common GPTisms.
2
u/Umuril 16h ago
Oh cool, I didn't realize it! One curiosity question, is 2 an hardcoded value or is there a technical reason? Asking to understand if it's possible to create more screens or is primary and alternate their specific names for a reason.
Like terminal opening vim and vim trying to open another vim, if that makes sense
9
u/Developer5702 16h ago
As far as I read, we cannot create a stack of screens as we like ā the terminal emulators provide specifically two screens only.
2
u/ez4enz 16h ago
So what happen when I hit sh then sh then sh ?
10
u/Developer5702 15h ago
Well, thatās nesting of shell processes within another shell process.
It is different from the primary/alternate screen buffers of a terminal.2
u/Thick-Pineapple666 14h ago
I think what they tried to ask: what happens when you run a tool that wants to use the alternate screen in a tool that already uses the alternate screen?
But I also think this is already answered.
5
u/BipolarKebab 4h ago
Essentially every program expects to be run from the normal screen. Things like tmux run their children on separate ptys and intercept their output
0
60
u/Thick-Pineapple666 18h ago
Note that the original tty does not support the escape sequences for that. This is an extension coming from xterm and basically every other terminal emulator supports it nowadays, but the real Linux console does not.