Read files with this CLI App made with C++

Replaces the 'less' command for pagination. And more tips on how to control your terminal's default cursor via C++ and FTXUI!


Read files with this CLI App made with C++


If you want a good alternative to the less command of Unix/GNU, ftxuiFileReader is a good alternative.

It is a simple software made with C++ and the FTXUI library!


Compiling and installing ftxuiFileReader

To install, of course, you will need the basic dependencies, they are:

Just clone, compile and run:

git clone https://github.com/J0sephDavis/ftxuiFileReader
cd ftxuiFileReader
cmake -B build .
cd build && make

After that, there will be a binary in the build/ directory, where you ran make, with the same name as the program: ftxuiFileReader. Just run it and pass as an argument the path of the file you want to read, example:

./ftxuiFileReader /path/to/main.cpp

The result will be similar to the GIF below:

ftxuiFileReader GIF


Installation with additional tips

Still within the directory you built: build/ you can install it in the desired location on your system.

But, before that I recommend some changes for better usability of ftxuiFileReader, as there are some problems that I detected, they are:

First: There is a bug in the terminal cursor when finishing the program execution

When you run and exit the program, your terminal cursor changes to blinking underline(_). In my case I use (and it is defined by default) the vertical line(|) and it didn’t look good.

To solve this, still within build/, I edited the source file main.cc:

vim ../src/main.cc

And before the return EXIT_SUCCESS; in the main() function on line 250 I added this std::cout << "\x1b[\x35 q";, looking like this:

std::cout << "\x1b[\x35 q";
return EXIT_SUCCESS;

This works for me, as this x35 is precisely to return to my cursor blinking vertical line. You can change it depending on your cursor, the alternatives are:

  • std::cout << "\x1b[\x31 q"; → Changes to blinking block;
  • std::cout << "\x1b[\x32 q"; → Change to constant block;
  • std::cout << "\x1b[\x33 q"; → Changes to blinking underline;
  • std::cout << "\x1b[\x34 q"; → Changes to constant underscore;
  • std::cout << "\x1b[\x35 q"; → Changes to blinking vertical line;
  • std::cout << "\x1b[\x36 q"; → Also changes to constant vertical line;

    The numbers actually start from 30 and can go even higher, however, the modifications will be similar to these! If you want, you can even declare one: std::system("reset");.

Use the modification depending on your terminal’s default cursor, the one you chose!

Once this is done, just recompile:

Remembering that recompilation takes much less time, as we only modify one file!

make

And to install, for example, in: /usr/local/bin, more recommended, I also suggest installing with a more Unix style name (only with lowercase letters and separated by dashes instead of capitalizations), example: ftx-file-reader:

sudo install -v ftxuiFileViewer /usr/local/bin/ftx-file-reader

Now you can even remove the cloned repository, e.g.: cd ../.. && rm -rf ftxuiFileViewer and use the modified ftx-file-reader command or the default one (ftxuiFileReader), if you modified it, example reading a main.cpp file:

ftx-file-reader main.cpp

Note: You can exit the program by typing the letter q, for example, but there is a small bug that can display the typed character when exiting, but in the form of detectable input, example for q: 1R. To resolve this, simply scroll the down arrow keypad line at least twice. In short, to exit, press the key combination: ↓ ↓ q.

Any questions or improvement tips (getting the default cursor automatically and resetting it when exiting, for example) visit the official repository.

There is other software made with FTXUI that we have already published here on the blog, you can read them by clicking on the links below:


cpp ftxui


Share


YouTube channel

Subscribe


Marcos Oliveira

Marcos Oliveira

Software developer
https://github.com/terroo

Related articles