From e652c840eeba7d8bd4776fdbb45010c4770bdc04 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sun, 9 Oct 2016 14:09:04 +0300 Subject: Add delete line --- editor.cpp | 26 +++++++++++++++++++++++++- editor.h | 2 ++ 2 files changed, 27 insertions(+), 1 deletion(-) diff --git a/editor.cpp b/editor.cpp index 08abdde..80add20 100644 --- a/editor.cpp +++ b/editor.cpp @@ -30,6 +30,9 @@ int Editor::run_editor() { case 'q': quit_command(); break; + case 'd': + delete_and_clamp_command(); + break; default: break; } @@ -44,12 +47,14 @@ int Editor::print_contents_on_line(int line) const { output_length = line + OUTPUT_LINE_MAX; truncated = true; } - for (int i = line; i < std::min((int) m_contents->size(), line + OUTPUT_LINE_MAX); i++) { + for (int i = line; i < output_length; i++) { std::cout << i << " " << m_contents->at(i) << '\n'; } if (truncated) { std::cout << "...\n"; } + + return 0; } int Editor::edit_command() { @@ -141,3 +146,22 @@ int Editor::quit_command() { return 0; } + +int Editor::delete_and_clamp_command() { + std::cout << "Delete and clamp 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 delete_and_clamp(line); +} + +int Editor::delete_and_clamp(int line) { + m_contents->erase(m_contents->begin() + line); + return 0; +} diff --git a/editor.h b/editor.h index 6693446..645ab63 100644 --- a/editor.h +++ b/editor.h @@ -21,10 +21,12 @@ class Editor { int move_to_command(); int write_command(); int quit_command(); + int delete_and_clamp_command(); int edit_line(int line); int move_to_line(int line); int write(); + int delete_and_clamp(int line); }; #endif -- cgit v1.3