diff --git a/src/action.h b/src/action.h index 6c5ced7..baf0b29 100644 --- a/src/action.h +++ b/src/action.h @@ -42,9 +42,11 @@ enum class ActionType { TriggerUndo, // Pass value + AddMark, // char ChangeMode, // Mode FindNext, // Char FindPrev, // Char + GoToMark, // char InsertChar, // Char ReplaceChar, // Char }; @@ -368,6 +370,14 @@ template return {}; } break; + case ActionType::AddMark: { + if constexpr (std::is_same_v) { + v->get_active_model()->add_mark(action.payload); + } + + return {}; + } break; + case ActionType::ChangeMode: { if constexpr (std::is_same_v) { auto logger = spdlog::get("basic_logger"); @@ -418,6 +428,17 @@ template } } break; + case ActionType::GoToMark: { + if constexpr (std::is_same_v && std::is_same_v) { + bool ret = v->get_active_model()->go_to_mark(action.payload); + + // TODO: rename this method to something like "replace cursor" + v->change_model_cursor(); + return ret; + } + return {}; + } break; + case ActionType::InsertChar: { if constexpr (std::is_same_v) { auto logger = spdlog::get("basic_logger"); diff --git a/src/controller.cpp b/src/controller.cpp index 913ef63..8f10763 100644 --- a/src/controller.cpp +++ b/src/controller.cpp @@ -285,10 +285,12 @@ void Controller::start_action_engine() { redraw_all = redraw.value(); // Add mark - // TODO: Make sure this becomes an action instead } else if (k.value() == rawterm::Key('m')) { auto k2 = rawterm::wait_for_input(); - if (k2.isCharInput()) { view.get_active_model()->add_mark(k2.code); } + if (k2.isCharInput()) { + parse_action(&view, Action {ActionType::AddMark, k2.code}); + } + // TODO: redraw just this line? redraw_all = true; @@ -388,18 +390,14 @@ void Controller::start_action_engine() { parse_action(&view, Action {ActionType::ChangeMode, Mode::Read}); // go to mark - // TODO: Make sure this becomes an action instead } else if (k.value() == rawterm::Key('\'')) { auto k2 = rawterm::wait_for_input(); if (k2.isCharInput()) { - redraw_all = view.get_active_model()->go_to_mark(k2.code); - - // TODO: rename this method to something like "replace cursor" - view.change_model_cursor(); - // TODO: calculate if the marked line wasn't currently on screen - // and redraw if needed - redraw_all = true; + // and only redraw if needed + std::optional ret = parse_action( + &view, Action {ActionType::GoToMark, k2.code}); + if (ret.has_value()) { redraw_all = ret.value(); } } // Move to beginning/end of line diff --git a/src/view.cpp b/src/view.cpp index 9bf7fe3..644a0e1 100644 --- a/src/view.cpp +++ b/src/view.cpp @@ -375,12 +375,12 @@ void View::display_message(std::string msg, std::optional color) [[maybe_unused]] bool View::cursor_left(std::size_t dist) { if (get_active_model()->vertical_offset && uint_t(cur.horizontal) == (LINE_NUMBERS ? line_number_offset + 3 : 0)) { - get_active_model()->current_char -= dist; + get_active_model()->current_char -= uint_t(dist); if (get_active_model()->vertical_offset == 2) { get_active_model()->vertical_offset--; } get_active_model()->vertical_offset -= dist; return true; } else if (get_active_model()->current_char) { - get_active_model()->current_char -= dist; + get_active_model()->current_char -= uint_t(dist); cur.move_left(int32_t(dist)); return false; } @@ -450,7 +450,7 @@ void View::display_message(std::string msg, std::optional color) // Clamp dist to line dist = std::min(dist, line_size - get_active_model()->current_char); if (dist == 0) { return false; } - get_active_model()->current_char += dist; + get_active_model()->current_char += uint_t(dist); if (cur.horizontal < view_size.horizontal - 2) { cur.move_right(int32_t(dist)); diff --git a/tests/model_test.cpp b/tests/model_test.cpp index 34d0c96..93f57db 100644 --- a/tests/model_test.cpp +++ b/tests/model_test.cpp @@ -569,13 +569,13 @@ TEST_CASE("redo", "[model]") { TEST_CASE("move_line_down", "[model]") { auto m = Model({"line one", "line two", "line three", "", "line four", "line five"}, ""); - m.move_line_down(); + REQUIRE(m.move_line_down()); REQUIRE(m.buf.at(0) == "line two"); REQUIRE(m.buf.at(1) == "line one"); m.current_line++; - m.move_line_down(); + REQUIRE(m.move_line_down()); REQUIRE(m.buf.at(1) == "line three"); REQUIRE(m.buf.at(2) == "line one"); } @@ -584,13 +584,13 @@ TEST_CASE("move_line_up", "[model]") { auto m = Model({"line one", "line two", "line three", "", "line four", "line five"}, ""); m.current_line = 5; - m.move_line_up(); + REQUIRE(m.move_line_up()); REQUIRE(m.buf.at(4) == "line five"); REQUIRE(m.buf.at(5) == "line four"); m.current_line--; - m.move_line_up(); + REQUIRE(m.move_line_up()); REQUIRE(m.buf.at(3) == "line five"); REQUIRE(m.buf.at(4) == ""); }