From 43fe02ecb4c1c99e4d942488e8982890d8c6a8d7 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sun, 9 Oct 2016 13:32:48 +0300 Subject: Add movement between lines --- editor.cpp | 29 +++++++++++++++++++++++++---- editor.h | 3 +++ 2 files changed, 28 insertions(+), 4 deletions(-) diff --git a/editor.cpp b/editor.cpp index 3eb0d7a..28bd081 100644 --- a/editor.cpp +++ b/editor.cpp @@ -21,6 +21,9 @@ int Editor::run_editor() { case 'e': edit_command(); break; + case 'm': + move_to_command(); + break; default: break; } @@ -29,7 +32,7 @@ int Editor::run_editor() { } int Editor::print_contents_on_line(int line) const { - for (int i = line; i < std::min(m_contents->size(), line + OUTPUT_LINE_MAX); i++) { + for (int i = line; i < std::min((int) m_contents->size(), line + OUTPUT_LINE_MAX); i++) { std::cout << i << " " << m_contents->at(i) << '\n'; } } @@ -37,15 +40,14 @@ int Editor::print_contents_on_line(int line) const { int Editor::edit_command() { std::cout << "Edit which line? "; int line; - std::cin >> line; - if (std::cin.fail() || line < 0 || line >= m_contents->size()) { + std::cin >> line; if (std::cin.fail() || line < 0 || line >= m_contents->size()) { std::cout << "Not a valid line number!\n"; std::cin.clear(); std::cin.ignore(std::numeric_limits::max(),'\n'); return 1; } - edit_line(line); + return edit_line(line); } int Editor::edit_line(int line) { @@ -59,3 +61,22 @@ int Editor::edit_line(int line) { return 0; } + +int Editor::move_to_command() { + std::cout << "Move to which line? "; + int line; + std::cin >> line; + if (std::cin.fail() || line < 0 || line >= m_contents->size()) { + std::cout << "Not a valid line number!\n"; + std::cin.clear(); + std::cin.ignore(std::numeric_limits::max(),'\n'); + return 1; + } + + return move_to_line(line); +} + +int Editor::move_to_line(int line) { + m_current_line = line; + return 0; +} diff --git a/editor.h b/editor.h index 509141f..1ae2a51 100644 --- a/editor.h +++ b/editor.h @@ -16,7 +16,10 @@ class Editor { int print_contents_on_line(int line) const; int edit_command(); + int move_to_command(); + int edit_line(int line); + int move_to_line(int line); }; #endif -- cgit v1.3