Skip to content

Commit

Permalink
Handle dialog drawing in update_screen() and use in dialog_prompt()
Browse files Browse the repository at this point in the history
Instead of reimplementing parts of `update_screen()` just to insert an
extra call to `show_dialog()`.
  • Loading branch information
craigbarnes committed Aug 30, 2024
1 parent b24507a commit 9761a68
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 41 deletions.
2 changes: 2 additions & 0 deletions src/editor.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ typedef enum {

UPDATE_ALL_WINDOWS = 1u << 4, // update_all_windows()
UPDATE_ALL = (UPDATE_ALL_WINDOWS << 1) - 1, // All of the above

UPDATE_DIALOG = 1u << 30, // show_dialog(); modal dialog for e.g. `quit -p`
} ScreenUpdateFlags;

typedef struct EditorState {
Expand Down
1 change: 1 addition & 0 deletions src/replace.c
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ static unsigned int replace_on_line (
view->cursor = *bi;

if (flags & REPLACE_CONFIRM) {
e->screen_update |= UPDATE_CURRENT_BUFFER;
switch (status_prompt(e, "Replace? [Y/n/a/q]", "ynaq")) {
case 'y':
break;
Expand Down
8 changes: 0 additions & 8 deletions src/ui-cmdline.c
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,6 @@ static void print_message(Terminal *term, const StyleMap *styles, const char *ms
}
}

void show_message(Terminal *term, const StyleMap *styles, const char *msg, bool is_error)
{
term_output_reset(term, 0, term->width, 0);
term_move_cursor(&term->obuf, 0, term->height - 1);
print_message(term, styles, msg, is_error);
term_clear_eol(term);
}

static size_t print_command(Terminal *term, const StyleMap *styles, const CommandLine *cmdline, char prefix)
{
const String *buf = &cmdline->buf;
Expand Down
47 changes: 15 additions & 32 deletions src/ui-prompt.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,8 @@ static char get_choice(Terminal *term, const char *choices, unsigned int esc_tim
return 0;
}

static void show_dialog (
Terminal *term,
const StyleMap *styles,
const TermStyle *text_style,
const char *question
) {
void show_dialog(Terminal *term, const StyleMap *styles, const char *question)
{
unsigned int question_width = u_str_width(question);
unsigned int min_width = question_width + 2;
if (term->height < 9 || term->width < min_width) {
Expand All @@ -66,11 +62,12 @@ static void show_dialog (
unsigned int bot = top + height;
unsigned int width = MAX(term->width / 2, min_width);
unsigned int x = (term->width - width) / 2;
TermOutputBuffer *obuf = &term->obuf;

// The "underline" and "strikethrough" attributes should only apply
// to the text, not the whole dialog background:
TermStyle dialog_style = *text_style;
TermOutputBuffer *obuf = &term->obuf;
TermStyle text_style = styles->builtin[BSE_DIALOG];
TermStyle dialog_style = text_style;
dialog_style.attr &= ~(ATTR_UNDERLINE | ATTR_STRIKETHROUGH);
set_style(term, styles, &dialog_style);

Expand All @@ -79,7 +76,7 @@ static void show_dialog (
term_move_cursor(obuf, x, y);
if (y == mid) {
term_set_bytes(term, ' ', (width - question_width) / 2);
set_style(term, styles, text_style);
set_style(term, styles, &text_style);
term_put_str(obuf, question);
set_style(term, styles, &dialog_style);
}
Expand All @@ -89,43 +86,29 @@ static void show_dialog (

char dialog_prompt(EditorState *e, const char *question, const char *choices)
{
const StyleMap *styles = &e->styles;
const TermStyle *style = &styles->builtin[BSE_DIALOG];
Terminal *term = &e->terminal;
TermOutputBuffer *obuf = &term->obuf;

start_update(term);
update_term_title(term, e->buffer, e->options.set_window_title);
update_all_windows(e);
show_dialog(term, styles, style, question);
show_message(term, styles, question, false);
term_end_sync_update(term);
term_output_flush(obuf);
const ScreenState dummyval = {.id = 0};
info_msg("%s", question);
e->screen_update |= UPDATE_ALL | UPDATE_DIALOG;
update_screen(e, &dummyval);

Terminal *term = &e->terminal;
unsigned int esc_timeout = e->options.esc_timeout;
char choice;
while ((choice = get_choice(term, choices, esc_timeout)) == 0) {
if (!resized) {
continue;
if (resized) {
e->screen_update |= UPDATE_DIALOG;
ui_resize(e);
}
resized = 0;
update_screen_size(&e->terminal, e->root_frame);
term_begin_sync_update(term);
update_all_windows(e);
show_dialog(term, styles, style, question);
show_message(term, styles, question, false);
term_end_sync_update(term);
term_output_flush(obuf);
}

clear_error();
e->screen_update |= UPDATE_ALL;
return (choice >= 'a') ? choice : 0;
}

char status_prompt(EditorState *e, const char *question, const char *choices)
{
const ScreenState dummyval = {.id = 0};
e->screen_update |= UPDATE_CURRENT_BUFFER | UPDATE_TERM_TITLE;
info_msg("%s", question);
update_screen(e, &dummyval);

Expand Down
6 changes: 6 additions & 0 deletions src/ui.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <string.h>
#include "ui.h"
#include "error.h"
#include "frame.h"
#include "terminal/cursor.h"
#include "terminal/ioctl.h"
Expand Down Expand Up @@ -180,6 +181,11 @@ void update_screen(EditorState *e, const ScreenState *s)

update_command_line(e);

if (unlikely(flags & UPDATE_DIALOG)) {
bool u;
show_dialog(term, &e->styles, get_msg(&u));
}

if (flags & UPDATE_CURSOR_STYLE) {
update_cursor_style(e);
}
Expand Down
2 changes: 1 addition & 1 deletion src/ui.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ void restore_cursor(EditorState *e);

// ui-cmdline.c
void update_command_line(EditorState *e);
void show_message(Terminal *term, const StyleMap *styles, const char *msg, bool is_error);

// ui-tabbar.c
void print_tabbar(Terminal *term, const StyleMap *styles, Window *window);
Expand All @@ -59,5 +58,6 @@ void update_window_separators(EditorState *e);
// ui-prompt.c
char status_prompt(EditorState *e, const char *question, const char *choices) NONNULL_ARGS;
char dialog_prompt(EditorState *e, const char *question, const char *choices) NONNULL_ARGS;
void show_dialog(Terminal *term, const StyleMap *styles, const char *question) NONNULL_ARGS;

#endif

0 comments on commit 9761a68

Please sign in to comment.