libxr  1.0
Want to be the best embedded framework
Loading...
Searching...
No Matches
display.hpp
1
17 static size_t HistoryLineSize(const HistoryLine& line)
18 {
19 size_t size = 0;
20 while (size < MAX_LINE_SIZE && line[size] != '\0')
21 {
22 size++;
23 }
24 return size;
25 }
26
41 const HistoryLine& HistoryFromNewest(int index_from_newest)
42 {
43 ASSERT(index_from_newest >= 0);
44 ASSERT(index_from_newest < static_cast<int>(history_.Size()));
45 return history_[-index_from_newest - 1];
46 }
47
56 void LineFeed()
57 {
58 if (MODE == Mode::CRLF)
59 {
60 write_stream_ << ConstRawData("\r\n");
61 }
62 else if (MODE == Mode::LF)
63 {
64 write_stream_ << ConstRawData('\n');
65 }
66 else if (MODE == Mode::CR)
67 {
68 write_stream_ << ConstRawData('\r');
69 }
70 }
71
80 void UpdateDisplayPosition()
81 {
82 write_stream_ << ConstRawData(KEY_SAVE) << ConstRawData(CLEAR_BEHIND)
83 << ConstRawData(&input_line_[input_line_.Size() + offset_], -offset_)
84 << ConstRawData(KEY_LOAD);
85 }
86
92 bool CanDisplayChar() { return input_line_.EmptySize() > 1; }
93
99 bool CanDeleteChar() { return input_line_.Size() + offset_ > 0; }
100
106 void AddCharToInputLine(char data)
107 {
108 if (offset_ == 0)
109 {
110 input_line_.Push(data);
111 }
112 else
113 {
114 input_line_.Insert(data, input_line_.Size() + offset_);
115 }
116 input_line_[input_line_.Size()] = '\0';
117 }
118
125 void DisplayChar(char data)
126 {
127 bool use_history = false;
128
129 if (history_index_ >= 0)
130 {
131 CopyHistoryToInputLine();
132 use_history = true;
133 }
134
135 if (CanDisplayChar())
136 {
137 AddCharToInputLine(data);
138 if (use_history)
139 {
140 ShowHistory();
141 }
142 else
143 {
144 write_stream_ << ConstRawData(input_line_[input_line_.Size() - 1 + offset_]);
145 }
146 if (offset_ != 0)
147 {
148 UpdateDisplayPosition();
149 }
150 }
151 }
152
158 void RemoveCharFromInputLine()
159 {
160 if (offset_ == 0)
161 {
162 input_line_.Pop();
163 }
164 else
165 {
166 input_line_.Delete(input_line_.Size() + offset_ - 1);
167 }
168 input_line_[input_line_.Size()] = '\0';
169 }
170
176 void DeleteChar()
177 {
178 bool use_history = false;
179
180 if (history_index_ >= 0)
181 {
182 CopyHistoryToInputLine();
183 use_history = true;
184 }
185
186 if (CanDeleteChar())
187 {
188 RemoveCharFromInputLine();
189 if (use_history)
190 {
191 ShowHistory();
192 }
193 else
194 {
195 write_stream_ << ConstRawData(DELETE_CHAR);
196 }
197
198 if (offset_ != 0)
199 {
200 UpdateDisplayPosition();
201 }
202 }
203 }
204
209 void ShowHeader()
210 {
211 write_stream_ << ConstRawData(ramfs_.root_.GetName(), strlen(ramfs_.root_.GetName()));
212 if (current_dir_ == &ramfs_.root_)
213 {
214 write_stream_ << ConstRawData(":/");
215 }
216 else
217 {
218 write_stream_ << ConstRawData(":") << ConstRawData(current_dir_->GetName());
219 }
220
221 write_stream_ << ConstRawData("$ ");
222 }
223
228 void ClearLine() { write_stream_ << ConstRawData(CLEAR_LINE); }
229
234 void Clear() { write_stream_ << ConstRawData(CLEAR_ALL); }
235
244 void ShowHistory()
245 {
246 ClearLine();
247 ShowHeader();
248 offset_ = 0;
249 if (history_index_ >= 0)
250 {
251 const auto& line = HistoryFromNewest(history_index_);
252 write_stream_ << ConstRawData(line.data(), HistoryLineSize(line));
253 }
254 else
255 {
256 write_stream_ << ConstRawData(&input_line_[0], input_line_.Size());
257 }
258 }
259
265 void CopyHistoryToInputLine()
266 {
267 input_line_.Reset();
268 const auto& line = HistoryFromNewest(history_index_);
269 for (size_t i = 0; i < HistoryLineSize(line); i++)
270 {
271 input_line_.Push(line[i]);
272 }
273 input_line_[input_line_.Size()] = '\0';
274 history_index_ = -1;
275 offset_ = 0;
276 }
277
286 void AddHistory()
287 {
288 HistoryLine line{};
289 const size_t line_size =
290 LibXR::min(static_cast<size_t>(input_line_.Size()), MAX_LINE_SIZE);
291 input_line_.Push('\0');
292 if (line_size > 0)
293 {
294 std::memcpy(line.data(), &input_line_[0], line_size);
295 }
296 line[line_size] = '\0';
297
298 if (history_.EmptySize() == 0)
299 {
300 history_.Pop();
301 }
302 history_.Push(line);
303 }
constexpr auto min(LeftType a, RightType b) -> std::common_type_t< LeftType, RightType >
计算两个数的最小值