Skip to content

Commit cb22be8

Browse files
committed
rename
1 parent b55926b commit cb22be8

File tree

3 files changed

+85
-85
lines changed

3 files changed

+85
-85
lines changed

cpp-terminal/prompt.cpp

Lines changed: 70 additions & 70 deletions
Original file line numberDiff line numberDiff line change
@@ -184,26 +184,26 @@ void Term::print_left_curly_bracket(Term::Window& scr, const std::size_t& x, con
184184
}
185185
}
186186

187-
void Term::render(Term::Window& scr, const Model& m, const std::size_t& cols)
187+
void Term::render(Term::Window& scr, const Model& model, const std::size_t& cols)
188188
{
189189
scr.clear();
190-
print_left_curly_bracket(scr, cols, 1, m.lines.size());
191-
scr.print_str(cols - 6, m.lines.size(), std::to_string(m.cursor_row) + "," + std::to_string(m.cursor_col));
192-
for(std::size_t j = 0; j < m.lines.size(); j++)
190+
print_left_curly_bracket(scr, cols, 1, model.lines.size());
191+
scr.print_str(cols - 6, model.lines.size(), std::to_string(model.cursor_row) + "," + std::to_string(model.cursor_col));
192+
for(std::size_t j = 0; j < model.lines.size(); j++)
193193
{
194194
if(j == 0)
195195
{
196-
scr.fill_fg(1, j + 1, m.prompt_string.size(), m.lines.size(), Term::Color::Name::Green);
197-
scr.fill_style(1, j + 1, m.prompt_string.size(), m.lines.size(), Term::Style::Bold);
198-
scr.print_str(1, j + 1, m.prompt_string);
196+
scr.fill_fg(1, j + 1, model.prompt_string.size(), model.lines.size(), Term::Color::Name::Green);
197+
scr.fill_style(1, j + 1, model.prompt_string.size(), model.lines.size(), Term::Style::Bold);
198+
scr.print_str(1, j + 1, model.prompt_string);
199199
}
200200
else
201201
{
202-
for(std::size_t i = 0; i < m.prompt_string.size() - 1; i++) { scr.set_char(i + 1, j + 1, '.'); }
202+
for(std::size_t i = 0; i < model.prompt_string.size() - 1; i++) { scr.set_char(i + 1, j + 1, '.'); }
203203
}
204-
scr.print_str(m.prompt_string.size() + 1, j + 1, m.lines[j]);
204+
scr.print_str(model.prompt_string.size() + 1, j + 1, model.lines[j]);
205205
}
206-
scr.set_cursor_pos(m.prompt_string.size() + m.cursor_col, m.cursor_row);
206+
scr.set_cursor_pos(model.prompt_string.size() + model.cursor_col, model.cursor_row);
207207
}
208208

209209
std::string Term::prompt_multiline(const std::string& prompt_string, std::vector<std::string>& m_history, std::function<bool(std::string)>& iscomplete)
@@ -217,18 +217,18 @@ std::string Term::prompt_multiline(const std::string& prompt_string, std::vector
217217
screen = screen_size();
218218
}
219219

220-
Model m;
221-
m.prompt_string = prompt_string;
220+
Model model;
221+
model.prompt_string = prompt_string;
222222

223223
// Make a local copy of history that can be modified by the user. All
224224
// changes will be forgotten once a command is submitted.
225225
std::vector<std::string> history = m_history;
226226
std::size_t history_pos = history.size();
227-
history.push_back(concat(m.lines)); // Push back empty input
227+
history.push_back(concat(model.lines)); // Push back empty input
228228

229229
Term::Window scr(screen.columns(), 1);
230230
Term::Key key;
231-
render(scr, m, screen.columns());
231+
render(scr, model, screen.columns());
232232
std::cout << scr.render(1, cursor.row(), term_attached) << std::flush;
233233
bool not_complete = true;
234234
while(not_complete)
@@ -237,123 +237,123 @@ std::string Term::prompt_multiline(const std::string& prompt_string, std::vector
237237
if(key == Term::Key::NoKey) continue;
238238
if(key.isprint())
239239
{
240-
std::string before = m.lines[m.cursor_row - 1].substr(0, m.cursor_col - 1);
240+
std::string before = model.lines[model.cursor_row - 1].substr(0, model.cursor_col - 1);
241241
std::string newchar;
242242
newchar.push_back(static_cast<char>(key));
243-
std::string after = m.lines[m.cursor_row - 1].substr(m.cursor_col - 1);
244-
m.lines[m.cursor_row - 1] = before += newchar += after;
245-
m.cursor_col++;
243+
std::string after = model.lines[model.cursor_row - 1].substr(model.cursor_col - 1);
244+
model.lines[model.cursor_row - 1] = before += newchar += after;
245+
model.cursor_col++;
246246
}
247247
else if(key == Key::Ctrl_D)
248248
{
249-
if(m.lines.size() == 1 && m.lines[m.cursor_row - 1].empty())
249+
if(model.lines.size() == 1 && model.lines[model.cursor_row - 1].empty())
250250
{
251-
m.lines[m.cursor_row - 1].push_back(static_cast<char>(Key::Ctrl_D));
251+
model.lines[model.cursor_row - 1].push_back(static_cast<char>(Key::Ctrl_D));
252252
std::cout << "\n" << std::flush;
253-
m_history.push_back(m.lines[0]);
254-
return m.lines[0];
253+
m_history.push_back(model.lines[0]);
254+
return model.lines[0];
255255
}
256256
}
257257
else
258258
{
259259
switch(key)
260260
{
261261
case Key::Enter:
262-
not_complete = !iscomplete(concat(m.lines));
262+
not_complete = !iscomplete(concat(model.lines));
263263
if(not_complete) key = Key(static_cast<Term::Key>(Term::MetaKey::Value::Alt + Term::Key::Enter));
264264
else
265265
break;
266266
CPP_TERMINAL_FALLTHROUGH;
267267
case Key::Backspace:
268-
if(m.cursor_col > 1)
268+
if(model.cursor_col > 1)
269269
{
270-
std::string before = m.lines[m.cursor_row - 1].substr(0, m.cursor_col - 2);
271-
std::string after = m.lines[m.cursor_row - 1].substr(m.cursor_col - 1);
272-
m.lines[m.cursor_row - 1] = before + after;
273-
m.cursor_col--;
270+
std::string before = model.lines[model.cursor_row - 1].substr(0, model.cursor_col - 2);
271+
std::string after = model.lines[model.cursor_row - 1].substr(model.cursor_col - 1);
272+
model.lines[model.cursor_row - 1] = before + after;
273+
model.cursor_col--;
274274
}
275-
else if(m.cursor_col == 1 && m.cursor_row > 1)
275+
else if(model.cursor_col == 1 && model.cursor_row > 1)
276276
{
277-
m.cursor_col = m.lines[m.cursor_row - 2].size() + 1;
278-
m.lines[m.cursor_row - 2] += m.lines[m.cursor_row - 1];
279-
m.lines.erase(m.lines.begin() + static_cast<long>(m.cursor_row) - 1);
280-
m.cursor_row--;
277+
model.cursor_col = model.lines[model.cursor_row - 2].size() + 1;
278+
model.lines[model.cursor_row - 2] += model.lines[model.cursor_row - 1];
279+
model.lines.erase(model.lines.begin() + static_cast<long>(model.cursor_row) - 1);
280+
model.cursor_row--;
281281
}
282282
break;
283283
case Key::Del:
284-
if(m.cursor_col <= m.lines[m.cursor_row - 1].size())
284+
if(model.cursor_col <= model.lines[model.cursor_row - 1].size())
285285
{
286-
std::string before = m.lines[m.cursor_row - 1].substr(0, m.cursor_col - 1);
287-
std::string after = m.lines[m.cursor_row - 1].substr(m.cursor_col);
288-
m.lines[m.cursor_row - 1] = before + after;
286+
std::string before = model.lines[model.cursor_row - 1].substr(0, model.cursor_col - 1);
287+
std::string after = model.lines[model.cursor_row - 1].substr(model.cursor_col);
288+
model.lines[model.cursor_row - 1] = before + after;
289289
}
290290
break;
291291
case Key::ArrowLeft:
292-
if(m.cursor_col > 1) { m.cursor_col--; }
292+
if(model.cursor_col > 1) { model.cursor_col--; }
293293
break;
294294
case Key::ArrowRight:
295-
if(m.cursor_col <= m.lines[m.cursor_row - 1].size()) { m.cursor_col++; }
295+
if(model.cursor_col <= model.lines[model.cursor_row - 1].size()) { model.cursor_col++; }
296296
break;
297-
case Key::Home: m.cursor_col = 1; break;
298-
case Key::End: m.cursor_col = m.lines[m.cursor_row - 1].size() + 1; break;
297+
case Key::Home: model.cursor_col = 1; break;
298+
case Key::End: model.cursor_col = model.lines[model.cursor_row - 1].size() + 1; break;
299299
case Key::ArrowUp:
300-
if(m.cursor_row == 1)
300+
if(model.cursor_row == 1)
301301
{
302302
if(history_pos > 0)
303303
{
304-
history[history_pos] = concat(m.lines);
304+
history[history_pos] = concat(model.lines);
305305
history_pos--;
306-
m.lines = split(history[history_pos]);
307-
m.cursor_row = m.lines.size();
308-
if(m.cursor_col > m.lines[m.cursor_row - 1].size() + 1) { m.cursor_col = m.lines[m.cursor_row - 1].size() + 1; }
309-
if(m.lines.size() > scr.get_h()) { scr.set_h(m.lines.size()); }
306+
model.lines = split(history[history_pos]);
307+
model.cursor_row = model.lines.size();
308+
if(model.cursor_col > model.lines[model.cursor_row - 1].size() + 1) { model.cursor_col = model.lines[model.cursor_row - 1].size() + 1; }
309+
if(model.lines.size() > scr.get_h()) { scr.set_h(model.lines.size()); }
310310
}
311311
}
312312
else
313313
{
314-
m.cursor_row--;
315-
if(m.cursor_col > m.lines[m.cursor_row - 1].size() + 1) { m.cursor_col = m.lines[m.cursor_row - 1].size() + 1; }
314+
model.cursor_row--;
315+
if(model.cursor_col > model.lines[model.cursor_row - 1].size() + 1) { model.cursor_col = model.lines[model.cursor_row - 1].size() + 1; }
316316
}
317317
break;
318318
case Key::ArrowDown:
319-
if(m.cursor_row == m.lines.size())
319+
if(model.cursor_row == model.lines.size())
320320
{
321321
if(history_pos < history.size() - 1)
322322
{
323-
history[history_pos] = concat(m.lines);
323+
history[history_pos] = concat(model.lines);
324324
history_pos++;
325-
m.lines = split(history[history_pos]);
326-
m.cursor_row = 1;
327-
if(m.cursor_col > m.lines[m.cursor_row - 1].size() + 1) { m.cursor_col = m.lines[m.cursor_row - 1].size() + 1; }
328-
if(m.lines.size() > scr.get_h()) { scr.set_h(m.lines.size()); }
325+
model.lines = split(history[history_pos]);
326+
model.cursor_row = 1;
327+
if(model.cursor_col > model.lines[model.cursor_row - 1].size() + 1) { model.cursor_col = model.lines[model.cursor_row - 1].size() + 1; }
328+
if(model.lines.size() > scr.get_h()) { scr.set_h(model.lines.size()); }
329329
}
330330
}
331331
else
332332
{
333-
m.cursor_row++;
334-
if(m.cursor_col > m.lines[m.cursor_row - 1].size() + 1) { m.cursor_col = m.lines[m.cursor_row - 1].size() + 1; }
333+
model.cursor_row++;
334+
if(model.cursor_col > model.lines[model.cursor_row - 1].size() + 1) { model.cursor_col = model.lines[model.cursor_row - 1].size() + 1; }
335335
}
336336
break;
337337
case Key::Ctrl_N:
338338
{
339-
std::string before = m.lines[m.cursor_row - 1].substr(0, m.cursor_col - 1);
340-
std::string after = m.lines[m.cursor_row - 1].substr(m.cursor_col - 1);
341-
m.lines[m.cursor_row - 1] = before;
342-
if(m.cursor_row < m.lines.size())
339+
std::string before = model.lines[model.cursor_row - 1].substr(0, model.cursor_col - 1);
340+
std::string after = model.lines[model.cursor_row - 1].substr(model.cursor_col - 1);
341+
model.lines[model.cursor_row - 1] = before;
342+
if(model.cursor_row < model.lines.size())
343343
{
344344
// Not at the bottom row, can't push back
345-
m.lines.insert(m.lines.begin() + static_cast<long>(m.cursor_row), after);
345+
model.lines.insert(model.lines.begin() + static_cast<long>(model.cursor_row), after);
346346
}
347-
else { m.lines.push_back(after); }
348-
m.cursor_col = 1;
349-
m.cursor_row++;
350-
if(m.lines.size() > scr.get_h()) { scr.set_h(m.lines.size()); }
347+
else { model.lines.push_back(after); }
348+
model.cursor_col = 1;
349+
model.cursor_row++;
350+
if(model.lines.size() > scr.get_h()) { scr.set_h(model.lines.size()); }
351351
break;
352352
}
353353
default: break;
354354
}
355355
}
356-
render(scr, m, screen.columns());
356+
render(scr, model, screen.columns());
357357
std::cout << scr.render(1, cursor.row(), term_attached) << std::flush;
358358
if(cursor.row() + (int)scr.get_h() - 1 > screen.rows())
359359
{
@@ -362,8 +362,8 @@ std::string Term::prompt_multiline(const std::string& prompt_string, std::vector
362362
}
363363
}
364364
std::string line_skips;
365-
for(std::size_t i = 0; i <= m.lines.size() - m.cursor_row; i++) { line_skips += "\n"; }
365+
for(std::size_t i = 0; i <= model.lines.size() - model.cursor_row; i++) { line_skips += "\n"; }
366366
std::cout << line_skips << std::flush;
367-
m_history.push_back(concat(m.lines));
368-
return concat(m.lines);
367+
m_history.push_back(concat(model.lines));
368+
return concat(model.lines);
369369
}

cpp-terminal/window.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
namespace Term
2424
{
2525

26-
Term::Window::Window(const std::size_t& columns, const std::size_t& rows) : m_window({rows, columns}) { clear(); }
26+
Term::Window::Window(const std::size_t& columns, const std::size_t& rows) : m_size({rows, columns}) { clear(); }
2727

2828
char32_t Term::Window::get_char(const std::size_t& column, const std::size_t& row) { return m_chars[index(column, row)]; }
2929

@@ -37,9 +37,9 @@ Term::Color Term::Window::get_bg(const std::size_t& column, const std::size_t& r
3737

3838
Term::Style Term::Window::get_style(const std::size_t& column, const std::size_t& row) { return m_style[index(column, row)]; }
3939

40-
std::size_t Term::Window::get_w() const { return m_window.columns(); }
40+
std::size_t Term::Window::get_w() const { return m_size.columns(); }
4141

42-
std::size_t Term::Window::get_h() const { return m_window.rows(); }
42+
std::size_t Term::Window::get_h() const { return m_size.rows(); }
4343

4444
void Term::Window::set_char(const std::size_t& column, const std::size_t& row, const char32_t& character)
4545
{
@@ -77,17 +77,17 @@ void Term::Window::set_cursor_pos(const std::size_t& column, const std::size_t&
7777

7878
void Term::Window::set_h(const std::size_t& new_h)
7979
{
80-
if(new_h == m_window.rows()) { return; }
81-
if(new_h > m_window.rows())
80+
if(new_h == m_size.rows()) { return; }
81+
if(new_h > m_size.rows())
8282
{
83-
const std::size_t dc = (new_h - m_window.rows()) * m_window.columns();
83+
const std::size_t dc = (new_h - m_size.rows()) * m_size.columns();
8484
m_chars.insert(m_chars.end(), dc, ' ');
8585
m_fg_reset.insert(m_fg_reset.end(), dc, true);
8686
m_bg_reset.insert(m_bg_reset.end(), dc, true);
8787
m_fg.insert(m_fg.end(), dc, {0, 0, 0});
8888
m_bg.insert(m_bg.end(), dc, {0, 0, 0});
8989
m_style.insert(m_style.end(), dc, Style::Reset);
90-
m_window = {m_window.columns(), new_h};
90+
m_size = {m_size.columns(), new_h};
9191
}
9292
else { throw Term::Exception("Shrinking height not supported."); }
9393
}
@@ -143,7 +143,7 @@ void Term::Window::fill_style(const std::size_t& x1, const std::size_t& y1, cons
143143
}
144144
}
145145

146-
void Term::Window::print_border() { print_rect(1, 1, m_window.columns(), m_window.rows()); }
146+
void Term::Window::print_border() { print_rect(1, 1, m_size.columns(), m_size.rows()); }
147147

148148
void Term::Window::print_rect(const std::size_t& x1, const std::size_t& y1, const std::size_t& x2, const std::size_t& y2)
149149
{
@@ -186,7 +186,7 @@ void Term::Window::print_rect(const std::size_t& x1, const std::size_t& y1, cons
186186

187187
void Term::Window::clear()
188188
{
189-
const std::size_t area{m_window.rows() * m_window.columns()};
189+
const std::size_t area{m_size.rows() * m_size.columns()};
190190
m_style.assign(area, Style::Reset);
191191
m_bg_reset.assign(area, true);
192192
m_bg.assign(area, Term::Color::Name::Default);
@@ -204,10 +204,10 @@ std::string Term::Window::render(const std::size_t& x0, const std::size_t& y0, b
204204
bool current_fg_reset = true;
205205
bool current_bg_reset = true;
206206
Style current_style = Style::Reset;
207-
for(std::size_t j = 1; j <= m_window.rows(); ++j)
207+
for(std::size_t j = 1; j <= m_size.rows(); ++j)
208208
{
209209
if(term) { out.append(cursor_move(y0 + j - 1, x0)); }
210-
for(std::size_t i = 1; i <= m_window.columns(); ++i)
210+
for(std::size_t i = 1; i <= m_size.columns(); ++i)
211211
{
212212
bool update_fg = false;
213213
bool update_bg = false;
@@ -282,7 +282,7 @@ std::string Term::Window::render(const std::size_t& x0, const std::size_t& y0, b
282282
}
283283
out.append(Private::utf32_to_utf8(get_char(i, j)));
284284
}
285-
if(j < m_window.rows()) { out.append("\n"); }
285+
if(j < m_size.rows()) { out.append("\n"); }
286286
}
287287
if(!current_fg_reset) { out.append(color_fg(Term::Color::Name::Default)); }
288288
if(!current_bg_reset) { out.append(color_bg(Term::Color::Name::Default)); }
@@ -298,8 +298,8 @@ std::string Term::Window::render(const std::size_t& x0, const std::size_t& y0, b
298298
std::size_t Term::Window::index(const std::size_t& column, const std::size_t& row) const
299299
{
300300
if(!insideWindow(column, row)) { throw Term::Exception("Cursor out of range"); }
301-
return ((row - 1) * m_window.columns()) + (column - 1);
301+
return ((row - 1) * m_size.columns()) + (column - 1);
302302
}
303303

304-
bool Term::Window::insideWindow(const std::size_t& column, const std::size_t& row) const { return (column >= 1) && (row >= 1) && (column <= m_window.columns()) && (row <= m_window.rows()); }
304+
bool Term::Window::insideWindow(const std::size_t& column, const std::size_t& row) const { return (column >= 1) && (row >= 1) && (column <= m_size.columns()) && (row <= m_size.rows()); }
305305
} // namespace Term

cpp-terminal/window.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class Window
7878

7979
private:
8080
std::size_t index(const std::size_t& column, const std::size_t& row) const;
81-
Term::Screen m_window{0, 0};
81+
Term::Screen m_size{0, 0};
8282
Term::Cursor m_cursor{1, 1};
8383
std::vector<char32_t> m_chars; // the characters in row first order
8484
std::vector<Term::Color> m_fg;

0 commit comments

Comments
 (0)