// Buffer.h #include #include #include #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_; } }