Based on terminal reader development
I’m a relative reader, but I only read two kinds of books, at least for now. One is for eating books, programming books, one is for my leisure books, all kinds of novels. Of course, I also have a preference for the type of novel. For example, my colleague Amway’s book by Wang Xiaobo lies quietly on the shelf.
A few days ago, my colleague shared some e-books with me, but there was no reader I liked in the MAC system. Although iBook was nearly perfect, it did not support TXT text files, so I took this opportunity to realize a terminal based reader with Golang, which was convenient for me to read novels.
Let’s take a look at the final interface style:
For ease of use, I had to find a convenient terminal drawing library to make interface development easier. In C language application, we can choose ncurses library, which is convenient for us to develop terminal programs based on simple graphics. However, this library is not implemented by Golang. After a bucket search on Github, I finally locked a third-party library named Termui. You can find the library on Termui’s Github page.
I divided the tool into two parts, the interface drawing part and the configuration recording part. Let’s start with configuration.
All configuration files
When I wrote the blog system last time, I found a good configuration file format: TOML. The advantage of this format is that it can be easily mapped to the structure in Golang. At the same time, the configuration information is ambiguous, and the configuration is more accurate. In this reader, we don’t have to use such a complex structure, just need to tell the tool where our ebook files are stored. The tool then scans all files under this path (excluding subdirectories). These files should all be text files.
The content of the configuration file is as follows:
/Users/mebius/Documents/story/
Copy the code
This directory is the directory where e-books are stored.
In business logic, we need to read the contents of the configuration file at startup and then perform subsequent logical operations.
Progress Configuration
After reading all the configuration information, we need to record the progress of reading different books. But how to record unique ebook files? The file name can be used, but if the file name changes, the corresponding progress is lost. Problems also occur if paths are recorded. Finally, I generated the CORRESPONDING MD5 value for each book file so that progress information would not be lost even if the file name and path changed, as long as the file contents did not change. You can pick any other encoding, as long as it’s unique.
There is also a value to record the progress of the file, which records the reading position, using an int to record.
Interface logic
With termui library, interface development is relatively easy, you can use the library to build your own controls, while binding corresponding keyboard events, for different interfaces, different keyboard events for interactive operation.
In the interface part, I encapsulated a UIBar control, which is used to display the operation tips, also in the bottom of the screen toolbar. At the same time, the two interfaces, the list interface and the reading interface, are placed in two interface packages respectively. The final interface switching operation is managed by UI encapsulation.
Open source code
If you need, or are interested in, pulling code from our Command-book project repository and compiling it into your own tool.