How To Build a Command-Line Text Editor With Java (Part 3)
Want to know how text editors implement cursor movements and scrolling? Learn how in this video tutorial.
Join the DZone community and get the full member experience.Join For Free
- How to implement page-up and page-down functionality
- How to make the end key work properly, including cursor snapping
- How to make our text editor work on all operating systems, including macOS and Windows - not just Linux
You're in for a ride!
What’s in the Video
In the previous episode, we got vertical scrolling with the arrow keys working. Unfortunately, when you press page up or page down, nothing happens - and we need to change that! We'll use a tiny trick to simulate the page up/down functionality, mapping it to pressing the arrow up/down for the number of rows our screen has. It serves as a good initial implementation, though there are a couple of edge cases we need to iron out.
Once we have the page up and down working, it's time to care about horizontal scrolling. At the moment, our text viewer renders lines overflowing the screen, leading to heavy flickering whenever we vertically move our cursor. Ideally, we only want to render as much text as we have columns on the screen - and then we want to move the screen's contents horizontally, whenever we press the left or right keys at the beginning or end of the screen. To implement horizontal scrolling we can take most of the code for vertical scrolling, copy and paste it, and just replace a couple of key variables - done!
After horizontal scrolling, let's take care of a couple of minor editing issues: first of all, the end key. It currently makes the user jump to the end of the screen. Ideally, we'd like the end key to only jump to the end of the current line. With a couple of small changes to our
moveCursor() function, we can implement that behavior.
This opens up another problem: when we are at the end of a line and then move vertically upwards or downwards, we also want to automatically snap to the end of the new line, not just end up somewhere in the middle. So, we'll need to fix our cursor-snapping implementation.
In between, I'll leave a couple of notes for you regarding cursor line wrapping. We don't have enough time to implement it in this episode, but it would serve as a great exercise, for you, the watcher, to implement.
Last but not least, we'll need to fix a couple of issues for our macOS and Windows platform support.
The issue with macOS is that while it uses the same OS APIs as Linux, it uses different values for the OS calls. Hence, we'll need to invent an abstraction/delegation layer that detects if the current OS is macOS or Linux, and then use the corresponding, OS-specific classes.
Windows uses a completely different API to put terminals into raw mode or get the current terminal size, and we'll have to dig deep into Microsoft's API documentation to find out which Windows methods we'll need to implement on our JNA side.
That's it for today! See you in the next episode, where we'll implement searching across your text file.
Opinions expressed by DZone contributors are their own.