diff options
| author | Jan Tuomi <jan-sebastian.tuomi@aalto.fi> | 2016-10-09 14:09:04 +0300 |
|---|---|---|
| committer | Jan Tuomi <jan-sebastian.tuomi@aalto.fi> | 2016-10-09 14:50:24 +0300 |
| commit | e652c840eeba7d8bd4776fdbb45010c4770bdc04 (patch) | |
| tree | c2858b72de3195bb416f47e2e6997e73a3cbdd06 | |
| parent | 06861f43e9898af8a9bccb33d1e1609494238d32 (diff) | |
Add delete line
| -rw-r--r-- | editor.cpp | 26 | ||||
| -rw-r--r-- | editor.h | 2 |
2 files changed, 27 insertions, 1 deletions
@@ -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<std::streamsize>::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; +} @@ -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 |
