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
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Coding
  3. Java
  4. How To Build a Command-Line Text Editor With Java (Part 1)

How To Build a Command-Line Text Editor With Java (Part 1)

Want to know how text editors work, how shell scripts change terminal text colors, update lines without scrolling, or move the cursor around? Here, learn how.

Marco Behler user avatar by
Marco Behler
CORE ·
Sep. 09, 22 · Tutorial
Like (3)
Save
Tweet
Share
11.65K Views

Join the DZone community and get the full member experience.

Join For Free

Have you ever wanted to know how text editors work, or how shell scripts change terminal text colors, update lines without scrolling, or move the cursor around? Surprise, surprise: even as Java devs, we can do this! 

In this series, I'll walk you through building a terminal-based text editor with Java. 

In part 1, learn how to use ANSI escape codes and native C libraries to do fancy text output with Java, interact with your terminal, and build a file viewer skeleton.

What’s in the Video

We'll begin the video by explaining the general project setup. In fact, the only thing you need for our text editor to work is to have Java installed. We'll start with a blank Java class, and from there on will work our way towards a working file viewer skeleton.

As a quick note, the video focuses on Unix/macOS terminals. Windows support is something that will be added in a future episode.

To understand how text editors work, you'll first need to understand what ANSI escape codes are. They are specific strings you output to your terminal that let you delete the screen, single rows, position the cursor, change your text's color, and much more. Even better, all modern terminals understand them by default.

Up next is understanding how to set your terminal into "raw mode." By default, your terminal processes text line-by-line, echoing every character that we type and much more. We don't want all of that. However, to enter raw mode, we'll need to learn about native Unix APIs and how to access them with Java.

One such API is the Termios API. It effectively lets you set your terminal into raw mode; hence, I'll show you how to use JNA to access the Termios API — and do a fair amount of bit-wise ANDs to get your terminal into the right mode.

With ANSI escape codes and terminal raw mode knowledge under our belts, we can finally create our terminal editor skeleton. When we start the editor, we want to see a clear screen. In addition, all empty rows should start with a "~" sign, and we should also have a nice little status bar at the bottom of the screen — time for implementation!

To properly implement the status bar and "~" signs, however, we need information on how many rows and columns our terminal window actually has. You cannot get that information with the Termios API, hence we need to use another native Unix API for that: the IOCTL API. There are a couple of caveats to watch out for when calling IOCTL, so we'll focus on that. Eventually, with the IOCTL API hooked up, our terminal skeleton actually works as expected, using the width and height of the entire terminal.

We're almost at the end of the episode, were it not for some tiny, pesky bugs, like the flickering of the screen and wrong cursor positioning. Hence, we're going to optimize the method calls responsible for that behavior.

That was a lot of concepts we just went through! That's why we'll finish off now, having a look at what comes next: loading and displaying text files, as well as being able to move your cursor around.

Enjoy!


Text editor Build (game engine) Java (programming language)

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Using GPT-3 in Our Applications
  • Choosing the Right Framework for Your Project
  • Fixing Bottlenecks in Your Microservices App Flows
  • 3 Main Pillars in ReactJS

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

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

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: