From 0ef7eba6dbbbcc9196df29a2a14347f3edc5e552 Mon Sep 17 00:00:00 2001 From: trilader Date: Sun, 18 Jul 2021 23:27:20 +0200 Subject: [PATCH] Add ability to rename user flags --- application.cpp | 17 +++++++++++++++++ tui.cpp | 11 ++++++++--- tui.h | 1 + yt.cpp | 10 ++++++++++ yt.h | 1 + 5 files changed, 37 insertions(+), 3 deletions(-) diff --git a/application.cpp b/application.cpp index 7e208f4..dcdf94b 100644 --- a/application.cpp +++ b/application.cpp @@ -529,6 +529,22 @@ void action_add_new_user_flag() { userFlags.push_back(UserFlag::create(db, name)); } +void action_rename_user_flag() { + std::vector names; + for(const UserFlag &flag: userFlags) { + names.push_back(flag.name); + } + int index = get_selection("Select flag to rename", names); + if(index < 0) + return; + UserFlag &flag = userFlags.at(index); + std::string name = edit_string("Enter new name", std::string(), flag.name); + if(name.empty()) + return; + flag.name = name; + flag.save(db); +} + void action_manage_user_flags() { bool done = false; @@ -551,6 +567,7 @@ void action_manage_user_flags() { std::vector actions = { {TERMPAINT_EV_KEY, "F2", 0, action_add_new_user_flag, "Add new user flag"}, + {TERMPAINT_EV_KEY, "F3", 0, action_rename_user_flag, "Rename user flag"}, {TERMPAINT_EV_KEY, "Escape", 0, [&]{ done = true; }, "Stop user flag management"}, {TERMPAINT_EV_KEY, "ArrowUp", 0, [&]{ if(selected_channel > 0) selected_channel--; }, "Previous channel"}, {TERMPAINT_EV_KEY, "ArrowDown", 0, [&]{ if(selected_channel < channels.size()) selected_channel++; }, "Next channel"}, diff --git a/tui.cpp b/tui.cpp index 452d12d..0c603bc 100644 --- a/tui.cpp +++ b/tui.cpp @@ -308,7 +308,7 @@ Button operator&(const Button &a, const Button &b) return Button((int)a & (int)b); } -std::string get_string(const std::string &caption, const std::string &text, const Align align) +std::string edit_string(const std::string &caption, const std::string &text, const std::string &value, const Align align) { const int cols = termpaint_surface_width(surface); const int rows = termpaint_surface_height(surface); @@ -322,8 +322,8 @@ std::string get_string(const std::string &caption, const std::string &text, cons const int input_row = y + 1 + !text.empty(); - std::string input; - size_t input_pos = 0; + std::string input = value; + size_t input_pos = value.size(); termpaint_terminal_set_cursor_visible(terminal, true); termpaint_terminal_set_cursor_style(terminal, TERMPAINT_CURSOR_STYLE_BAR, true); @@ -380,6 +380,11 @@ std::string get_string(const std::string &caption, const std::string &text, cons return input; } +std::string get_string(const std::string &caption, const std::string &text, const Align align) +{ + return edit_string(caption, text, std::string(), align); +} + std::vector split(const std::string &str, const char delim, const unsigned int max_splits=0) { std::vector parts; diff --git a/tui.h b/tui.h index 2529112..de00203 100644 --- a/tui.h +++ b/tui.h @@ -81,6 +81,7 @@ std::string text_wrap(const std::string &text, const size_t desired_width); Button message_box(const std::string &caption, const std::string &text, const Button buttons=Button::Ok, const Button default_button=Button::Ok, const Align align=Align::Center); void draw_box_with_caption(int x, int y, int w, int h, const std::string &caption=std::string()); int get_selection(const std::string &caption, const std::vector &choices, size_t selected=0, const Align align=Align::Center); +std::string edit_string(const std::string &caption, const std::string &text, const std::string &value, const Align align=Align::Center); std::string get_string(const std::string &caption, const std::string &text=std::string(), const Align align=Align::Center); struct progress_info; diff --git a/yt.cpp b/yt.cpp index 031b5cd..f453ab9 100644 --- a/yt.cpp +++ b/yt.cpp @@ -15,6 +15,16 @@ struct yt_config yt_config; UserFlag::UserFlag(sqlite3_stmt *row): id(get_int(row, 0)), name(get_string(row, 1)) {} +void UserFlag::save(sqlite3 *db) const +{ + sqlite3_stmt *query; + SC(sqlite3_prepare_v2(db, "UPDATE user_flags SET name = ?2 WHERE flagId = ?1;", -1, &query, nullptr)); + SC(sqlite3_bind_int(query, 1, id)); + SC(sqlite3_bind_text(query, 2, name.c_str(), -1, SQLITE_TRANSIENT)); + SC(sqlite3_step(query)); + SC(sqlite3_finalize(query)); +} + UserFlag UserFlag::create(sqlite3 *db, const std::string &name) { int next_flag = next_free(db); diff --git a/yt.h b/yt.h index dcd19a1..13a7935 100644 --- a/yt.h +++ b/yt.h @@ -22,6 +22,7 @@ public: std::string name; UserFlag(sqlite3_stmt *row); + void save(sqlite3 *db) const; static UserFlag create(sqlite3 *db, const std::string &name); static int next_free(sqlite3 *db);