From 2b77640618425c015bb8e35c06c5ad4eb290ac72 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sun, 9 Oct 2016 14:14:47 +0300 Subject: Add append after line --- editor.cpp | 23 +++++++++++++++++++++++ editor.h | 2 ++ 2 files changed, 25 insertions(+) diff --git a/editor.cpp b/editor.cpp index 80add20..6c86447 100644 --- a/editor.cpp +++ b/editor.cpp @@ -33,6 +33,9 @@ int Editor::run_editor() { case 'd': delete_and_clamp_command(); break; + case 'a': + append_after_command(); + break; default: break; } @@ -165,3 +168,23 @@ int Editor::delete_and_clamp(int line) { m_contents->erase(m_contents->begin() + line); return 0; } + +int Editor::append_after_command() { + std::cout << "Append after 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 append_after_line(line); +} + +int Editor::append_after_line(int line) { + m_contents->insert(m_contents->begin() + line + 1, ""); + return edit_line(line + 1); +} + diff --git a/editor.h b/editor.h index 645ab63..bb429bd 100644 --- a/editor.h +++ b/editor.h @@ -22,11 +22,13 @@ class Editor { int write_command(); int quit_command(); int delete_and_clamp_command(); + int append_after_command(); int edit_line(int line); int move_to_line(int line); int write(); int delete_and_clamp(int line); + int append_after_line(int line); }; #endif -- cgit v1.3