aboutsummaryrefslogtreecommitdiff
path: root/examples/dte/screen-cmdline.c
diff options
context:
space:
mode:
authorMitja Felicijan <mitja.felicijan@gmail.com>2023-11-09 23:19:53 +0100
committerMitja Felicijan <mitja.felicijan@gmail.com>2023-11-09 23:19:53 +0100
commit1566b6faa8534118c3566188181367cd0868468f (patch)
tree1de8d4b369efb5e592685a31088f798a6b63ffa1 /examples/dte/screen-cmdline.c
parent349991bf6efe473ab9a5cbdae0a8114d72b997e3 (diff)
downloadcrep-1566b6faa8534118c3566188181367cd0868468f.tar.gz
Added partial matching and introduced threads
Diffstat (limited to 'examples/dte/screen-cmdline.c')
-rw-r--r--examples/dte/screen-cmdline.c91
1 files changed, 91 insertions, 0 deletions
diff --git a/examples/dte/screen-cmdline.c b/examples/dte/screen-cmdline.c
new file mode 100644
index 0000000..58d04c4
--- /dev/null
+++ b/examples/dte/screen-cmdline.c
@@ -0,0 +1,91 @@
1#include "screen.h"
2#include "error.h"
3#include "search.h"
4
5static void print_message(Terminal *term, const ColorScheme *colors, const char *msg, bool is_error)
6{
7 BuiltinColorEnum c = BC_COMMANDLINE;
8 if (msg[0]) {
9 c = is_error ? BC_ERRORMSG : BC_INFOMSG;
10 }
11
12 TermOutputBuffer *obuf = &term->obuf;
13 set_builtin_color(term, colors, c);
14
15 for (size_t i = 0; msg[i]; ) {
16 CodePoint u = u_get_char(msg, i + 4, &i);
17 if (!term_put_char(obuf, u)) {
18 break;
19 }
20 }
21}
22
23void show_message(Terminal *term, const ColorScheme *colors, const char *msg, bool is_error)
24{
25 term_output_reset(term, 0, term->width, 0);
26 term_move_cursor(&term->obuf, 0, term->height - 1);
27 print_message(term, colors, msg, is_error);
28 term_clear_eol(term);
29}
30
31static size_t print_command(Terminal *term, const ColorScheme *colors, const CommandLine *cmdline, char prefix)
32{
33 const String *buf = &cmdline->buf;
34 TermOutputBuffer *obuf = &term->obuf;
35
36 // Width of characters up to and including cursor position
37 size_t w = 1; // ":" (prefix)
38
39 for (size_t i = 0; i <= cmdline->pos && i < buf->len; ) {
40 CodePoint u = u_get_char(buf->buffer, buf->len, &i);
41 w += u_char_width(u);
42 }
43 if (cmdline->pos == buf->len) {
44 w++;
45 }
46 if (w > term->width) {
47 obuf->scroll_x = w - term->width;
48 }
49
50 set_builtin_color(term, colors, BC_COMMANDLINE);
51 term_put_char(obuf, prefix);
52
53 size_t x = obuf->x - obuf->scroll_x;
54 for (size_t i = 0; i < buf->len; ) {
55 BUG_ON(obuf->x > obuf->scroll_x + obuf->width);
56 CodePoint u = u_get_char(buf->buffer, buf->len, &i);
57 if (!term_put_char(obuf, u)) {
58 break;
59 }
60 if (i <= cmdline->pos) {
61 x = obuf->x - obuf->scroll_x;
62 }
63 }
64
65 return x;
66}
67
68void update_command_line(EditorState *e)
69{
70 Terminal *term = &e->terminal;
71 char prefix = ':';
72 term_output_reset(term, 0, term->width, 0);
73 term_move_cursor(&term->obuf, 0, term->height - 1);
74 switch (e->input_mode) {
75 case INPUT_NORMAL: {
76 bool msg_is_error;
77 const char *msg = get_msg(&msg_is_error);
78 print_message(term, &e->colors, msg, msg_is_error);
79 break;
80 }
81 case INPUT_SEARCH:
82 prefix = e->search.reverse ? '?' : '/';
83 // Fallthrough
84 case INPUT_COMMAND:
85 e->cmdline_x = print_command(term, &e->colors, &e->cmdline, prefix);
86 break;
87 default:
88 BUG("unhandled input mode");
89 }
90 term_clear_eol(term);
91}