From 5bc2a0d9b6be834755efe5b09f9795f7495e8483 Mon Sep 17 00:00:00 2001 From: Jan Tuomi Date: Sun, 9 Oct 2016 13:48:34 +0300 Subject: Add quit --- editor.cpp | 20 +++++++++++++++++--- editor.h | 2 ++ 2 files changed, 19 insertions(+), 3 deletions(-) diff --git a/editor.cpp b/editor.cpp index 27169ef..e714a15 100644 --- a/editor.cpp +++ b/editor.cpp @@ -5,7 +5,7 @@ #include Editor::Editor(std::vector* contents, const std::string& filename): - m_current_line(0), m_filename(filename) { + m_current_line(0), m_filename(filename), m_stopped(false) { m_contents = contents; } @@ -13,8 +13,7 @@ Editor::Editor(std::vector* contents, const std::string& filename): int Editor::run_editor() { std::vector* c = m_contents; - bool stopped = false; - while (!stopped) { + while (!m_stopped) { print_contents_on_line(m_current_line); char input = Utils::getch(); @@ -28,6 +27,9 @@ int Editor::run_editor() { case 'w': write_command(); break; + case 'q': + quit_command(); + break; default: break; } @@ -112,3 +114,15 @@ int Editor::write() { return 0; } + +int Editor::quit_command() { + std::cout << "Really quit? [y/n] "; + char answer = Utils::getch(); + + if (answer == 'y') { + m_stopped = true; + std::cout << "\n"; + } + + return 0; +} diff --git a/editor.h b/editor.h index 9d2a773..6693446 100644 --- a/editor.h +++ b/editor.h @@ -14,11 +14,13 @@ class Editor { std::vector* m_contents; std::string m_filename; int m_current_line; + bool m_stopped; int print_contents_on_line(int line) const; int edit_command(); int move_to_command(); int write_command(); + int quit_command(); int edit_line(int line); int move_to_line(int line); -- cgit v1.3