-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommand_manager.cpp
More file actions
44 lines (39 loc) · 1.21 KB
/
command_manager.cpp
File metadata and controls
44 lines (39 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
#include <iostream>
#include "command_manager.hpp"
namespace text_editor
{
void CommandManager::executeCommand(Command* cmd, rope& r) {
if (current_ + 1 < history_.size()) {
history_.erase(history_.begin() + current_ + 1, history_.end());
}
cmd->execute(r);
if (history_.size() >= 25) {
history_.erase(history_.begin());
current_--;
}
history_.push_back(std::unique_ptr<Command>(cmd));
current_ = history_.size() - 1;
}
void CommandManager::undo(rope& r) {
if (current_ >= 0) {
history_[current_]->undo(r);
current_--;
}
}
void CommandManager::redo(rope& r) {
if (current_ + 1 < history_.size()) {
current_++;
history_[current_]->execute(r);
}
}
void CommandManager::printCommandHistory() {
std::cout << "История команд: ";
for (size_t i = 0; i < history_.size(); ++i) {
if (i == current_) {
std::cout << "-> ";
}
std::cout << "Команда " << i + 1 << (i == history_.size()-1 ? "" : ", ");
}
std::cout << std::endl;
}
}