// Buffer.h #include #include class Buffer { public: void display() const; void erase(); const std::string & get_file_name() const { return file_name; } void insert(const std::string & new_line); void move_to_next_line(); void move_to_previous_line(); bool open(const std::string & new_file_name); bool save(const std::string & new_file_name); void set_window_height(int h) { window_height = h; } private: std::vector v_lines; int ix_current_line = 0; int ix_top_line = 0; std::string file_name; int window_height; }; inline void Buffer::erase() { if (ix_current_line < v_lines.size()) v_lines.erase(v_lines.begin() + ix_current_line); } inline void Buffer::insert(const std::string & new_line) { v_lines.insert(v_lines.begin() + ix_current_line, new_line); move_to_next_line(); } inline void Buffer::move_to_next_line() { if (ix_current_line < v_lines.size()) { ++ix_current_line; // check if window needs to be scrolled down if (ix_current_line >= ix_top_line + window_height) ++ix_top_line; } } inline void Buffer::move_to_previous_line() { if (ix_current_line > 0) { --ix_current_line; // check if window needs to be scrolled up if (ix_current_line < ix_top_line) --ix_top_line; } }