aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Tuomi <jan-sebastian.tuomi@aalto.fi>2016-10-09 14:09:04 +0300
committerJan Tuomi <jan-sebastian.tuomi@aalto.fi>2016-10-09 14:50:24 +0300
commite652c840eeba7d8bd4776fdbb45010c4770bdc04 (patch)
treec2858b72de3195bb416f47e2e6997e73a3cbdd06
parent06861f43e9898af8a9bccb33d1e1609494238d32 (diff)
Add delete line
-rw-r--r--editor.cpp26
-rw-r--r--editor.h2
2 files changed, 27 insertions, 1 deletions
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<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;
+}
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