最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

shell - How can I emulate Bash's command history scrolling functionality in C? - Stack Overflow

programmeradmin3浏览0评论

I'm trying to write my own terminal shell in C and want to know how I can register arrow-up and arrow-down presses to populate the current input line with a previously entered line? I'd like to do this without pressing enter.

I wrote my shell to save every entered line to a history.txt file, so I'm thinking my approach will be to have the shell take inputs like ^[[A or ^[[B and automatically replace it with the last recorded command, and somehow iterate through the lines of the history.txt file with each successive key press. Since I've read that implementation of this can be platform-specific, I'm hoping for this to work on any GNU/Linux distribution, or at least Debian 12.

Once I figure this out I'd also like to use the left and right keys to scroll through text that I've already typed, instead of adding ^[[D and ^[[C after what's been typed, and eventually add autocomplete using Tab, but for the sake of having a more specific question, for now I'm asking for help primarily with the up and down keys scrolling through previous commands the same way it works in Bash on Debian and similar Linux/POSIX systems I've used.

I haven't been able to think of a way to implement this so far, and wasn't sure what to search for to find any examples of other people implementing this behavior in their own code, I could only see discussions of people who weren't seeing this behavior as they expected to in their OS's provided shell.

发布评论

评论列表(0)

  1. 暂无评论