// TextEditor.cpp #include "TextEditor.h" using namespace std; void TextEditor::display() { const string long_separator(50, '-'); const string short_separator(8, '-'); system(clear_command); string file_name = buffer_.get_file_name(); if (file_name.empty()) cout << "\n"; else cout << file_name << endl; cout << long_separator << endl; buffer_.display(); cout << long_separator << endl; cout << " next jump insert open quit\n"; cout << " previous replace delete save\n"; cout << short_separator << endl; if (!error_message_.empty()) { cout << "ERROR: " + error_message_ << endl; error_message_.clear(); } } void TextEditor::execute_command(char command, bool & done) { switch (command) { case 'd': { buffer_.erase(); break; } case 'i': { cout << "new line: "; string new_line; getline(cin, new_line); buffer_.insert(new_line); break; } case 'j': { error_message_ = "Command jump not yet implemented"; break; } case 'n': { buffer_.move_to_next_line(); break; } case 'o': { cout << "file name: "; string file_name; getline(cin, file_name); if (!buffer_.open(file_name)) error_message_ = "Could not open " + file_name; break; } case 'p': { buffer_.move_to_previous_line(); break; } case 'q': { done = true; break; } case 'r': { error_message_ = "Command replace not yet implemented"; break; } case 's': { cout << "file name: "; string file_name; getline(cin, file_name); if (!buffer_.save(file_name)) error_message_ = "Could not open " + file_name; break; } } } void TextEditor::run() { cout << "Window height? "; cin >> window_height_; cin.get(); // '\n' cout << '\n'; buffer_.set_window_height(window_height_); bool done = false; while (!done) { display(); cout << "choice: "; char command; cin >> command; cin.get(); // '\n' execute_command(command, done); cout << endl; } }