-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathinput_history_navigation.cpp
More file actions
117 lines (98 loc) · 3.7 KB
/
Copy pathinput_history_navigation.cpp
File metadata and controls
117 lines (98 loc) · 3.7 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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
#include "input_history_navigation.hpp"
#include <utility>
namespace acecode::tui {
namespace {
constexpr int kClearedInputRestoreIndex = -2;
} // namespace
bool try_cancel_latest_pending_for_history_text(TuiState& state,
const std::string& text) {
if (state.pending_queue.empty() || state.pending_queue.back() != text) {
return false;
}
state.pending_queue.pop_back();
if (!state.pending_structured_queue.empty() &&
state.pending_structured_queue.back().display_text == text) {
state.pending_structured_queue.pop_back();
}
return true;
}
bool clear_current_input_for_history_restore(TuiState& state) {
if (state.input_text.empty() && state.input_mode == InputMode::Normal) {
return false;
}
state.saved_input = prepend_mode_prefix(state.input_text, state.input_mode);
state.history_index = kClearedInputRestoreIndex;
state.input_mode = InputMode::Normal;
state.input_text.clear();
state.input_cursor = 0;
state.clear_input_selection();
return true;
}
bool navigate_input_history_up(TuiState& state) {
if (state.history_index == kClearedInputRestoreIndex) {
auto [saved_mode, saved_text] = parse_mode_prefix(state.saved_input);
state.input_mode = saved_mode;
state.input_text = std::move(saved_text);
state.input_cursor = state.input_text.size();
state.clear_input_selection();
state.history_index = -1;
state.saved_input.clear();
return true;
}
if (state.input_history.empty()) {
return false;
}
if (state.history_index == -1) {
state.saved_input = prepend_mode_prefix(state.input_text, state.input_mode);
state.history_index = static_cast<int>(state.input_history.size()) - 1;
} else if (state.history_index > 0) {
--state.history_index;
}
if (state.history_index < 0 ||
state.history_index >= static_cast<int>(state.input_history.size())) {
state.history_index = -1;
return false;
}
auto [hist_mode, hist_text] =
parse_mode_prefix(state.input_history[state.history_index]);
if (hist_mode == InputMode::Normal) {
try_cancel_latest_pending_for_history_text(state, hist_text);
}
state.input_mode = hist_mode;
state.input_text = std::move(hist_text);
state.input_cursor = state.input_text.size();
state.clear_input_selection();
return true;
}
bool navigate_input_history_down(TuiState& state) {
if (state.history_index == -1 ||
state.history_index == kClearedInputRestoreIndex) {
return false;
}
if (state.input_history.empty() ||
state.history_index >= static_cast<int>(state.input_history.size())) {
state.history_index = -1;
auto [saved_mode, saved_text] = parse_mode_prefix(state.saved_input);
state.input_mode = saved_mode;
state.input_text = std::move(saved_text);
state.input_cursor = state.input_text.size();
state.clear_input_selection();
return true;
}
if (state.history_index < static_cast<int>(state.input_history.size()) - 1) {
++state.history_index;
auto [hist_mode, hist_text] =
parse_mode_prefix(state.input_history[state.history_index]);
state.input_mode = hist_mode;
state.input_text = std::move(hist_text);
} else {
state.history_index = -1;
auto [saved_mode, saved_text] = parse_mode_prefix(state.saved_input);
state.input_mode = saved_mode;
state.input_text = std::move(saved_text);
}
state.input_cursor = state.input_text.size();
state.clear_input_selection();
return true;
}
} // namespace acecode::tui