Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions src/action.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,11 @@ enum class ActionType {
TriggerUndo,

// Pass value
AddMark, // char
ChangeMode, // Mode
FindNext, // Char
FindPrev, // Char
GoToMark, // char
InsertChar, // Char
ReplaceChar, // Char
};
Expand Down Expand Up @@ -368,6 +370,14 @@ template <typename T, typename U>
return {};
} break;

case ActionType::AddMark: {
if constexpr (std::is_same_v<T, char>) {
v->get_active_model()->add_mark(action.payload);
}

return {};
} break;

case ActionType::ChangeMode: {
if constexpr (std::is_same_v<T, Mode>) {
auto logger = spdlog::get("basic_logger");
Expand Down Expand Up @@ -418,6 +428,17 @@ template <typename T, typename U>
}
} break;

case ActionType::GoToMark: {
if constexpr (std::is_same_v<T, char> && std::is_same_v<U, bool>) {
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<T, char>) {
auto logger = spdlog::get("basic_logger");
Expand Down
18 changes: 8 additions & 10 deletions src/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<char, None>(&view, Action<char> {ActionType::AddMark, k2.code});
}

// TODO: redraw just this line?
redraw_all = true;

Expand Down Expand Up @@ -388,18 +390,14 @@ void Controller::start_action_engine() {
parse_action<Mode, None>(&view, Action<Mode> {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<const bool> ret = parse_action<char, bool>(
&view, Action<char> {ActionType::GoToMark, k2.code});
if (ret.has_value()) { redraw_all = ret.value(); }
}

// Move to beginning/end of line
Expand Down
6 changes: 3 additions & 3 deletions src/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -375,12 +375,12 @@ void View::display_message(std::string msg, std::optional<rawterm::Color> 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;
}
Expand Down Expand Up @@ -450,7 +450,7 @@ void View::display_message(std::string msg, std::optional<rawterm::Color> 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));
Expand Down
8 changes: 4 additions & 4 deletions tests/model_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");
}
Expand All @@ -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) == "");
}
Expand Down
Loading