From 1502d70cc9dcbf6373680732453f6d38b8bff21b Mon Sep 17 00:00:00 2001 From: trilader Date: Wed, 2 Dec 2020 23:13:57 +0100 Subject: [PATCH] Add auto refresh support --- README.md | 1 + main.cpp | 41 ++++++++++++++++++++++++++++++++--------- yttui.conf.example | 3 ++- 3 files changed, 35 insertions(+), 10 deletions(-) diff --git a/README.md b/README.md index f68da1b..bb469e7 100644 --- a/README.md +++ b/README.md @@ -43,6 +43,7 @@ This tool is work in progress and some functions might not work (correctly). | database | Path of channel/video database | $HOME/.local/share/yttui.db | ✘ | | watchCommand | Command executed to watch a video. `{{vid}}` will be replaced by the Id of the video to watch. | `["xdg-open", "https://youtube.com/watch?v={{vid}}"]` | ✘ | | notifications | Object describing notification settings | `{}` | ✘ | +| autoRefreshInterval | Automatically refresh all channels every X seconds (and after 30 seconds of inactivity). -1 to disable. | -1 | ✘ | #### Notifcation options The `notifications` entry can have the following sub-options: diff --git a/main.cpp b/main.cpp index 47042e7..daa9e90 100644 --- a/main.cpp +++ b/main.cpp @@ -7,6 +7,7 @@ #include "subprocess.h" #include +#include #include #include #include @@ -352,8 +353,8 @@ void action_refresh_channel() { } -void action_refresh_all_channels() { - if(message_box("Refresh all channels?", ("Do you want to refresh all " + std::to_string(channels.size()) + " channels?").c_str(), Button::Yes | Button::No, Button::No) != Button::Yes) +void action_refresh_all_channels(bool ask=true) { + if(ask && message_box("Refresh all channels?", ("Do you want to refresh all " + std::to_string(channels.size()) + " channels?").c_str(), Button::Yes | Button::No, Button::No) != Button::Yes) return; int updated_channels = 0; int new_videos = 0; @@ -560,6 +561,14 @@ int main() config_get_string_list(notify_channels_new_videos_command, notifications, "channelsNewVideosCommand"); } + int auto_refresh_interval = -1; // In seconds + if(config.count("autoRefreshInterval") && config["autoRefreshInterval"].is_number_integer()) { + auto_refresh_interval = config["autoRefreshInterval"]; + auto_refresh_interval = std::max(-1, auto_refresh_interval); + } + std::chrono::system_clock::time_point next_update = std::chrono::system_clock::now() + std::chrono::seconds(auto_refresh_interval); + std::chrono::system_clock::time_point last_user_action; + db_init(database_filename); make_virtual_unwatched_channel(); for(Channel &channel: Channel::get_all(db)) { @@ -578,7 +587,7 @@ int main() {TERMPAINT_EV_CHAR, "j", 0, action_select_prev_channel, "Select previous channel"}, {TERMPAINT_EV_CHAR, "k", 0, action_select_next_channel, "Select next channel"}, {TERMPAINT_EV_CHAR, "r", 0, action_refresh_channel, "Refresh selected channel"}, - {TERMPAINT_EV_CHAR, "R", 0, action_refresh_all_channels, "Refresh all channels"}, + {TERMPAINT_EV_CHAR, "R", 0, [&](){ action_refresh_all_channels(); }, "Refresh all channels"}, {TERMPAINT_EV_CHAR, "w", 0, action_watch_video, "Watch video"}, {TERMPAINT_EV_CHAR, "w", TERMPAINT_MOD_ALT, action_mark_video_watched, "Mark video as watched"}, {TERMPAINT_EV_CHAR, "u", 0, action_mark_video_unwatched, "Mark video as unwatched"}, @@ -597,17 +606,31 @@ int main() {TERMPAINT_EV_CHAR, "l", TERMPAINT_MOD_CTRL, [&](){ force_repaint = true; }, "Force redraw"}, }; + bool draw = true; do { - termpaint_surface_clear(surface, TERMPAINT_DEFAULT_COLOR, TERMPAINT_DEFAULT_COLOR); - draw_channel_list(videos[channels.at(selected_channel).id]); - tp_flush(force_repaint); - force_repaint = false; + if(draw) { + termpaint_surface_clear(surface, TERMPAINT_DEFAULT_COLOR, TERMPAINT_DEFAULT_COLOR); + draw_channel_list(videos[channels.at(selected_channel).id]); + tp_flush(force_repaint); + force_repaint = false; + } + draw = true; - auto event = tp_wait_for_event(); + auto event = tp_wait_for_event(1000); if(!event) abort(); - tui_handle_action(*event, actions); + if(event->type == EV_TIMEOUT) { + draw = false; + const bool update_pending = next_update < std::chrono::system_clock::now(); + const bool inactivity_threshold = (std::chrono::system_clock::now() - last_user_action) > std::chrono::seconds(30); + if(auto_refresh_interval != -1 && update_pending && inactivity_threshold) { + action_refresh_all_channels(false); + std::chrono::system_clock::now() + std::chrono::seconds(auto_refresh_interval); + } + } else if(tui_handle_action(*event, actions)) { + last_user_action = std::chrono::system_clock::now(); + } } while (!exit); tp_shutdown(); diff --git a/yttui.conf.example b/yttui.conf.example index 7f6dd62..fed97e3 100644 --- a/yttui.conf.example +++ b/yttui.conf.example @@ -9,5 +9,6 @@ "channelNewVideoCommand": ["notify-send", "--app-name", "yttui", "New video from {{channelName}}", "{{videoTitle}}"], "channelNewVideosCommand": ["notify-send", "--app-name", "yttui", "New videos from {{channelName}}", "There are {{newVideos}} new videos."], "channelsNewVideosCommand": ["notify-send", "--app-name", "yttui", "New videos from multiple channels", "There are {{newVideos}} new videos from {{updatedChannels}} channels."] - } + }, + "autoRefreshInterval": 3600 }