Misc fixes and optimizations (mostly suggestions by clang-tidy)

This commit is contained in:
Daniel Schulte 2021-07-18 16:41:19 +02:00
parent 3ffd1776a0
commit 2d1e42ea33
6 changed files with 22 additions and 19 deletions

View File

@ -24,7 +24,7 @@
#include <curl/curl.h> #include <curl/curl.h>
#include <nlohmann/json.hpp> #include <nlohmann/json.hpp>
static std::string user_home; std::string user_home;
std::vector<UserFlag> userFlags; std::vector<UserFlag> userFlags;
std::vector<Channel> channels; std::vector<Channel> channels;
@ -60,6 +60,7 @@ void draw_channel_list(const std::vector<Video> &videos, bool show_channel_name=
const size_t available_rows = rows - 2; const size_t available_rows = rows - 2;
videos_per_page = available_rows; videos_per_page = available_rows;
const int cur_page = selected_video / available_rows; const int cur_page = selected_video / available_rows;
const int pages = videos.size() / available_rows;
size_t cur_entry = 0; size_t cur_entry = 0;
@ -87,9 +88,7 @@ void draw_channel_list(const std::vector<Video> &videos, bool show_channel_name=
const size_t last_name_column = cols; const size_t last_name_column = cols;
const size_t name_quater = (last_name_column - first_name_column) / 4; const size_t name_quater = (last_name_column - first_name_column) / 4;
const int pages = videos.size() / available_rows; const std::string channel_name = std::string("Channel: ") + channels[selected_channel].name;
const std::string channel_name = std::string("Channel: ") + channels[selected_channel].name.c_str();
termpaint_surface_write_with_attr(surface, 0, 0, channel_name.c_str(), get_attr(ASNormal)); termpaint_surface_write_with_attr(surface, 0, 0, channel_name.c_str(), get_attr(ASNormal));
if(pages > 1) { if(pages > 1) {
@ -324,15 +323,16 @@ void action_add_channel_by_id()
} }
void action_select_channel() { void action_select_channel() {
if(channels.size()) { if(channels.empty()) {
message_box("Can't select channel", "No channels configured.\n Please configure one.");
} else {
std::vector<std::string> names; std::vector<std::string> names;
names.reserve(channels.size());
for(const Channel &c: channels) for(const Channel &c: channels)
names.push_back(c.name + " (" + std::to_string(c.unwatched) + ")"); names.push_back(c.name + " (" + std::to_string(c.unwatched) + ")");
const int channel = get_selection("Switch Channel", names, selected_channel, Align::VCenter | Align::Left); const int channel = get_selection("Switch Channel", names, selected_channel, Align::VCenter | Align::Left);
if(channel != -1) if(channel != -1)
select_channel_by_index(channel); select_channel_by_index(channel);
} else {
message_box("Can't select channel", "No channels configured.\n Please configure one.");
} }
} }
@ -387,7 +387,7 @@ void action_refresh_channel() {
} else { } else {
if(host && host->notify_channel_multiple_videos) { if(host && host->notify_channel_multiple_videos) {
host->notify_channel_multiple_videos(ch.name, new_videos); host->notify_channel_multiple_videos(ch.name, new_videos);
} else if(notify_channel_new_videos_command.size()) { } else if(!notify_channel_new_videos_command.empty()) {
run_command(notify_channel_new_videos_command, { run_command(notify_channel_new_videos_command, {
{"{{channelName}}", ch.name}, {"{{channelName}}", ch.name},
{"{{newVideos}}", std::to_string(new_videos)} {"{{newVideos}}", std::to_string(new_videos)}
@ -397,7 +397,7 @@ void action_refresh_channel() {
} }
void action_refresh_all_channels(bool ask=true) { 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) if(ask && message_box("Refresh all channels?", "Do you want to refresh all " + std::to_string(channels.size()) + " channels?", Button::Yes | Button::No, Button::No) != Button::Yes)
return; return;
int updated_channels = 0; int updated_channels = 0;
int new_videos = 0; int new_videos = 0;
@ -449,7 +449,7 @@ void action_mark_video_unwatched() {
void action_mark_all_videos_watched() { void action_mark_all_videos_watched() {
Channel &ch = channels.at(selected_channel); Channel &ch = channels.at(selected_channel);
if(message_box("Mark all as watched", ("Do you want to mark all videos of " + ch.name + " as watched?").c_str(), Button::Yes | Button::No, Button::No) != Button::Yes) if(message_box("Mark all as watched", "Do you want to mark all videos of " + ch.name + " as watched?", Button::Yes | Button::No, Button::No) != Button::Yes)
return; return;
{ {
db_transaction transaction; db_transaction transaction;
@ -727,7 +727,7 @@ static void run()
add_channel_to_list(channel); add_channel_to_list(channel);
} }
if(channels.size()) { if(!channels.empty()) {
select_channel_by_index(0); select_channel_by_index(0);
} }

2
db.cpp
View File

@ -86,7 +86,7 @@ std::string db_get_setting(const std::string &key)
SC(sqlite3_prepare_v2(db, "SELECT value FROM settings WHERE key = ?1;", -1, &query, nullptr)); SC(sqlite3_prepare_v2(db, "SELECT value FROM settings WHERE key = ?1;", -1, &query, nullptr));
SC(sqlite3_bind_text(query, 1, key.c_str(), -1, SQLITE_TRANSIENT)); SC(sqlite3_bind_text(query, 1, key.c_str(), -1, SQLITE_TRANSIENT));
SC(sqlite3_step(query)); SC(sqlite3_step(query));
const std::string value = get_string(query, 0); std::string value = get_string(query, 0);
SC(sqlite3_finalize(query)); SC(sqlite3_finalize(query));
return value; return value;
} }

View File

@ -641,7 +641,7 @@ std::string text_wrap(const std::string &text, const size_t desired_width)
return out; return out;
} }
void tui_abort(std::string message) void tui_abort(const std::string &message)
{ {
const size_t cols = termpaint_surface_width(surface); const size_t cols = termpaint_surface_width(surface);
@ -705,7 +705,7 @@ static void draw_help(const std::vector<helpitem> &items)
to_show.append("\n"); to_show.append("\n");
} }
message_box("Help", to_show.c_str()); message_box("Help", to_show);
} }
static std::string format_key(const action &action) { static std::string format_key(const action &action) {

2
tui.h
View File

@ -88,5 +88,5 @@ progress_info* begin_progress(const std::string &caption, const int width, const
void update_progress(progress_info *info, const int val, const int maxval); void update_progress(progress_info *info, const int val, const int maxval);
void end_progress(progress_info *info); void end_progress(progress_info *info);
void tui_abort(std::string message); void tui_abort(const std::string &message);
void tui_abort(const char *fmt, ...); void tui_abort(const char *fmt, ...);

9
yt.cpp
View File

@ -78,18 +78,21 @@ static size_t curl_writecallback(void *data, size_t size, size_t nmemb, void *us
return to_add; return to_add;
} }
static json api_request(const std::string &url, std::map<std::string, std::string> params) static json api_request(const std::string &url, const std::map<std::string, std::string> &params)
{ {
CURL *curl = curl_easy_init(); CURL *curl = curl_easy_init();
curl_slist *headers = nullptr; curl_slist *headers = nullptr;
for(const auto &[header, value]: yt_config.extra_headers) { for(const auto &[header, value]: yt_config.extra_headers) {
headers = curl_slist_append(headers, (header + ": " + value).c_str()); std::string h = header;
h.append(": ").append(value);
headers = curl_slist_append(headers, h.c_str());
} }
CURLU *u = curl_url(); CURLU *u = curl_url();
curl_url_set(u, CURLUPART_URL, url.c_str(), 0); curl_url_set(u, CURLUPART_URL, url.c_str(), 0);
for(const auto &[k, v]: params) { for(const auto &[k, v]: params) {
std::string p = k + "=" + v; std::string p = k;
p.append("=").append(v);
curl_url_set(u, CURLUPART_QUERY, p.c_str(), CURLU_APPENDQUERY|CURLU_URLENCODE); curl_url_set(u, CURLUPART_QUERY, p.c_str(), CURLU_APPENDQUERY|CURLU_URLENCODE);
} }
char *real_url; char *real_url;

View File

@ -182,7 +182,7 @@ int main(int argc, char *argv[])
}; };
appthread.start(); appthread.start();
int rc = app.exec(); int rc = QApplication::exec();
close(term_fd); close(term_fd);
close(app_fd); close(app_fd);