How To Build a Command-Line Text Editor With Java (Part 2)
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 FreeLet's continue building our Java-based, command-line text editor that we started here.
This is Part 2 of the "How To Build a Command-Line Text Editor With Java" series. We will cover the following:
- How to detect "complex" key presses like arrow up, arrow down, and friends
- How to move the cursor around on the screen
- How to load and display text files, as well as a first, vertical scrolling implementation
You're going to be in for a ride!
What’s in the Video
Before we can tackle cursor movements in our command-line text editor, we'll have to figure out how to detect complex keypresses, like Arrow-Up, Arrow-Down, Page-Up, etc. Why is this complex? Because pressing these keys doesn't, in fact, just send one key to your program, but three or four different ones. We'll first need to learn what these key combinations are that are being sent.
Having learned that, we'll need to figure out how to map those combinations of keys to just one single keypress. So, whenever our program is sent a combination like "esc]5~," it knows that it is sent the HOME_KEY, not four individual keys. Making this change is possible using just a couple of lines of code.
Being able to detect keypresses, we can now move on to cursor movements. First, we'll need to remember our cursor's X and Y coordinates. Moving the cursor itself, is just simple arithmetic, increasing or decreasing the X and Y coordinates as needed, and putting some boundaries in place, so the cursor cannot move off the screen. Finally, we don't really move the cursor, we just draw it in the right place, but slightly change the ANSI escape codes for cursor placements, which we learned about in the last episode.
With cursor movements in place, we can finally think about loading files. In fact, we'll load all the text files we want to display entirely into memory for now - an area we can optimize later on. It's also important to turn our text file into a list of strings, as that will make it very simple to display its content later on. Once we're able to load files, it's also time to do some much-needed refactorings, extracting a couple of methods and renaming variables so they make sense.
It's time to think about vertical scrolling! We'll work through a bit of scrolling theory, namely that we'll need to get a new variable involved, an offset. The offset specifies the line of your file, that should be displayed at the top of the screen. And scrolling "merely" means to correctly change the offset, and then make sure that all cursor movement methods and screen drawing methods take the offset into account, not just the raw screen width and height.
Even though the scrolling implementation is just a couple of lines of code, it is somewhat hard to understand, and I'll leave it to you as an exercise to really get comfortable with it. This leaves us with what's next in the series: making sure that the page up and page down keys work, proper macOS and windows support for our editor and plenty of other things that you would expect from a text editor!
See you in the next episode.
Published at DZone with permission of Marco Behler. See the original article here.
Opinions expressed by DZone contributors are their own.
Comments