From 975aa4330f27ce436903fa1275db9f3961969e16 Mon Sep 17 00:00:00 2001 From: Ttibsi Date: Thu, 15 Jan 2026 14:30:21 +0000 Subject: [PATCH 1/3] Change return type in template --- src/controller.cpp | 108 ++++++++++++--------------------------------- 1 file changed, 29 insertions(+), 79 deletions(-) diff --git a/src/controller.cpp b/src/controller.cpp index f297b3e..04bc743 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -59,9 +59,7 @@ void Controller::create_view(const std::string& file_name, unsigned long lineno) view.add_model(&models.at(models.size() - 1)); } else { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Creating view from file: " + file_name); - } + if (logger != nullptr) { logger->info("Creating view from file: " + file_name); } opt_lines_t file_chars = open_file(file_name); @@ -106,9 +104,7 @@ void Controller::start_action_engine() { rawterm::signal_handler(rawterm::Signal::SIG_CONT, sig_resize_redraw); rawterm::signal_handler(rawterm::Signal::SIG_WINCH, sig_resize_redraw); - if (!(k.has_value())) { - continue; - } + if (!(k.has_value())) { continue; } if (mode == Mode::Write) { if (k.value() == rawterm::Key(' ', rawterm::Mod::Escape)) { @@ -119,7 +115,7 @@ void Controller::start_action_engine() { continue; } else if (k.value() == rawterm::Key('D', rawterm::Mod::Arrow)) { - parse_action(&view, Action {ActionType::MoveCursorLeft}); + parse_action(&view, Action {ActionType::MoveCursorLeft}); } else if (k.value() == rawterm::Key('B', rawterm::Mod::Arrow)) { auto redraw = @@ -140,7 +136,7 @@ void Controller::start_action_engine() { } } else if (k.value() == rawterm::Key('C', rawterm::Mod::Arrow)) { - parse_action(&view, Action {ActionType::MoveCursorRight}); + parse_action(&view, Action {ActionType::MoveCursorRight}); } else if (k.value() == rawterm::Key(' ', rawterm::Mod::Backspace)) { auto draw = parse_action(&view, Action {ActionType::Backspace}); @@ -173,18 +169,14 @@ void Controller::start_action_engine() { } else if (mode == Mode::Read) { // Move right, then enter insert mode if (k.value() == rawterm::Key('a')) { - if (is_readonly_model()) { - continue; - } + if (is_readonly_model()) { continue; } - parse_action(&view, Action {ActionType::MoveCursorRight}); + parse_action(&view, Action {ActionType::MoveCursorRight}); parse_action(&view, Action {ActionType::ChangeMode, Mode::Write}); // Move cur to end of line and enter insert mode } else if (k.value() == rawterm::Key('A', rawterm::Mod::Shift)) { - if (is_readonly_model()) { - continue; - } + if (is_readonly_model()) { continue; } parse_action(&view, Action {ActionType::EndOfLine}); parse_action(&view, Action {ActionType::ChangeMode, Mode::Write}); @@ -196,9 +188,7 @@ void Controller::start_action_engine() { // Delete } else if (k.value() == rawterm::Key('d')) { auto k2 = rawterm::wait_for_input(); - if (!(k2.isCharInput())) { - continue; - } + if (!(k2.isCharInput())) { continue; } switch (k2.code) { case 'l': @@ -253,9 +243,7 @@ void Controller::start_action_engine() { parse_action(&view, Action {ActionType::MoveCursorLeft}); redraw_all = ret.value(); } else if (k.value() == rawterm::Key('i')) { - if (is_readonly_model()) { - continue; - } + if (is_readonly_model()) { continue; } parse_action(&view, Action {ActionType::ChangeMode, Mode::Write}); } else if (k.value() == rawterm::Key('j')) { @@ -268,9 +256,7 @@ void Controller::start_action_engine() { } } else if (k.value() == rawterm::Key('J', rawterm::Mod::Shift)) { - if (is_readonly_model()) { - continue; - } + if (is_readonly_model()) { continue; } parse_action(&view, Action {ActionType::MoveLineDown}); redraw_all = true; @@ -285,9 +271,7 @@ void Controller::start_action_engine() { } } else if (k.value() == rawterm::Key('K', rawterm::Mod::Shift)) { - if (is_readonly_model()) { - continue; - } + if (is_readonly_model()) { continue; } parse_action(&view, Action {ActionType::MoveLineUp}); redraw_all = true; @@ -299,9 +283,7 @@ void Controller::start_action_engine() { // add new line and go to insert mode (below) } else if (k.value() == rawterm::Key('o')) { - if (is_readonly_model()) { - continue; - } + if (is_readonly_model()) { continue; } parse_action(&view, Action {ActionType::EndOfLine}); parse_action(&view, Action {ActionType::Newline}); @@ -310,13 +292,11 @@ void Controller::start_action_engine() { // add new line and go to insert mode (above) } else if (k.value() == rawterm::Key('O', rawterm::Mod::Shift)) { - if (is_readonly_model()) { - continue; - } + if (is_readonly_model()) { continue; } uint32_t horizontal_cursor_pos = view.get_active_model()->current_char; while (horizontal_cursor_pos) { - parse_action(&view, Action {ActionType::MoveCursorLeft}); + parse_action(&view, Action {ActionType::MoveCursorLeft}); horizontal_cursor_pos--; } @@ -328,9 +308,7 @@ void Controller::start_action_engine() { // Replace current char } else if (k.value() == rawterm::Key('r')) { - if (is_readonly_model()) { - continue; - } + if (is_readonly_model()) { continue; } auto k2 = rawterm::wait_for_input(); if (k2.isCharInput()) { @@ -341,14 +319,10 @@ void Controller::start_action_engine() { // redo } else if (k.value() == rawterm::Key('R', rawterm::Mod::Shift)) { - if (is_readonly_model()) { - continue; - } + if (is_readonly_model()) { continue; } auto ret = parse_action(&view, Action {ActionType::TriggerRedo}); - if (ret.has_value()) { - redraw_all = ret.value(); - } + if (ret.has_value()) { redraw_all = ret.value(); } // tabs } else if (k.value() == rawterm::Key('t')) { @@ -366,14 +340,10 @@ void Controller::start_action_engine() { // Undo } else if (k.value() == rawterm::Key('u')) { - if (is_readonly_model()) { - continue; - } + if (is_readonly_model()) { continue; } auto ret = parse_action(&view, Action {ActionType::TriggerUndo}); - if (ret.has_value()) { - redraw_all = ret.value(); - } + if (ret.has_value()) { redraw_all = ret.value(); } // Jump to next "word" } else if (k.value() == rawterm::Key('w')) { @@ -381,9 +351,7 @@ void Controller::start_action_engine() { // Delete char under key } else if (k.value() == rawterm::Key('x')) { - if (is_readonly_model()) { - continue; - } + if (is_readonly_model()) { continue; } auto draw = parse_action(&view, Action {ActionType::DelCurrentChar}); @@ -405,9 +373,7 @@ void Controller::start_action_engine() { view.draw_status_bar(); redraw_all = enter_command_mode(); view.cur.reset(); - if (quit_flag) { - break; - } + if (quit_flag) { break; } parse_action(&view, Action {ActionType::ChangeMode, Mode::Read}); // Move to beginning/end of line @@ -426,9 +392,7 @@ void Controller::start_action_engine() { // toggle case of char } else if (k.value() == rawterm::Key('~')) { - if (is_readonly_model()) { - continue; - } + if (is_readonly_model()) { continue; } parse_action(&view, Action {ActionType::ToggleCase}); view.draw_line(Draw_Line_dir::None); @@ -459,9 +423,7 @@ bool Controller::enter_command_mode() { while (true) { auto previous = view.draw_command_bar(cmd_text_pos + 1); - if (!(prev_cursor_pos.has_value())) { - prev_cursor_pos = previous; - } + if (!(prev_cursor_pos.has_value())) { prev_cursor_pos = previous; } auto in = rawterm::wait_for_input(); if (in == rawterm::Key(' ', rawterm::Mod::Escape)) { @@ -470,9 +432,7 @@ bool Controller::enter_command_mode() { view.cur = prev_cursor_pos.value(); break; } else if (in == rawterm::Key('m', rawterm::Mod::Enter)) { - if (view.command_text.substr(0, 2) == ";s") { - view.draw_screen(); - } + if (view.command_text.substr(0, 2) == ";s") { view.draw_screen(); } view.cur = prev_cursor_pos.value(); ret = parse_command(); break; @@ -487,9 +447,7 @@ bool Controller::enter_command_mode() { const long pipes = std::count_if( view.command_text.begin(), view.command_text.end(), [](char c) { return c == '|'; }); - if (pipes == 2) { - view.draw_screen(); - } + if (pipes == 2) { view.draw_screen(); } } } else { @@ -529,9 +487,7 @@ bool Controller::enter_command_mode() { const long pipes = std::count_if( view.command_text.begin(), view.command_text.end(), [](char c) { return c == '|'; }); - if (pipes < 3) { - continue; - } + if (pipes < 3) { continue; } const std::vector parts = split_by(view.command_text, '|'); std::vector found_lines = @@ -551,14 +507,10 @@ bool Controller::enter_command_mode() { bool Controller::parse_command() { std::string cmd = std::move(view.command_text); auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Iris CMD called: " + cmd); - } + if (logger != nullptr) { logger->info("Iris CMD called: " + cmd); } // Empty command - if (cmd.size() < 2) { - return false; - } + if (cmd.size() < 2) { return false; } if (std::isdigit(cmd.at(1))) { const unsigned int offset = uint32_t(std::stoi(cmd.substr(1, cmd.size()))); @@ -731,9 +683,7 @@ void Controller::add_model(const std::string& filename) { WriteAllData write_all_data = {}; for (auto&& m : models) { - if (m.filename == "NO NAME") { - continue; - } + if (m.filename == "NO NAME") { continue; } if (write_to_file(&m, std::nullopt).valid) { write_all_data.files++; From 57b6592ed26cc1cf0baa3faf5853ab88c21835b7 Mon Sep 17 00:00:00 2001 From: Ttibsi Date: Thu, 15 Jan 2026 15:42:01 +0000 Subject: [PATCH 2/3] minor tweaks to tests --- tests/integration/command_mode_test.py | 2 ++ tests/integration/normal_mode_test.py | 2 +- tests/integration/ui_test.py | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/tests/integration/command_mode_test.py b/tests/integration/command_mode_test.py index 455dbc7..1e40fdf 100644 --- a/tests/integration/command_mode_test.py +++ b/tests/integration/command_mode_test.py @@ -78,6 +78,7 @@ def test_quit_all_this_modified_file(r: TmuxRunner): current_cursor: tuple[int, ...] = r.cursor_pos() r.iris_cmd("qa") + time.sleep(0.1) assert r.await_statusbar_parts()[-2] != "[2]" assert r.await_statusbar_parts()[1] == "[X]" assert " | " not in r.lines()[0] # no tab bar @@ -201,6 +202,7 @@ def test_write_all_command(r: TmuxRunner): r.iris_cmd("wa") assert "[X]" not in r.statusbar_parts() assert "*" not in r.await_tab_bar_parts()[1] + time.sleep(0.1) with open(r.filename, "r") as f: first_line: str = f.readlines()[0] diff --git a/tests/integration/normal_mode_test.py b/tests/integration/normal_mode_test.py index bffc955..4001ad9 100644 --- a/tests/integration/normal_mode_test.py +++ b/tests/integration/normal_mode_test.py @@ -20,7 +20,7 @@ def test_a_key(r: TmuxRunner): statusbar: list[str] = r.await_statusbar_parts() assert statusbar[0] == "WRITE" - assert statusbar[-1] == "1:1" + assert statusbar[-1] == "1:2" @setup("tests/fixture/test_file_1.txt") diff --git a/tests/integration/ui_test.py b/tests/integration/ui_test.py index b3a7ce5..3624930 100644 --- a/tests/integration/ui_test.py +++ b/tests/integration/ui_test.py @@ -202,6 +202,7 @@ def test_multi_file_cursor_on_active_line(r: TmuxRunner): @setup("tests/fixture/test_file_1.txt", multi_file=True) def test_modified_marker_in_tab_bar(r: TmuxRunner): r.press("x") + time.sleep(0.1) tab_bar = r.await_tab_bar_parts() assert tab_bar[0] == "temp_file.txt" r.assert_inverted_text(tab_bar[1], "test_file_1.txt*") From 9e0472a8eb62e175946455802aeac756f2225765 Mon Sep 17 00:00:00 2001 From: Ttibsi Date: Thu, 15 Jan 2026 16:23:17 +0000 Subject: [PATCH 3/3] pre-commit refactor --- src/action.h | 120 +++++++++++-------------------------------- src/main.cpp | 8 +-- src/model.cpp | 100 +++++++++--------------------------- src/text_io.cpp | 36 ++++--------- src/view.cpp | 88 ++++++++----------------------- tests/model_test.cpp | 4 +- tests/view_test.cpp | 4 +- 7 files changed, 90 insertions(+), 270 deletions(-) diff --git a/src/action.h b/src/action.h index 67a74f1..915c3fd 100644 --- a/src/action.h +++ b/src/action.h @@ -64,9 +64,7 @@ template case ActionType::Backspace: { if constexpr (std::is_same_v) { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: Backspace"); - } + if (logger != nullptr) { logger->info("Action called: Backspace"); } // Skip if cur at 0,0 if (v->get_active_model()->current_line == 0 && @@ -114,9 +112,7 @@ template case ActionType::DelCurrentChar: { if constexpr (std::is_same_v) { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: DelCurrentChar"); - } + if (logger != nullptr) { logger->info("Action called: DelCurrentChar"); } v->get_active_model()->undo_stack.push_back(Change( ActionType::DelCurrentChar, v->get_active_model()->current_line, @@ -132,9 +128,7 @@ template case ActionType::DelCurrentLine: { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: DelCurrentLine"); - } + if (logger != nullptr) { logger->info("Action called: DelCurrentLine"); } v->get_active_model()->undo_stack.push_back(Change( ActionType::DelCurrentLine, v->get_active_model()->current_line, @@ -148,9 +142,7 @@ template case ActionType::DelCurrentWord: { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: DelCurrentWord"); - } + if (logger != nullptr) { logger->info("Action called: DelCurrentWord"); } const WordPos word = v->get_active_model()->current_word(); @@ -164,18 +156,14 @@ template case ActionType::EndOfLine: { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: EndOfLine"); - } + if (logger != nullptr) { logger->info("Action called: EndOfLine"); } v->cursor_end_of_line(); } break; case ActionType::JumpEndOfWord: { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: JumpEndOfWord"); - } + if (logger != nullptr) { logger->info("Action called: JumpEndOfWord"); } std::optional count = v->get_active_model()->end_of_word_pos(); if (count.has_value()) { @@ -188,35 +176,25 @@ template case ActionType::JumpNextPara: { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: JumpNextPara"); - } + if (logger != nullptr) { logger->info("Action called: JumpNextPara"); } std::optional count = v->get_active_model()->next_para_pos(); - if (count.has_value()) { - v->cursor_down(count.value()); - } + if (count.has_value()) { v->cursor_down(count.value()); } } break; case ActionType::JumpPrevPara: { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: JumpPrevPara"); - } + if (logger != nullptr) { logger->info("Action called: JumpPrevPara"); } std::optional count = v->get_active_model()->prev_para_pos(); - if (count.has_value()) { - v->cursor_up(count.value()); - } + if (count.has_value()) { v->cursor_up(count.value()); } } break; case ActionType::JumpNextWord: { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: JumpNextWord"); - } + if (logger != nullptr) { logger->info("Action called: JumpNextWord"); } std::optional count = v->get_active_model()->next_word_pos(); if (count.has_value()) { @@ -229,9 +207,7 @@ template case ActionType::JumpPrevWord: { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: JumpPrevWord"); - } + if (logger != nullptr) { logger->info("Action called: JumpPrevWord"); } std::optional count = v->get_active_model()->prev_word_pos(); if (count.has_value()) { @@ -245,9 +221,7 @@ template case ActionType::MoveCursorLeft: { if constexpr (std::is_same_v) { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: MoveCursorLeft"); - } + if (logger != nullptr) { logger->info("Action called: MoveCursorLeft"); } return v->cursor_left(); } @@ -259,9 +233,7 @@ template case ActionType::MoveCursorUp: { if constexpr (std::is_same_v) { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: MoveCursorUp"); - } + if (logger != nullptr) { logger->info("Action called: MoveCursorUp"); } return v->cursor_up(); } } break; @@ -269,9 +241,7 @@ template case ActionType::MoveCursorDown: { if constexpr (std::is_same_v) { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: MoveCursorDown"); - } + if (logger != nullptr) { logger->info("Action called: MoveCursorDown"); } return v->cursor_down(); } } break; @@ -279,9 +249,7 @@ template case ActionType::MoveCursorRight: { if constexpr (std::is_same_v) { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: MoveCursorRight"); - } + if (logger != nullptr) { logger->info("Action called: MoveCursorRight"); } return v->cursor_right(); } @@ -289,9 +257,7 @@ template case ActionType::MoveLineDown: { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: MoveLineDown"); - } + if (logger != nullptr) { logger->info("Action called: MoveLineDown"); } v->get_active_model()->move_line_down(); v->cursor_down(); @@ -300,9 +266,7 @@ template case ActionType::MoveLineUp: { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: MoveLineUp"); - } + if (logger != nullptr) { logger->info("Action called: MoveLineUp"); } v->get_active_model()->move_line_up(); v->cursor_up(); @@ -311,9 +275,7 @@ template case ActionType::Newline: { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: Newline"); - } + if (logger != nullptr) { logger->info("Action called: Newline"); } v->get_active_model()->undo_stack.push_back(Change( ActionType::Newline, v->get_active_model()->current_line, @@ -335,9 +297,7 @@ template case ActionType::StartOfLine: { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: Newline"); - } + if (logger != nullptr) { logger->info("Action called: Newline"); } v->cursor_start_of_line(); @@ -345,35 +305,27 @@ template case ActionType::TabNew: { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: TabNew"); - } + if (logger != nullptr) { logger->info("Action called: TabNew"); } v->tab_new(); } break; case ActionType::TabNext: { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: TabNext"); - } + if (logger != nullptr) { logger->info("Action called: TabNext"); } v->tab_next(); } break; case ActionType::TabPrev: { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: TabPrev"); - } + if (logger != nullptr) { logger->info("Action called: TabPrev"); } v->tab_prev(); } break; case ActionType::ToggleCase: { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: Newline"); - } + if (logger != nullptr) { logger->info("Action called: Newline"); } v->get_active_model()->toggle_case(); @@ -386,9 +338,7 @@ template case ActionType::TriggerRedo: { if constexpr (std::is_same_v) { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: TriggerRedo"); - } + if (logger != nullptr) { logger->info("Action called: TriggerRedo"); } return v->get_active_model()->redo(v->view_size.horizontal); } @@ -399,9 +349,7 @@ template case ActionType::TriggerUndo: { if constexpr (std::is_same_v) { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: TriggerUndo"); - } + if (logger != nullptr) { logger->info("Action called: TriggerUndo"); } return v->get_active_model()->undo(v->view_size.horizontal); } @@ -412,9 +360,7 @@ template case ActionType::ChangeMode: { if constexpr (std::is_same_v) { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: ChangeMode"); - } + if (logger != nullptr) { logger->info("Action called: ChangeMode"); } v->ctrlr_ptr->set_mode(action.payload); } @@ -477,9 +423,7 @@ template case ActionType::InsertChar: { if constexpr (std::is_same_v) { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: InsertChar"); - } + if (logger != nullptr) { logger->info("Action called: InsertChar"); } v->get_active_model()->insert(action.payload); v->cur.move_right(); @@ -494,9 +438,7 @@ template case ActionType::ReplaceChar: { if constexpr (std::is_same_v) { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Action called: ReplaceChar"); - } + if (logger != nullptr) { logger->info("Action called: ReplaceChar"); } v->get_active_model()->undo_stack.push_back(Change( ActionType::ReplaceChar, v->get_active_model()->current_line, @@ -508,9 +450,7 @@ template default: { auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Unknown action called"); - } + if (logger != nullptr) { logger->info("Unknown action called"); } return {}; } break; diff --git a/src/main.cpp b/src/main.cpp index dc524f2..5aed8c7 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -29,9 +29,7 @@ int main(int argc, char* argv[]) { try { app.parse(argc, argv); - } catch (const CLI::ParseError& e) { - return app.exit(e); - } + } catch (const CLI::ParseError& e) { return app.exit(e); } if (print_version) { std::println("{}", version()); @@ -46,9 +44,7 @@ int main(int argc, char* argv[]) { try { auto logger = spdlog::basic_logger_mt("basic_logger", "iris.log"); - } catch (const spdlog::spdlog_ex& ex) { - std::println("Log init failed: {}", ex.what()); - } + } catch (const spdlog::spdlog_ex& ex) { std::println("Log init failed: {}", ex.what()); } spdlog::set_pattern("[%H:%M:%S %z] [thread %t] [%l] %v"); spdlog::get("basic_logger")->info("Iris startup"); diff --git a/src/model.cpp b/src/model.cpp index e19c536..a1c288b 100644 --- a/src/model.cpp +++ b/src/model.cpp @@ -28,9 +28,7 @@ Model::Model(std::vector file_chars, std::string_view file_name) // Concat two lines // At top of buffer - if (current_line == 0) { - return Redraw(RedrawType::None); - } + if (current_line == 0) { return Redraw(RedrawType::None); } const std::size_t prev_line_len = buf.at(current_line - 1).size(); buf.at(current_line - 1) += buf.at(current_line); @@ -80,9 +78,7 @@ Model::Model(std::vector file_chars, std::string_view file_name) buf.at(current_line) = first; current_line++; - if (!second.size()) { - second.push_back(' '); - } + if (!second.size()) { second.push_back(' '); } // Add intentional indentation const int preceeding_ws = first_non_whitespace(first); @@ -129,9 +125,7 @@ void Model::insert(const char c) { incrementer++; // At end of line, don't move - if (incrementer == line_frag.size() - 1) { - return {}; - } + if (incrementer == line_frag.size() - 1) { return {}; } } // go to start of next word @@ -139,9 +133,7 @@ void Model::insert(const char c) { incrementer++; // At end of line, don't move - if (incrementer >= line_frag.size() - 1) { - return incrementer; - } + if (incrementer >= line_frag.size() - 1) { return incrementer; } } return incrementer; @@ -161,27 +153,21 @@ void Model::insert(const char c) { incrementer++; // At end of line, don't move - if (incrementer == line_frag.size()) { - return incrementer; - } + if (incrementer == line_frag.size()) { return incrementer; } } while (!(is_letter(line_frag.at(current_char - incrementer)))) { incrementer++; // At end of line, don't move - if (incrementer == line_frag.size()) { - return incrementer; - } + if (incrementer == line_frag.size()) { return incrementer; } } return incrementer; } [[nodiscard]] std::optional Model::next_para_pos() { - if (current_line == buf.size() - 1) { - return {}; - } + if (current_line == buf.size() - 1) { return {}; } auto pos = std::find_if(buf.begin() + current_line + 1, buf.end(), [](const std::string& s) { return s.empty() || std::all_of(s.begin(), s.end(), [](unsigned char c) { return std::isspace(c); }); @@ -189,16 +175,12 @@ void Model::insert(const char c) { unsigned int distance = static_cast(std::distance(buf.begin() + current_line, pos)); - if (current_line + distance >= buf.size()) { - return buf.size() - current_line - 1; - } + if (current_line + distance >= buf.size()) { return buf.size() - current_line - 1; } return distance; } [[nodiscard]] std::optional Model::prev_para_pos() { - if (current_line == 0) { - return {}; - } + if (current_line == 0) { return {}; } auto rev_pos = std::find_if( buf.rbegin() + static_cast(buf.size() - current_line + 1), buf.rend(), @@ -210,9 +192,7 @@ void Model::insert(const char c) { auto pos = rev_pos.base(); unsigned int distance = static_cast(std::distance(pos, buf.begin() + current_line)); - if (distance > current_line) { - return current_line; - } + if (distance > current_line) { return current_line; } return distance; } @@ -228,17 +208,13 @@ void Model::insert(const char c) { std::string_view(buf.at(current_line)).substr(current_char, buf.at(current_line).size()); // If the _next_ char is a space, we want to go past that - if (line_frag.at(1) == ' ') { - incrementer += 2; - }; + if (line_frag.at(1) == ' ') { incrementer += 2; }; // go to end of current "word" while (is_letter(line_frag.at(incrementer))) { incrementer++; - if (incrementer == line_frag.size() - 1) { - return incrementer; - } + if (incrementer == line_frag.size() - 1) { return incrementer; } } return --incrementer; @@ -290,9 +266,7 @@ void Model::toggle_case() { int cur_char = int32_t(current_char); for (; cur_line >= 0 && cur_line < buf.size(); cur_line--) { - if (!(cur_line == current_line)) { - cur_char = int32_t(buf.at(cur_line).size() - 1); - } + if (!(cur_line == current_line)) { cur_char = int32_t(buf.at(cur_line).size() - 1); } auto iter = std::find( buf.at(cur_line).rbegin() + int32_t(buf.at(cur_line).size()) - cur_char, @@ -311,9 +285,7 @@ void Model::toggle_case() { } [[nodiscard]] bool Model::undo(const int height) { - if (!undo_stack.size()) { - return false; - } + if (!undo_stack.size()) { return false; } Change cur_change = undo_stack.at(undo_stack.size() - 1); undo_stack.pop_back(); @@ -372,19 +344,13 @@ void Model::toggle_case() { break; }; - if (!undo_stack.size()) { - unsaved = false; - } + if (!undo_stack.size()) { unsaved = false; } current_line = uint32_t(cur_pos.vertical); current_char = uint32_t(cur_pos.horizontal); - if (view_offset <= cur_change.line_pos) { - return true; - } - if (cur_change.line_pos <= view_offset + uint32_t(height)) { - return true; - } + if (view_offset <= cur_change.line_pos) { return true; } + if (cur_change.line_pos <= view_offset + uint32_t(height)) { return true; } return false; } @@ -394,16 +360,12 @@ void Model::toggle_case() { } [[nodiscard]] bool Model::redo(const int height) { - if (!redo_stack.size()) { - return false; - } + if (!redo_stack.size()) { return false; } const Change cur_change = redo_stack.top(); redo_stack.pop(); undo_stack.push_back(cur_change); - if (undo_stack.size()) { - unsaved = true; - } + if (undo_stack.size()) { unsaved = true; } rawterm::Pos cur_pos = {int32_t(current_line), int32_t(current_char)}; current_line = cur_change.line_pos; @@ -467,9 +429,7 @@ void Model::move_line_up() { } void Model::set_read_only(std::string_view file) { - if (file == "" || !(file_exists(file))) { - return; - } + if (file == "" || !(file_exists(file))) { return; } readonly = (access(file.data(), W_OK) == -1); } @@ -493,9 +453,7 @@ void Model::delete_current_line() { } // if we aren't at 0, we're on a whitespace, so we want to increment once - if (start) { - start++; - } + if (start) { start++; } uint_t len = uint_t( std::distance( @@ -524,9 +482,7 @@ void Model::delete_current_word(const WordPos pos) { ret.push_back(std::format("|{}| {}", idx + 1, highlighted_line)); } - if (ret.size() == 7) { - break; - } + if (ret.size() == 7) { break; } } return ret; @@ -537,9 +493,7 @@ void Model::delete_current_word(const WordPos pos) { void Model::search_and_replace(const std::string& input) { // TODO: c (confirm) (todo once we have highlighting?) std::vector parts = split_by(input, '|'); - if (parts.size() < 2 || parts.size() > 3) { - return; - } + if (parts.size() < 2 || parts.size() > 3) { return; } auto logger = spdlog::get("basic_logger"); if (logger != nullptr) { @@ -559,9 +513,7 @@ void Model::search_and_replace(const std::string& input) { // NOTE: This is the "find next" used for finding from the command line // for finding the next char, see `find_next` std::optional Model::find_next_str(std::string_view sv) { - if (sv.size() >= 3) { - search_str = sv.substr(3, sv.size()).data(); - } + if (sv.size() >= 3) { search_str = sv.substr(3, sv.size()).data(); } // We're using a single loop here to get both the right line number and // position in the line. This is faster than two complete iterations @@ -569,9 +521,7 @@ std::optional Model::find_next_str(std::string_view sv) { // NOTE: + 1 to avoid searching the current line (cursor may go backward in current line) // TODO: switch to using enumerate for (std::size_t i = current_line + 1; i < buf.size(); i++) { - if (!buf.at(i).contains(search_str)) { - continue; - } + if (!buf.at(i).contains(search_str)) { continue; } const std::size_t str_pos = buf.at(i).find(search_str); return rawterm::Pos {static_cast(i), static_cast(str_pos)}; } diff --git a/src/text_io.cpp b/src/text_io.cpp index e18ac71..a56a076 100644 --- a/src/text_io.cpp +++ b/src/text_io.cpp @@ -15,18 +15,14 @@ #include "view.h" [[nodiscard]] opt_lines_t open_file(const std::string& file) { - if (!get_file_size(file)) { - return {}; - } + if (!get_file_size(file)) { return {}; } auto ret = std::vector(); std::string line = ""; char ch; std::ifstream ifs(file); - if (ifs.fail()) { - return {}; - } + if (ifs.fail()) { return {}; } while (ifs.get(ch)) { switch (ch) { @@ -46,9 +42,7 @@ // If there's no newlines in the stream at all, it never gets added to // the vector - if (line.size()) { - ret.push_back(line); - } + if (line.size()) { ret.push_back(line); } return ret; } @@ -59,19 +53,13 @@ try { return uint_t(fs::file_size(p)); - } catch (const fs::filesystem_error&) { - return 0; - } + } catch (const fs::filesystem_error&) { return 0; } } [[nodiscard]] WriteData write_to_file(Model* model, std::optional filename_input) { - if (!filename_input.has_value() && model->filename == "") { - return WriteData(); - } + if (!filename_input.has_value() && model->filename == "") { return WriteData(); } - if (filename_input.has_value()) { - model->filename = filename_input.value(); - } + if (filename_input.has_value()) { model->filename = filename_input.value(); } std::ofstream out(model->filename); for (auto&& line : model->buf) { @@ -94,9 +82,7 @@ void rtrim(std::string& str) { std::string line; while (std::getline(ss, line)) { - if (line.back() == '\r') { - line.pop_back(); - } + if (line.back() == '\r') { line.pop_back(); } result.push_back(rawterm::raw_str(line)); } @@ -194,9 +180,7 @@ void rtrim(std::string& str) { } auto logger = spdlog::get("basic_logger"); - if (logger != nullptr) { - logger->info("Shell cmd run: `" + cmd + "`"); - } + if (logger != nullptr) { logger->info("Shell cmd run: `" + cmd + "`"); } if (!resp.out.empty() && resp.out.at(resp.out.size() - 1) == '\n') { resp.out = resp.out.substr(0, resp.out.size() - 1); @@ -220,8 +204,6 @@ void rtrim(std::string& str) { [[nodiscard]] int first_non_whitespace(const std::string& line) { std::size_t ret = line.find_first_not_of(WHITESPACE, 0); - if (ret == std::string::npos) { - return -1; - } + if (ret == std::string::npos) { return -1; } return int32_t(ret); } diff --git a/src/view.cpp b/src/view.cpp index f61e5ff..0c118ad 100644 --- a/src/view.cpp +++ b/src/view.cpp @@ -62,9 +62,7 @@ void View::draw_screen() { } } - if (visible_tab_bar() && cur.vertical == 1) { - cur.move_down(); - } + if (visible_tab_bar() && cur.vertical == 1) { cur.move_down(); } if (get_active_model()->type == ModelType::META) { rawterm::Cursor::cursor_hide(); @@ -77,9 +75,7 @@ void View::draw_screen() { std::string screen; // Draw tab bar - if (view_models.size() > 1) { - screen += render_tab_bar(); - } + if (view_models.size() > 1) { screen += render_tab_bar(); } // Get displayable subrange std::size_t end = std::min(std::size_t(view_size.vertical - 2), get_active_model()->buf.size()); @@ -95,9 +91,7 @@ void View::draw_screen() { for (const auto [idx, line] : enumerate(viewable_range, start_idx)) { if (LINE_NUMBERS) { rawterm::Color c = COLOR_UI_BG; - if (idx == get_active_model()->current_line + 1) { - c = COLOR_DARK_YELLOW; - } + if (idx == get_active_model()->current_line + 1) { c = COLOR_DARK_YELLOW; } screen += rawterm::set_foreground(std::format("{:>{}}\u2502", idx, line_number_offset), c); } @@ -107,17 +101,13 @@ void View::draw_screen() { continue; } - if (vert_offset) { - screen += "\u00AB"; - } + if (vert_offset) { screen += "\u00AB"; } if (line.size() > viewable_hor_len) { const std::size_t draw_len = viewable_hor_len - 2 - (vert_offset ? 1 : 0); screen += line.substr(vert_offset, draw_len); - if (line.size() > draw_len + vert_offset + 3) { - screen += "\u00BB"; - } + if (line.size() > draw_len + vert_offset + 3) { screen += "\u00BB"; } } else { screen += line.substr(vert_offset, line.size()); }; @@ -144,14 +134,10 @@ void View::draw_screen() { } void View::draw_tab_bar() { - if (view_models.size() == 1) { - return; - } + if (view_models.size() == 1) { return; } std::string new_tab_bar = render_tab_bar(); - if (new_tab_bar == prev_tab_bar) { - return; - } + if (new_tab_bar == prev_tab_bar) { return; } rawterm::Pos starting_cur_pos = cur; cur.move(1, 1); @@ -189,9 +175,7 @@ const std::string View::render_tab_bar() const { display_name = display_name.substr(0, filename_max_length); } - if (view_models.at(i)->unsaved) { - display_name += "*"; - } + if (view_models.at(i)->unsaved) { display_name += "*"; } if (i == active_model) { ret += rawterm::inverse(display_name); @@ -250,22 +234,16 @@ void View::draw_line(const Draw_Line_dir::values redraw_prev) { } const std::size_t vert_offset = get_active_model()->vertical_offset; - if (curr_line.empty() || (vert_offset && curr_line.size() < vert_offset)) { - return line; - } + if (curr_line.empty() || (vert_offset && curr_line.size() < vert_offset)) { return line; } // Truncate if (curr_line.size() > viewable_hor_len) { - if (vert_offset) { - line += "\u00AB"; - } + if (vert_offset) { line += "\u00AB"; } const std::size_t draw_len = viewable_hor_len - 2 - (vert_offset ? 1 : 0); line += curr_line.substr(vert_offset, draw_len); - if (curr_line.size() > draw_len + vert_offset + 2) { - line += "\u00BB"; - } + if (curr_line.size() > draw_len + vert_offset + 2) { line += "\u00BB"; } } else { line += curr_line.substr(vert_offset, curr_line.size()); @@ -363,9 +341,7 @@ void View::display_message(std::string msg, std::optional color) cur.move({view_size.vertical, 1}); rawterm::clear_line(); - if (color.has_value()) { - msg = rawterm::set_foreground(msg, color.value()); - } + if (color.has_value()) { msg = rawterm::set_foreground(msg, color.value()); } std::print("{}", msg); cur.move(prev_pos); @@ -377,9 +353,7 @@ void View::display_message(std::string msg, std::optional color) // crashes that were down to the offset being wrong after editing text (I think) [[nodiscard]] std::size_t View::clamp_horizontal_movement(const int offset) { const int line_pos = static_cast(get_active_model()->current_line) + offset; - if (line_pos < 0 || line_pos > int32_t(get_active_model()->buf.size())) { - return 0; - } + if (line_pos < 0 || line_pos > int32_t(get_active_model()->buf.size())) { return 0; } std::string_view line_moving_to = get_active_model()->buf.at(static_cast(line_pos)); @@ -396,9 +370,7 @@ void View::display_message(std::string msg, std::optional color) if (get_active_model()->vertical_offset && uint_t(cur.horizontal) == (LINE_NUMBERS ? line_number_offset + 3 : 0)) { get_active_model()->current_char--; - if (get_active_model()->vertical_offset == 2) { - get_active_model()->vertical_offset--; - } + if (get_active_model()->vertical_offset == 2) { get_active_model()->vertical_offset--; } get_active_model()->vertical_offset--; return true; } else if (get_active_model()->current_char) { @@ -412,9 +384,7 @@ void View::display_message(std::string msg, std::optional color) [[maybe_unused]] bool View::cursor_up(unsigned int count) { // If no line above, do nothing - if (!(get_active_model()->current_line)) { - return false; - } + if (!(get_active_model()->current_line)) { return false; } std::size_t horizontal_clamp = clamp_horizontal_movement(int32_t(-count)); get_active_model()->current_line -= count; @@ -442,9 +412,7 @@ void View::display_message(std::string msg, std::optional color) [[maybe_unused]] bool View::cursor_down(unsigned int count) { // If we're on the last line, do nothing - if (get_active_model()->current_line >= get_active_model()->buf.size() - 1) { - return false; - } + if (get_active_model()->current_line >= get_active_model()->buf.size() - 1) { return false; } std::size_t horizontal_clamp = clamp_horizontal_movement(int32_t(count)); get_active_model()->current_line += count; @@ -478,9 +446,7 @@ void View::display_message(std::string msg, std::optional color) // Only scroll if we're still in the line std::string_view curr_line = get_active_model()->buf.at(get_active_model()->current_line); const std::size_t line_size = curr_line.size(); - if (get_active_model()->current_char == line_size) { - return false; - } + if (get_active_model()->current_char == line_size) { return false; } if (cur.horizontal < view_size.horizontal - 2) { get_active_model()->current_char++; @@ -488,9 +454,7 @@ void View::display_message(std::string msg, std::optional color) return false; } else { get_active_model()->current_char++; - if (!get_active_model()->vertical_offset) { - get_active_model()->vertical_offset++; - } + if (!get_active_model()->vertical_offset) { get_active_model()->vertical_offset++; } get_active_model()->vertical_offset++; return true; } @@ -525,9 +489,7 @@ void View::center_current_line() { } void View::set_current_line(const unsigned int lineno) { - if (lineno > get_active_model()->buf.size()) { - return; - } + if (lineno > get_active_model()->buf.size()) { return; } get_active_model()->current_line = lineno - 1; uint_t half_view = static_cast(std::floor(view_size.vertical / 2)); @@ -547,9 +509,7 @@ void View::set_current_line(const unsigned int lineno) { void View::get_git_branch() { auto resp = shell_exec("git rev-parse --abbrev-ref HEAD"); - if (resp.has_value()) { - git_branch = resp.value().out; - } + if (resp.has_value()) { git_branch = resp.value().out; } } void View::tab_new() { @@ -605,9 +565,7 @@ void View::change_model_cursor() { } bool View::set_buffer(const std::size_t bufnr, const std::size_t model_len) { - if (bufnr >= model_len) { - return false; - } + if (bufnr >= model_len) { return false; } view_models.at(active_model) = &ctrlr_ptr->models.at(std::size_t(bufnr)); change_model_cursor(); @@ -629,9 +587,7 @@ void View::draw_overlay(std::span contents, std::string_view title) std::string ret = ""; const std::size_t curr_char = get_active_model()->current_char + 1; - if (curr_char >= LINE_BORDER) { - ret += COLOR_ALERT; - } + if (curr_char >= LINE_BORDER) { ret += COLOR_ALERT; } ret += " "; diff --git a/tests/model_test.cpp b/tests/model_test.cpp index 067e3f1..3cbc379 100644 --- a/tests/model_test.cpp +++ b/tests/model_test.cpp @@ -673,9 +673,7 @@ TEST_CASE("search_and_replace", "[model]") { // multiline flag m.search_and_replace("line|entry|m"); for (std::size_t i = 0; i < m.buf.size(); i++) { - if (i == 3) { - continue; - } + if (i == 3) { continue; } REQUIRE(m.buf.at(i).substr(0, 5) == "entry"); } } diff --git a/tests/view_test.cpp b/tests/view_test.cpp index 3a85042..e89d6c4 100644 --- a/tests/view_test.cpp +++ b/tests/view_test.cpp @@ -28,9 +28,7 @@ TEST_CASE("add_model", "[view]") { REQUIRE(v.active_model == 0); REQUIRE(v.get_active_model()->filename == "test_file.txt"); - if (LINE_NUMBERS) { - REQUIRE(v.line_number_offset == 2); - } + if (LINE_NUMBERS) { REQUIRE(v.line_number_offset == 2); } } TEST_CASE("get_active_model", "[view]") {