Starting from Chapter 2, at the end of each subsection, there will be some code real-world operations, which you can choose to complete on your own (which is more recommended), and then cross-reference against my implementation, or of course, you can just look at my code implementation. However, for each of the subsequent feature implementations, I'll build on my previous version of the code implementation and expand upon it.
First, let's address the first problem left over from Chapter 1: input data is cached by stdin and is not sent until a line break is encountered (i.e., the Enter key is pressed).
Entering Raw Mode (Raw Mode)
"Raw mode" is a terminal setting that allows the program to process input data directly from the terminal. In "raw mode", typed characters are not buffered by the terminal and are sent to the program immediately. This means that the program can respond to every keystroke by the user immediately, without waiting for the user to press Enter.
In addition, in "raw mode" the terminal does not handle special control characters such as Ctrl+C (usually used to signal an interrupt) or Ctrl+Z (usually used to hang the program). These characters are sent directly to the program, which can decide for itself what to do with them.
Note that enabling "raw mode" requires a deep understanding of the terminal and operating system, and may require platform-specific code support. In this project we decided to usetermbox-go library to simplify this process. The following translation is from the introduction of the termbox-go library:
Termbox is a library that provides a clean API that allows programmers to write text-based user interfaces. The library is cross-platform, with both terminal-based implementations on *nix operating systems and winapi console-based implementations on Windows operating systems. The basic idea is to abstract the features of the largest common subset available in all major terminal and other terminal-like APIs in a concise way. The small API means that it is easy to implement, test, maintain and learn, which is what makes termbox a unique library in its field.
Introduce the codebase into the project via the go get command
go get -u /nsf/termbox-go
This library provides many API interfaces, we need to understand the following three functions:
()
()
-
()
Without expanding on this, you can read the comments of the corresponding library functions to understand the usage.
Assignment 1 Wavy Lines~
Your first task is to draw a column of wave symbols (~) on the far left side of the screen, just as vim does. In our text editor we will draw a wave character at the beginning of any line after the end of the file we are editing, which you can currently use to do so. Next when typingq
The time to end the program run.
Code Review My Realization
- First define a
editor
Structures represent terminals - pass (a bill or inspection etc)
()
Get the width and height of the terminal and keep printing ~ until you get to the bottom of the terminal. - pass (a bill or inspection etc)
()
to capture terminal events, if you are typingq
event, then mark theneedQuit
because oftrue
This way we can decide whether to continue refreshing the terminal or just quit based on this flag before refreshing the terminal each time. This way, before refreshing the terminal each time, we can decide whether to continue refreshing the terminal or just quit based on this flag.
Assignment 2 Wavy Lines ~ Optimization
In this assignment we optimize the drawing of the wavy line ~ by using the termbox api to draw it while showing the cursor.
Note that when the terminal is drawing the screen, the cursor may blink somewhere in the middle of the screen,. To make sure this flickering effect doesn't happen, we need to hide the cursor before refreshing the screen and show it as soon as the refresh is complete. You need to complete the following function:
- Whenever the keyboard is captured moving up, down, left or right, simultaneously move the cursor on the terminal in the corresponding direction.
- Hide the cursor before refreshing the screen (use)
- Display cursor after refreshing the screen (use)
- How to draw a wavy line ~
Note: When drawing with the termbox api, the drawing behavior is stored in its internal buffer, so it needs to be called at the right time to refresh the screen.
Code Review My Realization