As an introduction to the Rust language, I decided to make a console-based text editor using a tutorial by Philipp Flenker, similar to nano or Vim. It currently offers support for viewing and editing documents that contain all ASCII characters! It also has the added feature of syntax highlighting for keywords, characters, strings, and comments.
There is so much going on under the hood of console-based text editors, but it can be broken down into a few key components:
- Entering raw mode in the terminal
- Listening for key presses and handling them accordingly
- Using OOP to abstract the idea of a file
- Rendering/editing the file by interacting with the terminal
When you use the console during the execution of a program, you may notice that the console waits for the Enter key to be pressed before processing the characters. Additionally, all the characters you type show up on the line that you are typing on and pressing Ctrl+C completely halts the execution of the program. When editing a text file, none of these features are desirable. That's where terminal raw mode comes in. It process key presses immediately, does not print characters that are pressed, and allows the programmer to handle control key processing.
To process key presses, the program listens for keyboard input and performs a command accordingly. For example, a regular character would prompt the editor to insert the key in the current position and move to the right, a control character, for example Ctrl+Q exits the text editor, etc.
A file is simply a series of lines, and each line is a series of characters. When a file is opened in the text editor, it is handled exactly as such. To modify a specific line, we can alter or append a character to a line and then write each individual line back to the file.
In raw mode, we can manually move the cursor and print individual strings to the terminal. As a result, we can simply print out the given file, line by line, then listen for presses of the arrow keys to manually move the cursor around the terminal. The user can then enter characters at any specific location in the document.
cargo run --release sample_file
You can replace sample_file with your desired file to view/edit it!