aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorJan Tuomi <jan-sebastian.tuomi@aalto.fi>2016-10-09 13:32:48 +0300
committerJan Tuomi <jan-sebastian.tuomi@aalto.fi>2016-10-09 14:50:24 +0300
commit43fe02ecb4c1c99e4d942488e8982890d8c6a8d7 (patch)
tree396b6af21727710beb19d374582ff15468de2945
parent8ce06103b5c5848b48dd31e21b6efd3eefed03d7 (diff)
Add movement between lines
-rw-r--r--editor.cpp29
-rw-r--r--editor.h3
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<std::streamsize>::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<std::streamsize>::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