-
Notifications
You must be signed in to change notification settings - Fork 720
/
Copy pathterminal_ui.cc
1500 lines (1313 loc) · 48.7 KB
/
terminal_ui.cc
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "terminal_ui.hh"
#include "display_buffer.hh"
#include "event_manager.hh"
#include "exception.hh"
#include "file.hh"
#include "keys.hh"
#include "ranges.hh"
#include "string_utils.hh"
#include "diff.hh"
#include <algorithm>
#include <fcntl.h>
#include <csignal>
#include <sys/ioctl.h>
#include <unistd.h>
#include <strings.h>
namespace Kakoune
{
using std::min;
using std::max;
static String fix_atom_text(StringView str)
{
String res;
auto pos = str.begin();
for (auto it = str.begin(), end = str.end(); it != end; ++it)
{
char c = *it;
if (c >= 0 and c <= 0x1F)
{
res += StringView{pos, it};
res += String{Codepoint{(uint32_t)(0x2400 + c)}};
pos = it+1;
}
}
res += StringView{pos, str.end()};
return res;
}
struct TerminalUI::Window::Line
{
struct Atom
{
String text;
ColumnCount skip = 0;
Face face;
ColumnCount length() const { return text.column_length() + skip; }
void resize(ColumnCount size)
{
auto it = text.begin(), end = text.end();
while (it != end and size > 0)
size -= codepoint_width(utf8::read_codepoint(it, end));
if (size < 0) // possible if resizing to the middle of a double-width codepoint
{
kak_assert(size == -1);
utf8::to_previous(it, text.begin());
skip = 1;
}
else
skip = size;
text.resize(it - text.begin(), 0);
}
friend bool operator==(const Atom& lhs, const Atom& rhs) { return lhs.text == rhs.text and lhs.skip == rhs.skip and lhs.face == rhs.face; }
friend bool operator!=(const Atom& lhs, const Atom& rhs) { return not (lhs == rhs); }
friend size_t hash_value(const Atom& atom) { return hash_values(atom.text, atom.skip, atom.face); }
};
void append(StringView text, ColumnCount skip, Face face)
{
if (not atoms.empty() and atoms.back().face == face and (atoms.back().skip == 0 or text.empty()))
{
atoms.back().text += fix_atom_text(text);
atoms.back().skip += skip;
}
else
atoms.push_back({fix_atom_text(text), skip, face});
}
void resize(ColumnCount width)
{
auto it = atoms.begin();
ColumnCount column = 0;
for (; it != atoms.end() and column < width; ++it)
column += it->length();
if (column < width)
append({}, width - column, atoms.empty() ? Face{} : atoms.back().face);
else
{
atoms.erase(it, atoms.end());
if (column > width)
atoms.back().resize(atoms.back().length() - (column - width));
}
}
Vector<Atom>::iterator erase_range(ColumnCount pos, ColumnCount len)
{
struct Pos{ Vector<Atom>::iterator it; ColumnCount column; };
auto find_col = [pos=0_col, it=atoms.begin(), end=atoms.end()](ColumnCount col) mutable {
for (; it != end; ++it)
{
auto atom_len = it->length();
if (pos + atom_len >= col)
return Pos{it, col - pos};
pos += atom_len;
}
return Pos{it, 0_col};
};
Pos begin = find_col(pos);
Pos end = find_col(pos+len);
auto make_tail = [](const Atom& atom, ColumnCount from) {
auto it = atom.text.begin(), end = atom.text.end();
while (it != end and from > 0)
from -= codepoint_width(utf8::read_codepoint(it, end));
if (from < 0) // can happen if tail starts in the middle of a double-width codepoint
{
kak_assert(from == -1);
return Atom{" " + StringView{it, end}, atom.skip, atom.face};
}
return Atom{{it, end}, atom.skip - from, atom.face};
};
if (begin.it == end.it)
{
Atom tail = make_tail(*begin.it, end.column);
begin.it->resize(begin.column);
return (tail.text.empty() and tail.skip == 0) ? begin.it+1 : atoms.insert(begin.it+1, tail);
}
begin.it->resize(begin.column);
if (end.column > 0)
{
if (end.column == end.it->length())
++end.it;
else
*end.it = make_tail(*end.it, end.column);
}
return atoms.erase(begin.it+1, end.it);
}
Vector<Atom> atoms;
};
void TerminalUI::Window::create(const DisplayCoord& p, const DisplayCoord& s)
{
kak_assert(p.line >= 0 and p.column >= 0);
kak_assert(s.line >= 0 and s.column >= 0);
pos = p;
size = s;
lines.reset(new Line[(int)size.line]);
}
void TerminalUI::Window::destroy()
{
pos = DisplayCoord{};
size = DisplayCoord{};
lines.reset();
}
void TerminalUI::Window::blit(Window& target)
{
kak_assert(pos.line < target.size.line);
LineCount line_index = pos.line;
for (auto& line : ArrayView{lines.get(), (size_t)size.line})
{
line.resize(size.column);
auto& target_line = target.lines[(size_t)line_index];
target_line.resize(target.size.column);
target_line.atoms.insert(target_line.erase_range(pos.column, size.column), line.atoms.begin(), line.atoms.end());
if (++line_index == target.size.line)
break;
}
}
void TerminalUI::Window::draw(DisplayCoord pos,
ConstArrayView<DisplayAtom> atoms,
const Face& default_face)
{
if (pos.line >= size.line) // We might receive an out of date draw command after a resize
return;
lines[(size_t)pos.line].resize(pos.column);
for (const DisplayAtom& atom : atoms)
{
StringView content = atom.content();
if (content.empty())
continue;
auto face = merge_faces(default_face, atom.face);
if (content.back() == '\n')
lines[(int)pos.line].append(content.substr(0, content.length()-1), 1, face);
else
lines[(int)pos.line].append(content, 0, face);
pos.column += content.column_length();
}
if (pos.column < size.column)
lines[(int)pos.line].append({}, size.column - pos.column, default_face);
}
struct Writer : BufferedWriter<>
{
using Writer::BufferedWriter::BufferedWriter;
~Writer() noexcept(false) = default;
};
template<typename... Args>
static void format_with(Writer& writer, StringView format, Args&&... args)
{
format_with([&](StringView s) { writer.write(s); }, format, std::forward<Args>(args)...);
}
void TerminalUI::Screen::set_face(const Face& face, Writer& writer)
{
static constexpr int fg_table[]{ 39, 30, 31, 32, 33, 34, 35, 36, 37, 90, 91, 92, 93, 94, 95, 96, 97 };
static constexpr int bg_table[]{ 49, 40, 41, 42, 43, 44, 45, 46, 47, 100, 101, 102, 103, 104, 105, 106, 107 };
static constexpr int ul_table[]{ 0, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15 };
static constexpr const char* attr_table[]{ "0", "4", "4:3", "7", "5", "1", "2", "3", "9" };
auto set_color = [&](bool fg, const Color& color, bool join) {
if (join)
writer.write(";");
if (color.isRGB())
format_with(writer, "{};2;{};{};{}", fg ? 38 : 48, color.r, color.g, color.b);
else
format_with(writer, "{}", (fg ? fg_table : bg_table)[(int)(char)color.color]);
};
if (m_active_face == face)
return;
writer.write("\033[");
bool join = false;
if (face.attributes != m_active_face.attributes)
{
for (int i = 0; i < std::size(attr_table); ++i)
{
if (face.attributes & (Attribute)(1 << i))
format_with(writer, ";{}", attr_table[i]);
}
m_active_face.fg = m_active_face.bg = m_active_face.underline = Color::Default;
join = true;
}
if (m_active_face.fg != face.fg)
{
set_color(true, face.fg, join);
join = true;
}
if (m_active_face.bg != face.bg)
{
set_color(false, face.bg, join);
join = true;
}
if (m_active_face.underline != face.underline)
{
if (join)
writer.write(";");
if (face.underline != Color::Default)
{
if (face.underline.isRGB())
format_with(writer, "58:2::{}:{}:{}", face.underline.r, face.underline.g, face.underline.b);
else
format_with(writer, "58:5:{}", ul_table[(int)(char)face.underline.color]);
}
else
format_with(writer, "59");
}
writer.write("m");
m_active_face = face;
}
void TerminalUI::Screen::output(bool force, bool synchronized, Writer& writer)
{
if (not lines)
return;
if (force)
{
std::fill_n(hashes.get(), (size_t)size.line, 0);
writer.write("\033[m");
m_active_face = Face{};
}
auto hash_line = [](const Line& line) {
return (hash_value(line.atoms) << 1) | 1; // ensure non-zero
};
auto output_line = [&](const Line& line) {
ColumnCount pending_move = 0;
for (auto& [text, skip, face] : line.atoms)
{
if (text.empty() and skip == 0)
continue;
if (pending_move != 0)
{
format_with(writer, "\033[{}C", (int)pending_move);
pending_move = 0;
}
set_face(face, writer);
writer.write(text);
if (skip > 3 and face.attributes == Attribute{})
{
writer.write("\033[K");
pending_move = skip;
}
else if (skip > 0)
writer.write(String{' ', skip});
}
};
if (synchronized)
{
writer.write("\033[?2026h"); // begin synchronized update
struct Change { int keep; int add; int del; };
Vector<Change> changes{Change{}};
auto new_hashes = ArrayView{lines.get(), (size_t)size.line} | transform(hash_line);
for_each_diff(hashes.get(), (int)size.line,
new_hashes.begin(), (int)size.line,
[&changes](DiffOp op, int len) mutable {
switch (op)
{
case DiffOp::Keep:
changes.push_back({len, 0, 0});
break;
case DiffOp::Add:
changes.back().add += len;
break;
case DiffOp::Remove:
changes.back().del += len;
break;
}
});
std::copy(new_hashes.begin(), new_hashes.end(), hashes.get());
int line = 0;
for (auto& change : changes)
{
line += change.keep;
if (int del = change.del - change.add; del > 0)
{
format_with(writer, "\033[{}H\033[{}M", line + 1, del);
line -= del;
}
line += change.del;
}
line = 0;
for (auto& change : changes)
{
line += change.keep;
for (int i = 0; i < change.add; ++i)
{
if (int add = change.add - change.del; i == 0 and add > 0)
format_with(writer, "\033[{}H\033[{}L", line + 1, add);
else
format_with(writer, "\033[{}H", line + 1);
output_line(lines[line++]);
}
}
writer.write("\033[?2026l"); // end synchronized update
}
else
{
for (int line = 0; line < (int)size.line; ++line)
{
auto hash = hash_line(lines[line]);
if (hash == hashes[line])
continue;
hashes[line] = hash;
format_with(writer, "\033[{}H", line + 1);
output_line(lines[line]);
}
}
}
constexpr int TerminalUI::default_shift_function_key;
static constexpr StringView assistant_cat[] =
{ R"( ___ )",
R"( (__ \ )",
R"( / / ╭)",
R"( .' '·. │)",
R"( ' ” │)",
R"( ╰ /\_/| │)",
R"( | . \ │)",
R"( ╰_J` | | | ╯)",
R"( ' \__- _/ )",
R"( \_\ \_\ )",
R"( )"};
static constexpr StringView assistant_clippy[] =
{ " ╭──╮ ",
" │ │ ",
" @ @ ╭",
" ││ ││ │",
" ││ ││ ╯",
" │╰─╯│ ",
" ╰───╯ ",
" " };
static constexpr StringView assistant_dilbert[] =
{ R"( დოოოოოდ )",
R"( | | )",
R"( | | ╭)",
R"( |-ᱛ ᱛ-| │)",
R"( Ͼ ∪ Ͽ │)",
R"( | | ╯)",
R"( ˏ`-.ŏ.-´ˎ )",
R"( @ )",
R"( @ )",
R"( )"};
template<typename T> T sq(T x) { return x * x; }
static sig_atomic_t resize_pending = 0;
static sig_atomic_t stdin_closed = 0;
template<sig_atomic_t* signal_flag>
static void signal_handler(int)
{
*signal_flag = 1;
EventManager::instance().force_signal(0);
}
TerminalUI::TerminalUI()
: m_cursor{CursorMode::Buffer, {}},
m_stdin_watcher{STDIN_FILENO, FdEvents::Read, EventMode::Urgent,
[this](FDWatcher&, FdEvents, EventMode) {
if (not m_on_key)
return;
while (auto key = get_next_key())
{
if (key == ctrl('z'))
kill(0, SIGTSTP); // We suspend at this line
else
m_on_key(*key);
}
}},
m_assistant(assistant_clippy)
{
if (not isatty(1))
throw runtime_error("stdout is not a tty");
tcgetattr(STDIN_FILENO, &m_original_termios);
setup_terminal();
set_raw_mode();
enable_mouse(true);
set_signal_handler(SIGWINCH, &signal_handler<&resize_pending>);
set_signal_handler(SIGHUP, &signal_handler<&stdin_closed>);
set_signal_handler(SIGTSTP, [](int){ TerminalUI::instance().suspend(); });
check_resize(true);
redraw(false);
}
TerminalUI::~TerminalUI()
{
enable_mouse(false);
restore_terminal();
tcsetattr(STDIN_FILENO, TCSAFLUSH, &m_original_termios);
set_signal_handler(SIGWINCH, SIG_DFL);
set_signal_handler(SIGHUP, SIG_DFL);
set_signal_handler(SIGTSTP, SIG_DFL);
}
void TerminalUI::suspend()
{
bool mouse_enabled = m_mouse_enabled;
enable_mouse(false);
tcsetattr(STDIN_FILENO, TCSAFLUSH, &m_original_termios);
restore_terminal();
auto current = set_signal_handler(SIGTSTP, SIG_DFL);
sigset_t unblock_sigtstp, old_mask;
sigemptyset(&unblock_sigtstp);
sigaddset(&unblock_sigtstp, SIGTSTP);
sigprocmask(SIG_UNBLOCK, &unblock_sigtstp, &old_mask);
raise(SIGTSTP); // suspend here
set_signal_handler(SIGTSTP, current);
sigprocmask(SIG_SETMASK, &old_mask, nullptr);
setup_terminal();
check_resize(true);
set_raw_mode();
enable_mouse(mouse_enabled);
refresh(true);
}
void TerminalUI::set_raw_mode() const
{
termios attr = m_original_termios;
attr.c_iflag &= ~(IGNBRK | BRKINT | PARMRK | ISTRIP | INLCR | IGNCR | ICRNL | IXON);
attr.c_oflag &= ~OPOST;
attr.c_lflag &= ~(ECHO | ECHONL | ICANON | ISIG | IEXTEN);
attr.c_lflag |= NOFLSH;
attr.c_cflag &= ~(CSIZE | PARENB);
attr.c_cflag |= CS8;
attr.c_cc[VMIN] = attr.c_cc[VTIME] = 0;
tcsetattr(STDIN_FILENO, TCSANOW, &attr);
}
void TerminalUI::redraw(bool force)
{
m_window.blit(m_screen);
if (m_menu.columns != 0 or m_menu.pos.column > m_status_len)
m_menu.blit(m_screen);
m_info.blit(m_screen);
Writer writer{STDOUT_FILENO};
m_screen.output(force, (bool)m_synchronized, writer);
auto set_cursor_pos = [&](DisplayCoord c) {
format_with(writer, "\033[{};{}H", (int)c.line + 1, (int)c.column + 1);
};
if (m_cursor.mode == CursorMode::Prompt)
set_cursor_pos({m_status_on_top ? 0 : m_dimensions.line, m_cursor.coord.column});
else
set_cursor_pos(m_cursor.coord + content_line_offset());
}
void TerminalUI::set_cursor(CursorMode mode, DisplayCoord coord)
{
m_cursor = Cursor{mode, coord};
}
void TerminalUI::refresh(bool force)
{
if (m_dirty or force)
redraw(force);
m_dirty = false;
}
static const DisplayLine empty_line = { String(" "), {} };
void TerminalUI::draw(const DisplayBuffer& display_buffer,
const Face& default_face,
const Face& padding_face)
{
check_resize();
const DisplayCoord dim = dimensions();
const LineCount line_offset = content_line_offset();
LineCount line_index = line_offset;
for (const DisplayLine& line : display_buffer.lines())
m_window.draw(line_index++, line.atoms(), default_face);
auto face = merge_faces(default_face, padding_face);
DisplayAtom padding{String{m_padding_char, m_padding_fill ? dim.column : 1}};
while (line_index < dim.line + line_offset)
m_window.draw(line_index++, padding, face);
m_dirty = true;
}
void TerminalUI::draw_status(const DisplayLine& status_line,
const DisplayLine& mode_line,
const Face& default_face)
{
const LineCount status_line_pos = m_status_on_top ? 0 : m_dimensions.line;
m_window.draw(status_line_pos, status_line.atoms(), default_face);
const auto mode_len = mode_line.length();
m_status_len = status_line.length();
const auto remaining = m_dimensions.column - m_status_len;
if (mode_len < remaining)
{
ColumnCount col = m_dimensions.column - mode_len;
m_window.draw({status_line_pos, col}, mode_line.atoms(), default_face);
}
else if (remaining > 2)
{
DisplayLine trimmed_mode_line = mode_line;
trimmed_mode_line.trim(mode_len + 2 - remaining, remaining - 2);
trimmed_mode_line.insert(trimmed_mode_line.begin(), { "…", {} });
kak_assert(trimmed_mode_line.length() == remaining - 1);
ColumnCount col = m_dimensions.column - remaining + 1;
m_window.draw({status_line_pos, col}, trimmed_mode_line.atoms(), default_face);
}
if (m_set_title)
{
Writer writer{STDOUT_FILENO};
constexpr char suffix[] = " - Kakoune\007";
writer.write("\033]2;");
// Fill title escape sequence buffer, removing non ascii characters
for (auto& atom : mode_line)
{
const auto str = atom.content();
for (auto it = str.begin(), end = str.end(); it != end; utf8::to_next(it, end))
writer.write((*it >= 0x20 and *it <= 0x7e) ? *it : '?');
}
writer.write(suffix);
}
m_dirty = true;
}
void TerminalUI::check_resize(bool force)
{
if (not force and not resize_pending)
return;
resize_pending = 0;
const int fd = open("/dev/tty", O_RDWR);
if (fd < 0)
return;
auto close_fd = on_scope_end([fd]{ ::close(fd); });
DisplayCoord terminal_size{24_line, 80_col};
if (winsize ws; ioctl(fd, TIOCGWINSZ, &ws) == 0 and ws.ws_row > 0 and ws.ws_col > 0)
terminal_size = {ws.ws_row, ws.ws_col};
const bool info = (bool)m_info;
const bool menu = (bool)m_menu;
if (m_window) m_window.destroy();
if (info) m_info.destroy();
if (menu) m_menu.destroy();
m_window.create({0, 0}, terminal_size);
m_screen.create({0, 0}, terminal_size);
m_screen.hashes.reset(new size_t[(int)terminal_size.line]{});
kak_assert(m_window);
m_dimensions = terminal_size - 1_line;
// if (char* csr = tigetstr((char*)"csr"))
// putp(tparm(csr, 0, ws.ws_row));
if (menu)
menu_show(Vector<DisplayLine>(std::move(m_menu.items)),
m_menu.anchor, m_menu.fg, m_menu.bg, m_menu.style);
if (info)
info_show(m_info.title, m_info.content, m_info.anchor, m_info.face, m_info.style);
set_resize_pending();
}
Optional<Key> TerminalUI::get_next_key()
{
if (stdin_closed)
{
set_signal_handler(SIGWINCH, SIG_DFL);
set_signal_handler(SIGHUP, SIG_DFL);
if (m_window)
m_window.destroy();
m_stdin_watcher.disable();
return {};
}
check_resize();
if (m_resize_pending)
{
m_resize_pending = false;
return resize(dimensions());
}
static auto get_char = []() -> Optional<unsigned char> {
if (not fd_readable(STDIN_FILENO))
return {};
if (unsigned char c = 0; read(STDIN_FILENO, &c, 1) == 1)
return c;
stdin_closed = 1;
return {};
};
const auto c = get_char();
if (not c)
return {};
static constexpr auto control = [](char c) { return c & 037; };
auto convert = [this](Codepoint c) -> Codepoint {
if (c == control('m') or c == control('j'))
return Key::Return;
if (c == control('i'))
return Key::Tab;
if (c == ' ')
return Key::Space;
if (c == m_original_termios.c_cc[VERASE])
return Key::Backspace;
if (c == 127) // when it's not backspace
return Key::Delete;
if (c == 27)
return Key::Escape;
return c;
};
auto parse_key = [&convert](unsigned char c) -> Key {
if (Codepoint cp = convert(c); cp > 255)
return Key{cp};
// Special case: you can type NUL with Ctrl-2 or Ctrl-Shift-2 or
// Ctrl-Backtick, but the most straightforward way is Ctrl-Space.
if (c == 0)
return ctrl(Key::Space);
// Represent Ctrl-letter combinations in lower-case, to be clear
// that Shift is not involved.
if (c < 27)
return ctrl(c - 1 + 'a');
// Represent Ctrl-symbol combinations in "upper-case", as they are
// traditionally-rendered.
// Note that Escape is handled elsewhere.
if (c < 32)
return ctrl(c - 1 + 'A');
struct Sentinel{};
struct CharIterator
{
unsigned char operator*() { if (not c) c = get_char().value_or((unsigned char)0); return *c; }
CharIterator& operator++() { c.reset(); return *this; }
bool operator==(const Sentinel&) const { return false; }
Optional<unsigned char> c;
};
return Key{utf8::codepoint(CharIterator{c}, Sentinel{})};
};
auto parse_mask = [](int mask) {
Key::Modifiers mod = Key::Modifiers::None;
if (mask & 1)
mod |= Key::Modifiers::Shift;
if (mask & 2)
mod |= Key::Modifiers::Alt;
if (mask & 4)
mod |= Key::Modifiers::Control;
return mod;
};
auto parse_csi = [this, &convert, &parse_mask]() -> Optional<Key> {
auto next_char = [] { return get_char().value_or((unsigned char)0xff); };
int params[16][4] = {};
auto c = next_char();
char private_mode = 0;
if (c == '?' or c == '<' or c == '=' or c == '>')
{
private_mode = c;
c = next_char();
}
for (int count = 0, subcount = 0; count < 16 and c >= 0x30 && c <= 0x3f; c = next_char())
{
if (isdigit(c))
params[count][subcount] = params[count][subcount] * 10 + c - '0';
else if (c == ':' && subcount < 3)
++subcount;
else if (c == ';')
{
++count;
subcount = 0;
}
else
return {};
}
if (c != '$' and (c < 0x40 or c > 0x7e))
return {};
auto mouse_button = [this](Key::Modifiers mod, Key::MouseButton button, Codepoint coord, bool release) {
auto mask = 1 << (int)button;
if (not release)
{
mod |= (m_mouse_state & mask) ? Key::Modifiers::MousePos : Key::Modifiers::MousePress;
m_mouse_state |= mask;
}
else
{
mod |= Key::Modifiers::MouseRelease;
m_mouse_state &= ~mask;
}
return Key{mod | Key::to_modifier(button), coord};
};
auto mouse_scroll = [this](Key::Modifiers mod, bool down) -> Key {
return {mod | Key::Modifiers::Scroll,
(Codepoint)((down ? 1 : -1) * m_wheel_scroll_amount)};
};
auto masked_key = [&](Codepoint key, Codepoint shifted_key = 0) {
int mask = std::max(params[1][0] - 1, 0);
Key::Modifiers modifiers = parse_mask(mask);
if (shifted_key != 0 and (modifiers & Key::Modifiers::Shift))
{
modifiers &= ~Key::Modifiers::Shift;
key = shifted_key;
}
return Key{modifiers, key};
};
switch (c)
{
case '$':
if (private_mode == '?' and next_char() == 'y') // DECRPM
{
if (params[0][0] == 2026)
m_synchronized.supported = (params[1][0] == 1 or params[1][0] == 2);
return {Key::Invalid};
}
switch (params[0][0])
{
case 23: case 24:
return Key{Key::Modifiers::Shift, Key::F11 + params[0][0] - 23}; // rxvt style
}
return {};
case 'A': return masked_key(Key::Up);
case 'B': return masked_key(Key::Down);
case 'C': return masked_key(Key::Right);
case 'D': return masked_key(Key::Left);
case 'E': return masked_key('5'); // Numeric keypad 5
case 'F': return masked_key(Key::End); // PC/xterm style
case 'H': return masked_key(Key::Home); // PC/xterm style
case 'P': return masked_key(Key::F1);
case 'Q': return masked_key(Key::F2);
case 'R': return masked_key(Key::F3);
case 'S': return masked_key(Key::F4);
case '~':
switch (params[0][0])
{
case 1: return masked_key(Key::Home); // VT220/tmux style
case 2: return masked_key(Key::Insert);
case 3: return masked_key(Key::Delete);
case 4: return masked_key(Key::End); // VT220/tmux style
case 5: return masked_key(Key::PageUp);
case 6: return masked_key(Key::PageDown);
case 7: return masked_key(Key::Home); // rxvt style
case 8: return masked_key(Key::End); // rxvt style
case 11: case 12: case 13: case 14: case 15:
return masked_key(Key::F1 + params[0][0] - 11);
case 17: case 18: case 19: case 20: case 21:
return masked_key(Key::F6 + params[0][0] - 17);
case 23: case 24:
return masked_key(Key::F11 + params[0][0] - 23);
case 25: case 26:
return Key{Key::Modifiers::Shift, Key::F3 + params[0][0] - 25}; // rxvt style
case 28: case 29:
return Key{Key::Modifiers::Shift, Key::F5 + params[0][0] - 28}; // rxvt style
case 31: case 32:
return Key{Key::Modifiers::Shift, Key::F7 + params[0][0] - 31}; // rxvt style
case 33: case 34:
return Key{Key::Modifiers::Shift, Key::F9 + params[0][0] - 33}; // rxvt style
}
return {};
case 'u':
return masked_key(convert(static_cast<Codepoint>(params[0][0])),
convert(static_cast<Codepoint>(params[0][1])));
case 'Z': return shift(Key::Tab);
case 'I': return {Key::FocusIn};
case 'O': return {Key::FocusOut};
case 'M': case 'm':
const bool sgr = private_mode == '<';
if (not sgr and c != 'M')
return {};
const Codepoint b = sgr ? params[0][0] : next_char() - 32;
const int x = (sgr ? params[1][0] : next_char() - 32) - 1;
const int y = (sgr ? params[2][0] : next_char() - 32) - 1;
auto coord = encode_coord({y - content_line_offset(), x});
Key::Modifiers mod = parse_mask((b >> 2) & 0x7);
switch (const int code = b & 0x43; code)
{
case 0: case 1: case 2:
return mouse_button(mod, Key::MouseButton{code}, coord, c == 'm');
case 3:
if (sgr)
return {};
else if (int guess = ffs(m_mouse_state) - 1; 0 <= guess and guess < 3)
return mouse_button(mod, Key::MouseButton{guess}, coord, true);
break;
case 64: return mouse_scroll(mod, false);
case 65: return mouse_scroll(mod, true);
}
return Key{Key::Modifiers::MousePos, coord};
}
return {};
};
auto parse_ss3 = [&parse_mask]() -> Optional<Key> {
int raw_mask = 0;
char code = '0';
do {
raw_mask = raw_mask * 10 + (code - '0');
code = get_char().value_or((unsigned char)0xff);
} while (code >= '0' and code <= '9');
Key::Modifiers mod = parse_mask(std::max(raw_mask - 1, 0));
switch (code)
{
case ' ': return Key{mod, Key::Space};
case 'A': return Key{mod, Key::Up};
case 'B': return Key{mod, Key::Down};
case 'C': return Key{mod, Key::Right};
case 'D': return Key{mod, Key::Left};
case 'F': return Key{mod, Key::End};
case 'H': return Key{mod, Key::Home};
case 'I': return Key{mod, Key::Tab};
case 'M': return Key{mod, Key::Return};
case 'P': return Key{mod, Key::F1};
case 'Q': return Key{mod, Key::F2};
case 'R': return Key{mod, Key::F3};
case 'S': return Key{mod, Key::F4};
case 'X': return Key{mod, '='};
case 'j': return Key{mod, '*'};
case 'k': return Key{mod, '+'};
case 'l': return Key{mod, ','};
case 'm': return Key{mod, '-'};
case 'n': return Key{mod, '.'};
case 'o': return Key{mod, '/'};
case 'p': return Key{mod, '0'};
case 'q': return Key{mod, '1'};
case 'r': return Key{mod, '2'};
case 's': return Key{mod, '3'};
case 't': return Key{mod, '4'};
case 'u': return Key{mod, '5'};
case 'v': return Key{mod, '6'};
case 'w': return Key{mod, '7'};
case 'x': return Key{mod, '8'};
case 'y': return Key{mod, '9'};
default: return {};
}
};
if (*c != 27)
return parse_key(*c);
if (auto next = get_char())
{
if (*next == '[') // potential CSI
return parse_csi().value_or(alt('['));
if (*next == 'O') // potential SS3
return parse_ss3().value_or(alt('O'));
return alt(parse_key(*next));
}
return Key{Key::Escape};
}
template<typename T>
T div_round_up(T a, T b)
{
return (a - T(1)) / b + T(1);
}
void TerminalUI::draw_menu()
{
// menu show may have not created the window if it did not fit.
// so be tolerant.
if (not m_menu)
return;
const int item_count = (int)m_menu.items.size();
if (m_menu.columns == 0)
{
const auto win_width = m_menu.size.column - 4;
kak_assert(m_menu.size.line == 1);
ColumnCount pos = 0;
m_menu.draw({0, 0}, DisplayAtom(m_menu.first_item > 0 ? "< " : ""), m_menu.bg);
int i = m_menu.first_item;
for (; i < item_count and pos < win_width; ++i)
{
const DisplayLine& item = m_menu.items[i];
const ColumnCount item_width = item.length();
auto& face = i == m_menu.selected_item ? m_menu.fg : m_menu.bg;
m_menu.draw({0, pos+2}, item.atoms(), face);
if (pos + item_width >= win_width)
m_menu.draw({0, win_width+2}, DisplayAtom("…"), m_menu.bg);
pos += item_width + 1;
}
if (i != item_count)
m_menu.draw({0, win_width+3}, DisplayAtom(">"), m_menu.bg);
m_dirty = true;
return;
}