Event loop

The event loop is like the glue that puts everything together.

Before the event loop

But before the TUI enters it's main event loop, it does several things.

1. Check what command is being ran.

If the command does not need to enter the main TUI, don't enter the main TUI.

For example, help or version just returns a text message on your terminal screen. In main.rs line 19, it is checked whether the entered command is a valid text command. If so, the program exits without ever reaching the TUI code.

2. Enable raw mode

If you've tried killing youtube-tui while it's running (killall youtube-tui), your terminal should be in a weird state - where everything, including mouse movement seem to be captured. That's the raw mode.

I have no idea what is special about it, but it is in the examples of tui-rs here (one of the main dependency of this TUI).

3. Init

There are a bunch of things happening in this stage, but it really just boils down to a few basic things:

  • Reading/creating config files, subscriptions, libraries and inserting them to the pool of shared data (global data) within the framework struct.

Global data is used here, the reason should be obvious. All pages, should share the same config, there is no need to keep extra copies of the (usually) same thing and waste memory.

  • Insert some structs needed for the TUI to run.

These structs includes Message storing the message to be displayed in message bar, and Status, useful for storing all sorts of values, for example helping with the running (mostly rendering) of the TUI.

  • Move some files

Some files have to be moved to ~/.cache/youtube-tui/ from (usually) ~/.local/share/youtube-tui/ for stored content to be used by the TUI, this is why running multiple instances of the program can mess things up - as one instance exits the files got moved from .cache to .local, and the other instance no longer sees the files they thought was still in .cache.

The full init code is here


todo...