JonathanHelianthicusDoe / shticker_book_unwritten

Minimal CLI launcher for the Toontown Rewritten MMORPG
https://crates.io/crates/shticker_book_unwritten
GNU General Public License v3.0
13 stars 4 forks source link

up/down arrow history support #8

Open pop opened 1 week ago

pop commented 1 week ago

Hey! I really like using shticker_book_unwritten (installed via Nix). My one ask is to store a history of commands with up/down arrow to cycle through, similar to how bash/sh/zsh allow you to cycle through history.

I'm going to take a stab at implementing this, so if you have thoughts on how to best implement this, or if this is a feature you think s.b.u. should include let me know! I'm very open to feedback and would love some guidance.

JonathanHelianthicusDoe commented 1 week ago

I think this to be a useful feature for sure. I never bothered to implement it, because it would require the way that inputs are handled to be considerably less “dumb”. Right now, inputs are simply read via std::io::Stdin::read_line. This implies two important things:

If someone didn’t know any better, then they might think the former problem to be a consequence of using a function called read_line — it is in the name, after all. But the deeper reason is that stdin is typically line-buffered: unless you use an OS-specific API, your std::io::Stdin handle only ever observes full lines; specifically, of text (even if you use its Read implementation). This is, naturally, also the reason for the second problem.

So you need a way to access OS-specific APIs that allow for reading keystrokes directly. Realistically, the best way is to just depend on a crate like console. This solves the other problem that you have: