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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

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
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

Because the DevOps movement has redefined engineering responsibilities, SREs now have to become stewards of observability strategy.

Apache Cassandra combines the benefits of major NoSQL databases to support data management needs not covered by traditional RDBMS vendors.

The software you build is only as secure as the code that powers it. Learn how malicious code creeps into your software supply chain.

Generative AI has transformed nearly every industry. How can you leverage GenAI to improve your productivity and efficiency?

Related

  • How To Build a Command-Line Text Editor With Java (Part 3)
  • Build an AI Chatroom With ChatGPT and ZK by Asking It How!
  • How To Build a Google Photos Clone - Part 1
  • Build a Java Backend That Connects With Salesforce

Trending

  • Software Delivery at Scale: Centralized Jenkins Pipeline for Optimal Efficiency
  • Understanding the Shift: Why Companies Are Migrating From MongoDB to Aerospike Database?
  • Go 1.24+ Native FIPS Support for Easier Compliance
  • Advancing Your Software Engineering Career in 2025
  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.

By 
Marco Behler user avatar
Marco Behler
DZone Core CORE ·
Sep. 09, 22 · Tutorial
Likes (3)
Comment
Save
Tweet
Share
14.8K 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.

Related

  • How To Build a Command-Line Text Editor With Java (Part 3)
  • Build an AI Chatroom With ChatGPT and ZK by Asking It How!
  • How To Build a Google Photos Clone - Part 1
  • Build a Java Backend That Connects With Salesforce

Partner Resources

×

Comments
Oops! Something Went Wrong

The likes didn't load as expected. Please refresh the page and try again.

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!