In this chapter, we will complete the file opening and viewing functions of the text editor, and the final product is shown above. We will be divided into 4 steps, gradually complete the functions required in this chapter. The content is relatively large, will be divided into two parts, the first part focuses on the "View view" and "buffer and text reading".
As you can see in the final image above, we want to add a status bar at the bottom of the terminal that shows the currently opened file and the current cursor position.
At the same time we intend to do some segmentation and refactoring of the entire terminal view.
Let's first start with the positioning of the various components of the editor:
- editor (software)
editor
: This component is mainly used in different user interface components (currently only theView
) to coordinate among themselves. - view
View
: This component receives every text-related event from the editor, such as keystrokes entered for characters, line feeds, etc. The view uses this information for rendering and translates and passes text modification events to the bufferbuffer
。 - buffer
buffer
(more on this in a moment): this component will hold our text. For this project we will only start by processing ascii characters.Doesn't work with full-width characters, emoji symbols , so there may be exceptions to the display and editing of such special characters.
Specifically, the red box is the main interactive part of the editor, we use a structure called view to represent it; the lower part of the orange box we use status_bar structure to represent it, it will display some status information of the editor, such as the name of the currently open file and the current cursor in the first few rows and columns.
View Structure
First let's refactor part of the project code to put some existing functionality into the view structure and its methods, so oureditor
The structure should hold aView
type editor struct {
view *
needQuit bool
}
as forView
for example, because the cursor can only be used in theView
moves inside, so the cursor position indicating theTextLocX
、TextLocY
should be moved toView
among the members of the structure.
type View struct {
TextLocX int
TextLocY int
}
Remaining andView
The associated function page should be refactored to the View's methods, including the
- CursorPos
- Render
- MoveCursor
Homework 1
Refactor the code according to the requirements of the View structure.
Code Review My Realization
Simple Buffer
The buffer is a common structure that holds everything a text editor needs to modify and display a text file. The view interacts with the buffer to render the text on the screen. In many text editors, you can easily switch from one buffer to the next, allowing you to open multiple files in parallel.
The details of buffer implementation vary from text editor to text editor. For example, Nano uses a simple and straightforward buffer structure: when a file is saved, the contents of the buffer (excluding data such as syntax highlighting colors) are saved to disk. Vim, on the other hand, has a complex internal structure that handles operations efficiently even when working with large files, and includes a separate screen buffer to represent what is currently visible on the screen. These design choices reflect the vision of each editor: Nano is intended to be a small, lightweight editor suitable for quickly updating configuration files. Vim, on the other hand, is designed to be a full-featured, feature-rich text editor, ideal for working with lengthy files.
Assignment 2 Simple buffer
- Create a new structure called Buffer that contains a
string
thin section of specimen for examination (as part of biopsy) - Changing the View structure to hold a Buffer
- buffer provides load_file method: this method reads the text and stores the text content into the buffer, a line of text corresponds to a string element in the buffer
- You can use the
(used form a nominal expression)
scan
method to read text line by line - Renders each string element in the Buffer line by line on the terminal. For each empty line on the screen, still render a
~
Code Review My Realization
- You can use the