DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Newsletter
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Exploring Exciting New Features in Java 17 With Examples
  • Generics in Java and Their Implementation
  • How To Build a Command-Line Text Editor With Java (Part 3)
  • How To Build a Command-Line Text Editor With Java (Part 1)

Trending

  • Beyond @Transactional: Solving the Dual-Write Problem in Distributed Microservices
  • Time to First Token: How Long Does It Take for a Large Model to Start Responding?
  • You Don't Have to Pick One: A Hybrid Approach to Enterprise Data Refresh
  • One Database, Two Models: Building AI-Friendly Java Applications With MySQL JSON Duality Views

Draft This article hasn't been published, so readers can't see it.

In moderation This article is waiting for review, so readers can't see it yet.

Deleted This article has been deleted, so readers can't see it.

  1. DZone
  2. Coding
  3. Java
  4. How To Build a Command-Line Text Editor With Java (Part 2)

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.

By 
Marco Behler
·
Sep. 20, 22 · Tutorial
Post on X (opens in a new tab) Share on LinkedIn (opens in a new tab)
6.0K Views

Join the DZone community Save articles, join the discussion, and download Refcards and Trend Reports with a free account.

Join for free Already a member? Log in

Let'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.


IT Implementation Text editor Text file Build (game engine) Command (computing) Java (programming language) Memory (storage engine) SENT (protocol) Strings

Published at DZone with permission of Marco Behler. See the original article here.

Opinions expressed by DZone contributors are their own.

Related

  • Exploring Exciting New Features in Java 17 With Examples
  • Generics in Java and Their Implementation
  • How To Build a Command-Line Text Editor With Java (Part 3)
  • How To Build a Command-Line Text Editor With Java (Part 1)

Partner Resources

×

Comments

Loading comments

The comments didn't load

Please try again in a moment.

Likes

Loading

The next likes didn't load. Please try again.

The likes didn't load

Please try again in a moment.

See who liked this

Log in or join DZone for free to see who liked this article.

Log in Join for free
  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook