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
18 changes: 18 additions & 0 deletions src/controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -285,9 +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); }
// TODO: redraw just this line?
redraw_all = true;

// add new line and go to insert mode (below)
} else if (k.value() == rawterm::Key('o')) {
Expand Down Expand Up @@ -384,6 +387,21 @@ void Controller::start_action_engine() {
if (quit_flag) { break; }
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;
}

// Move to beginning/end of line
} else if (k.value() == rawterm::Key('_')) {
parse_action<void, None>(&view, Action<void> {ActionType::StartOfLine});
Expand Down
10 changes: 10 additions & 0 deletions src/model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,13 @@ void Model::add_mark(const char c) {
return map.second.line_pos == idx;
}) != marks.end();
}

[[nodiscard]] bool Model::go_to_mark(const char c) {
try {
const Mark& m = marks.at(c);
current_line = m.line_pos;
current_char = m.char_pos;

return true;
} catch (const std::out_of_range& e) { return false; }
}
1 change: 1 addition & 0 deletions src/model.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ struct Model {
void dedent_curr_line();
void add_mark(const char);
[[nodiscard]] bool is_marked(const std::size_t) const;
[[nodiscard]] bool go_to_mark(const char);
};

#endif // MODEL_H
4 changes: 2 additions & 2 deletions src/view.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,8 @@ void View::draw_screen() {
for (const auto& [idx, line] : enumerate<std::string>(viewable_range, start_idx)) {
if (LINE_NUMBERS) {
rawterm::Color c = COLOR_UI_BG;
if (get_active_model()->is_marked(idx - 1)) { c = COLOR_GREEN; }
if (idx == get_active_model()->current_line + 1) { c = COLOR_DARK_YELLOW; }
if (get_active_model()->is_marked(idx)) { c = COLOR_GREEN; }

screen +=
rawterm::set_foreground(std::format("{:>{}}\u2502", idx, line_number_offset), c);
Expand Down Expand Up @@ -231,8 +231,8 @@ void View::draw_line(const Draw_Line_dir::values redraw_prev) {
if (LINE_NUMBERS) {
// TODO: refactor lineno colour into it's own view method
rawterm::Color c = COLOR_UI_BG;
if (idx == get_active_model()->current_line) { c = COLOR_DARK_YELLOW; }
if (get_active_model()->is_marked(idx)) { c = COLOR_GREEN; }
if (idx == get_active_model()->current_line) { c = COLOR_DARK_YELLOW; }

line +=
rawterm::set_foreground(std::format("{:>{}}\u2502", idx + 1, line_number_offset), c);
Expand Down
1 change: 1 addition & 0 deletions tests/fixture/test_file_2.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
testtest
17 changes: 17 additions & 0 deletions tests/integration/normal_mode_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -590,3 +590,20 @@ def test_m_key(r: TmuxRunner):
target_line: str = r.color_screenshot()[3]
assert "4\u2502" in target_line
assert target_line.startswith("\x1b[38;2;95;250;104m")


@setup("tests/fixture/lorem_ipsum.txt")
def test_comma_key(r: TmuxRunner):
r.type_str("j" * 3)
r.type_str("ma")
pos = r.cursor_pos()

r.type_str("j" * 3)
assert r.cursor_pos() != pos

r.type_str("'a")
assert r.cursor_pos() == pos

target_line: str = r.color_screenshot()[3]
assert "4\u2502" in target_line
assert target_line.startswith(r.SELECTED_LINE_ANSI)
17 changes: 17 additions & 0 deletions tests/model_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,3 +753,20 @@ TEST_CASE("is_marked", "[model]") {
REQUIRE(m.is_marked(0));
REQUIRE(!m.is_marked(1));
}

TEST_CASE("go_to_mark", "[model]") {
auto m = Model({" foo", "bar"}, "");
m.current_line = 1;
m.current_char = 1;
m.add_mark('a');
REQUIRE(m.marks.size() == 1);

m.current_line = 0;
m.current_char = 0;

REQUIRE(m.go_to_mark('a'));
REQUIRE(m.current_line == 1);
REQUIRE(m.current_char == 1);

REQUIRE(!m.go_to_mark('b'));
}
Loading