Progressively skip menu elements on small terminals rather

than crashing.
This commit is contained in:
Vladimir 'phcoder' Serbinenko 2013-05-14 08:57:18 +02:00
parent c8d6cc3cf0
commit cdce14fad4
10 changed files with 798 additions and 675 deletions

View file

@ -235,7 +235,8 @@ read_config_file (const char *config)
/* Initialize the screen. */
void
grub_normal_init_page (struct grub_term_output *term)
grub_normal_init_page (struct grub_term_output *term,
int y)
{
grub_ssize_t msg_len;
int posx;
@ -260,8 +261,10 @@ grub_normal_init_page (struct grub_term_output *term)
}
posx = grub_getstringwidth (unicode_msg, last_position, term);
posx = (grub_term_width (term) - posx) / 2;
grub_term_gotoxy (term, posx, 1);
posx = ((int) grub_term_width (term) - posx) / 2;
if (posx < 0)
posx = 0;
grub_term_gotoxy (term, posx, y);
grub_print_ucs4 (unicode_msg, last_position, 0, 0, term);
grub_putcode ('\n', term);
@ -405,10 +408,14 @@ grub_normal_reader_init (int nested)
FOR_ACTIVE_TERM_OUTPUTS(term)
{
grub_normal_init_page (term);
grub_normal_init_page (term, 1);
grub_term_setcursor (term, 1);
grub_print_message_indented (msg_formatted, 3, STANDARD_MARGIN, term);
if (grub_term_width (term) > 3 + STANDARD_MARGIN + 20)
grub_print_message_indented (msg_formatted, 3, STANDARD_MARGIN, term);
else
grub_print_message_indented (msg_formatted, 0, 0, term);
grub_putcode ('\n', term);
grub_putcode ('\n', term);
grub_putcode ('\n', term);
}

View file

@ -50,8 +50,7 @@ struct per_term_screen
{
struct grub_term_output *term;
int y_line_start;
/* Number of entries. */
int num_entries;
struct grub_term_screen_geometry geo;
};
struct screen
@ -118,22 +117,13 @@ ensure_space (struct line *linep, int extra)
return 1;
}
/* The max column number of an entry. The last "-1" is for a
continuation marker. */
static inline int
grub_term_entry_width (struct grub_term_output *term)
{
return grub_term_border_width (term) - GRUB_TERM_MARGIN * 2 - 2;
}
/* Return the number of lines occupied by this line on the screen. */
static int
get_logical_num_lines (struct line *linep, struct per_term_screen *term_screen)
{
return (grub_getstringwidth (linep->buf, linep->buf + linep->len,
term_screen->term)
/ grub_term_entry_width (term_screen->term)) + 1;
/ term_screen->geo.entry_width) + 1;
}
static void
@ -156,10 +146,28 @@ print_empty_line (int y, struct per_term_screen *term_screen)
int i;
grub_term_gotoxy (term_screen->term,
GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_MARGIN + 1,
y + GRUB_TERM_FIRST_ENTRY_Y);
term_screen->geo.first_entry_x,
y + term_screen->geo.first_entry_y);
for (i = 0; i < grub_term_entry_width (term_screen->term) + 1; i++)
for (i = 0; i < term_screen->geo.entry_width + 1; i++)
grub_putcode (' ', term_screen->term);
}
static void
print_updown (int upflag, int downflag, struct per_term_screen *term_screen)
{
grub_term_gotoxy (term_screen->term, term_screen->geo.first_entry_x
+ term_screen->geo.entry_width + 1
+ term_screen->geo.border,
term_screen->geo.first_entry_y);
if (upflag && downflag)
grub_putcode (GRUB_UNICODE_UPDOWNARROW, term_screen->term);
else if (upflag)
grub_putcode (GRUB_UNICODE_UPARROW, term_screen->term);
else if (downflag)
grub_putcode (GRUB_UNICODE_DOWNARROW, term_screen->term);
else
grub_putcode (' ', term_screen->term);
}
@ -167,9 +175,10 @@ print_empty_line (int y, struct per_term_screen *term_screen)
static void
print_up (int flag, struct per_term_screen *term_screen)
{
grub_term_gotoxy (term_screen->term, GRUB_TERM_LEFT_BORDER_X
+ grub_term_border_width (term_screen->term),
GRUB_TERM_FIRST_ENTRY_Y);
grub_term_gotoxy (term_screen->term, term_screen->geo.first_entry_x
+ term_screen->geo.entry_width + 1
+ term_screen->geo.border,
term_screen->geo.first_entry_y);
if (flag)
grub_putcode (GRUB_UNICODE_UPARROW, term_screen->term);
@ -181,10 +190,11 @@ print_up (int flag, struct per_term_screen *term_screen)
static void
print_down (int flag, struct per_term_screen *term_screen)
{
grub_term_gotoxy (term_screen->term, GRUB_TERM_LEFT_BORDER_X
+ grub_term_border_width (term_screen->term),
GRUB_TERM_TOP_BORDER_Y
+ term_screen->num_entries);
grub_term_gotoxy (term_screen->term, term_screen->geo.first_entry_x
+ term_screen->geo.entry_width + 1
+ term_screen->geo.border,
term_screen->geo.first_entry_y
+ term_screen->geo.num_entries - 1);
if (flag)
grub_putcode (GRUB_UNICODE_DOWNARROW, term_screen->term);
@ -210,17 +220,20 @@ update_screen (struct screen *screen, struct per_term_screen *term_screen,
for (i = 0; i < screen->line; i++, linep++)
y += get_logical_num_lines (linep, term_screen);
linep = screen->lines + screen->line;
y += grub_getstringwidth (linep->buf, linep->buf + screen->column,
term_screen->term) / grub_term_entry_width (term_screen->term);
int t = grub_getstringwidth (linep->buf, linep->buf + screen->column,
term_screen->term);
y += t / term_screen->geo.entry_width;
if (t % term_screen->geo.entry_width == 0
&& t != 0 && screen->column == linep->len)
y--;
/* Check if scrolling is necessary. */
if (y < 0 || y >= term_screen->num_entries)
if (y < 0 || y >= term_screen->geo.num_entries)
{
int delta;
if (y < 0)
delta = -y;
else
delta = term_screen->num_entries - 1 - y;
delta = term_screen->geo.num_entries - 1 - y;
term_screen->y_line_start += delta;
region_start = 0;
@ -265,24 +278,22 @@ update_screen (struct screen *screen, struct per_term_screen *term_screen,
if (i == region_start || linep == screen->lines + screen->line
|| (i > region_start && mode == ALL_LINES))
{
grub_term_gotoxy (term_screen->term, GRUB_TERM_LEFT_BORDER_X
+ GRUB_TERM_MARGIN + 1, (y < 0 ? 0 : y)
+ GRUB_TERM_FIRST_ENTRY_Y);
grub_term_gotoxy (term_screen->term, term_screen->geo.first_entry_x,
(y < 0 ? 0 : y)
+ term_screen->geo.first_entry_y);
grub_print_ucs4_menu (linep->buf,
linep->buf + linep->len,
GRUB_TERM_LEFT_BORDER_X
+ GRUB_TERM_MARGIN + 1,
GRUB_TERM_MARGIN
+ GRUB_TERM_SCROLL_WIDTH,
term_screen->geo.first_entry_x,
term_screen->geo.right_margin,
term_screen->term,
(y < 0) ? -y : 0,
term_screen->num_entries
term_screen->geo.num_entries
- ((y > 0) ? y : 0), '\\',
*pos);
}
y += get_logical_num_lines (linep, term_screen);
if (y >= term_screen->num_entries)
if (y >= term_screen->geo.num_entries)
{
if (i + 1 < screen->num_lines)
down_flag = 1;
@ -292,16 +303,25 @@ update_screen (struct screen *screen, struct per_term_screen *term_screen,
i++;
if (mode == ALL_LINES && i == screen->num_lines)
for (; y < term_screen->num_entries; y++)
for (; y < term_screen->geo.num_entries; y++)
print_empty_line (y, term_screen);
}
while (y < term_screen->num_entries);
while (y < term_screen->geo.num_entries);
/* Draw up and down arrows. */
if (up)
print_up (up_flag, term_screen);
if (down)
print_down (down_flag, term_screen);
if (term_screen->geo.num_entries == 1)
{
if (up || down)
print_updown (up_flag, down_flag, term_screen);
}
else
{
if (up)
print_up (up_flag, term_screen);
if (down)
print_down (down_flag, term_screen);
}
}
/* Place the cursor. */
@ -318,15 +338,13 @@ update_screen (struct screen *screen, struct per_term_screen *term_screen,
y += get_logical_num_lines (screen->lines + i, term_screen);
if (cpos >= &(screen->lines[screen->line].pos[term_screen - screen->terms])[0])
grub_term_gotoxy (term_screen->term,
cpos->x + GRUB_TERM_LEFT_BORDER_X
+ GRUB_TERM_MARGIN + 1,
cpos->x + term_screen->geo.first_entry_x,
cpos->y + y
+ GRUB_TERM_FIRST_ENTRY_Y);
+ term_screen->geo.first_entry_y);
else
grub_term_gotoxy (term_screen->term,
GRUB_TERM_LEFT_BORDER_X
+ GRUB_TERM_MARGIN + 1,
y + GRUB_TERM_FIRST_ENTRY_Y);
term_screen->geo.first_entry_x,
y + term_screen->geo.first_entry_y);
}
@ -1003,57 +1021,58 @@ complete (struct screen *screen, int continuous, int update)
if (restore)
for (i = 0; i < screen->nterms; i++)
{
int width = grub_term_width (screen->terms[i].term) - 2;
if (width > 15)
width -= 6;
int num_sections = ((completion_buffer.len
+ grub_term_width (screen->terms[i].term)
- 8 - 1)
/ (grub_term_width (screen->terms[i].term)
- 8));
+ width - 1)
/ width);
grub_uint32_t *endp;
grub_uint16_t pos;
grub_uint32_t *p = ucs4;
pos = grub_term_getxy (screen->terms[i].term);
grub_term_gotoxy (screen->terms[i].term, 0,
grub_term_height (screen->terms[i].term) - 3);
screen->completion_shown = 1;
grub_term_gotoxy (screen->terms[i].term, 0,
grub_term_height (screen->terms[i].term) - 3);
grub_puts_terminal (" ", screen->terms[i].term);
switch (completion_type)
screen->terms[i].geo.timeout_y);
if (screen->terms[i].geo.timeout_lines >= 2)
{
case GRUB_COMPLETION_TYPE_COMMAND:
grub_puts_terminal (_("Possible commands are:"),
screen->terms[i].term);
break;
case GRUB_COMPLETION_TYPE_DEVICE:
grub_puts_terminal (_("Possible devices are:"),
screen->terms[i].term);
break;
case GRUB_COMPLETION_TYPE_FILE:
grub_puts_terminal (_("Possible files are:"),
screen->terms[i].term);
break;
case GRUB_COMPLETION_TYPE_PARTITION:
grub_puts_terminal (_("Possible partitions are:"),
screen->terms[i].term);
break;
case GRUB_COMPLETION_TYPE_ARGUMENT:
grub_puts_terminal (_("Possible arguments are:"),
screen->terms[i].term);
break;
default:
grub_puts_terminal (_("Possible things are:"),
screen->terms[i].term);
break;
grub_puts_terminal (" ", screen->terms[i].term);
switch (completion_type)
{
case GRUB_COMPLETION_TYPE_COMMAND:
grub_puts_terminal (_("Possible commands are:"),
screen->terms[i].term);
break;
case GRUB_COMPLETION_TYPE_DEVICE:
grub_puts_terminal (_("Possible devices are:"),
screen->terms[i].term);
break;
case GRUB_COMPLETION_TYPE_FILE:
grub_puts_terminal (_("Possible files are:"),
screen->terms[i].term);
break;
case GRUB_COMPLETION_TYPE_PARTITION:
grub_puts_terminal (_("Possible partitions are:"),
screen->terms[i].term);
break;
case GRUB_COMPLETION_TYPE_ARGUMENT:
grub_puts_terminal (_("Possible arguments are:"),
screen->terms[i].term);
break;
default:
grub_puts_terminal (_("Possible things are:"),
screen->terms[i].term);
break;
}
grub_puts_terminal ("\n ", screen->terms[i].term);
}
grub_puts_terminal ("\n ", screen->terms[i].term);
p += (count % num_sections)
* (grub_term_width (screen->terms[i].term) - 8);
endp = p + (grub_term_width (screen->terms[i].term) - 8);
p += (count % num_sections) * width;
endp = p + width;
if (p != ucs4)
grub_putcode (GRUB_UNICODE_LEFTARROW, screen->terms[i].term);
@ -1087,17 +1106,19 @@ static void
clear_completions (struct per_term_screen *term_screen)
{
grub_uint16_t pos;
unsigned i, j;
unsigned j;
int i;
pos = grub_term_getxy (term_screen->term);
grub_term_gotoxy (term_screen->term, 0,
grub_term_height (term_screen->term) - 3);
term_screen->geo.timeout_y);
for (i = 0; i < 2; i++)
for (i = 0; i < term_screen->geo.timeout_lines; i++)
{
for (j = 0; j < grub_term_width (term_screen->term) - 1; j++)
grub_putcode (' ', term_screen->term);
grub_putcode ('\n', term_screen->term);
if (i + 1 < term_screen->geo.timeout_lines)
grub_putcode ('\n', term_screen->term);
}
grub_term_gotoxy (term_screen->term, pos >> 8, pos & 0xFF);
@ -1249,7 +1270,7 @@ grub_menu_entry_run (grub_menu_entry_t entry)
}
/* Draw the screen. */
for (i = 0; i < screen->nterms; i++)
grub_menu_init_page (0, 1, &screen->terms[i].num_entries,
grub_menu_init_page (0, 1, &screen->terms[i].geo,
screen->terms[i].term);
update_screen_all (screen, 0, 0, 1, 1, ALL_LINES);
for (i = 0; i < screen->nterms; i++)

View file

@ -34,17 +34,21 @@ static grub_uint8_t grub_color_menu_highlight;
struct menu_viewer_data
{
int first, offset;
/* The number of entries shown at a time. */
int num_entries;
struct grub_term_screen_geometry geo;
enum {
TIMEOUT_UNKNOWN,
TIMEOUT_NORMAL,
TIMEOUT_TERSE,
TIMEOUT_TERSE_NO_MARGIN
} timeout_msg;
grub_menu_t menu;
struct grub_term_output *term;
};
static inline int
grub_term_cursor_x (struct grub_term_output *term)
grub_term_cursor_x (const struct grub_term_screen_geometry *geo)
{
return (GRUB_TERM_LEFT_BORDER_X + grub_term_border_width (term)
- GRUB_TERM_MARGIN - 1);
return (geo->first_entry_x + geo->entry_width);
}
grub_ssize_t
@ -83,15 +87,14 @@ grub_print_message_indented_real (const char *msg, int margin_left,
(grub_uint8_t *) msg, -1, 0);
last_position = unicode_msg + msg_len;
*last_position++ = '\n';
*last_position = 0;
if (dry_run)
ret = grub_ucs4_count_lines (unicode_msg, last_position, margin_left,
margin_right, term);
else
grub_print_ucs4 (unicode_msg, last_position, margin_left,
margin_right, term);
grub_print_ucs4_menu (unicode_msg, last_position, margin_left,
margin_right, term, 0, -1, 0, 0);
grub_free (unicode_msg);
@ -106,39 +109,38 @@ grub_print_message_indented (const char *msg, int margin_left, int margin_right,
}
static void
draw_border (struct grub_term_output *term, int num_entries)
draw_border (struct grub_term_output *term, const struct grub_term_screen_geometry *geo)
{
unsigned i;
int i;
grub_term_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
grub_term_gotoxy (term, GRUB_TERM_MARGIN, GRUB_TERM_TOP_BORDER_Y);
grub_term_gotoxy (term, geo->first_entry_x - 1, geo->first_entry_y - 1);
grub_putcode (GRUB_UNICODE_CORNER_UL, term);
for (i = 0; i < (unsigned) grub_term_border_width (term) - 2; i++)
for (i = 0; i < geo->entry_width + 1; i++)
grub_putcode (GRUB_UNICODE_HLINE, term);
grub_putcode (GRUB_UNICODE_CORNER_UR, term);
for (i = 0; i < (unsigned) num_entries; i++)
for (i = 0; i < geo->num_entries; i++)
{
grub_term_gotoxy (term, GRUB_TERM_MARGIN, GRUB_TERM_TOP_BORDER_Y + i + 1);
grub_term_gotoxy (term, geo->first_entry_x - 1, geo->first_entry_y + i);
grub_putcode (GRUB_UNICODE_VLINE, term);
grub_term_gotoxy (term, GRUB_TERM_MARGIN + grub_term_border_width (term)
- 1,
GRUB_TERM_TOP_BORDER_Y + i + 1);
grub_term_gotoxy (term, geo->first_entry_x + geo->entry_width + 1,
geo->first_entry_y + i);
grub_putcode (GRUB_UNICODE_VLINE, term);
}
grub_term_gotoxy (term, GRUB_TERM_MARGIN,
GRUB_TERM_TOP_BORDER_Y + num_entries + 1);
grub_term_gotoxy (term, geo->first_entry_x - 1,
geo->first_entry_y - 1 + geo->num_entries + 1);
grub_putcode (GRUB_UNICODE_CORNER_LL, term);
for (i = 0; i < (unsigned) grub_term_border_width (term) - 2; i++)
for (i = 0; i < geo->entry_width + 1; i++)
grub_putcode (GRUB_UNICODE_HLINE, term);
grub_putcode (GRUB_UNICODE_CORNER_LR, term);
grub_term_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
grub_term_gotoxy (term, GRUB_TERM_MARGIN,
(GRUB_TERM_TOP_BORDER_Y + num_entries
grub_term_gotoxy (term, geo->first_entry_x - 1,
(geo->first_entry_y - 1 + geo->num_entries
+ GRUB_TERM_MARGIN + 1));
}
@ -150,10 +152,6 @@ print_message (int nested, int edit, struct grub_term_output *term, int dry_run)
if (edit)
{
if(dry_run)
ret++;
else
grub_putcode ('\n', term);
ret += grub_print_message_indented_real (_("Minimum Emacs-like screen editing is \
supported. TAB lists completions. Press Ctrl-x or F10 to boot, Ctrl-c or F2 for a \
command-line or ESC to discard edits and return to the GRUB menu."),
@ -170,10 +168,6 @@ command-line or ESC to discard edits and return to the GRUB menu."),
GRUB_UNICODE_DOWNARROW);
if (!msg_translated)
return 0;
if(dry_run)
ret++;
else
grub_putcode ('\n', term);
ret += grub_print_message_indented_real (msg_translated, STANDARD_MARGIN,
STANDARD_MARGIN, term, dry_run);
@ -201,9 +195,8 @@ command-line or ESC to discard edits and return to the GRUB menu."),
static void
print_entry (int y, int highlight, grub_menu_entry_t entry,
struct grub_term_output *term)
const struct menu_viewer_data *data)
{
int x;
const char *title;
grub_size_t title_len;
grub_ssize_t len;
@ -231,78 +224,34 @@ print_entry (int y, int highlight, grub_menu_entry_t entry,
old_color_highlight = grub_term_highlight_color;
grub_term_normal_color = grub_color_menu_normal;
grub_term_highlight_color = grub_color_menu_highlight;
grub_term_setcolorstate (term, highlight
grub_term_setcolorstate (data->term, highlight
? GRUB_TERM_COLOR_HIGHLIGHT
: GRUB_TERM_COLOR_NORMAL);
grub_term_gotoxy (term, GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_MARGIN, y);
int last_printed = 0;
grub_term_gotoxy (data->term, data->geo.first_entry_x, y);
for (i = 0; i < len; i++)
if (unicode_title[i] == '\n' || unicode_title[i] == '\b'
|| unicode_title[i] == '\r' || unicode_title[i] == '\e')
unicode_title[i] = ' ';
for (x = GRUB_TERM_LEFT_BORDER_X + GRUB_TERM_MARGIN + 2, i = 0;
x < (int) (GRUB_TERM_LEFT_BORDER_X + grub_term_border_width (term)
- GRUB_TERM_MARGIN);)
{
if (i < len
&& x <= (int) (GRUB_TERM_LEFT_BORDER_X + grub_term_border_width (term)
- GRUB_TERM_MARGIN - 1))
{
grub_ssize_t width;
struct grub_unicode_glyph glyph;
if (data->geo.num_entries > 1)
grub_putcode (highlight ? '*' : ' ', data->term);
i += grub_unicode_aglomerate_comb (unicode_title + i,
len - i, &glyph);
grub_print_ucs4_menu (unicode_title,
unicode_title + len,
0,
data->geo.right_margin,
data->term, 0, 1,
GRUB_UNICODE_RIGHTARROW, 0);
width = grub_term_getcharwidth (term, &glyph);
grub_unicode_destroy_glyph (&glyph);
if (x + width <= (int) (GRUB_TERM_LEFT_BORDER_X
+ grub_term_border_width (term)
- GRUB_TERM_MARGIN - 1))
last_printed = i;
x += width;
}
else
break;
}
grub_putcode (highlight ? '*' : ' ', term);
grub_print_ucs4 (unicode_title,
unicode_title + last_printed, 0, 0, term);
if (last_printed != len)
{
grub_putcode (GRUB_UNICODE_RIGHTARROW, term);
struct grub_unicode_glyph pseudo_glyph = {
.base = GRUB_UNICODE_RIGHTARROW,
.variant = 0,
.attributes = 0,
.ncomb = 0,
.estimated_width = 1
};
x += grub_term_getcharwidth (term, &pseudo_glyph);
}
for (; x < (int) (GRUB_TERM_LEFT_BORDER_X + grub_term_border_width (term)
- GRUB_TERM_MARGIN); x++)
grub_putcode (' ', term);
grub_term_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
grub_putcode (' ', term);
grub_term_gotoxy (term, grub_term_cursor_x (term), y);
grub_term_setcolorstate (data->term, GRUB_TERM_COLOR_NORMAL);
grub_term_gotoxy (data->term, grub_term_cursor_x (&data->geo), y);
grub_term_normal_color = old_color_normal;
grub_term_highlight_color = old_color_highlight;
grub_term_setcolorstate (term, GRUB_TERM_COLOR_NORMAL);
grub_term_setcolorstate (data->term, GRUB_TERM_COLOR_NORMAL);
grub_free (unicode_title);
}
@ -313,49 +262,120 @@ print_entries (grub_menu_t menu, const struct menu_viewer_data *data)
int i;
grub_term_gotoxy (data->term,
GRUB_TERM_LEFT_BORDER_X + grub_term_border_width (data->term),
GRUB_TERM_FIRST_ENTRY_Y);
if (data->first)
grub_putcode (GRUB_UNICODE_UPARROW, data->term);
else
grub_putcode (' ', data->term);
data->geo.first_entry_x + data->geo.entry_width
+ data->geo.border + 1,
data->geo.first_entry_y);
if (data->geo.num_entries != 1)
{
if (data->first)
grub_putcode (GRUB_UNICODE_UPARROW, data->term);
else
grub_putcode (' ', data->term);
}
e = grub_menu_get_entry (menu, data->first);
for (i = 0; i < data->num_entries; i++)
for (i = 0; i < data->geo.num_entries; i++)
{
print_entry (GRUB_TERM_FIRST_ENTRY_Y + i, data->offset == i,
e, data->term);
print_entry (data->geo.first_entry_y + i, data->offset == i,
e, data);
if (e)
e = e->next;
}
grub_term_gotoxy (data->term, GRUB_TERM_LEFT_BORDER_X
+ grub_term_border_width (data->term),
GRUB_TERM_TOP_BORDER_Y + data->num_entries);
if (e)
grub_putcode (GRUB_UNICODE_DOWNARROW, data->term);
grub_term_gotoxy (data->term, data->geo.first_entry_x + data->geo.entry_width
+ data->geo.border + 1,
data->geo.first_entry_y + data->geo.num_entries - 1);
if (data->geo.num_entries == 1)
{
if (data->first && e)
grub_putcode (GRUB_UNICODE_UPDOWNARROW, data->term);
else if (data->first)
grub_putcode (GRUB_UNICODE_UPARROW, data->term);
else if (e)
grub_putcode (GRUB_UNICODE_DOWNARROW, data->term);
else
grub_putcode (' ', data->term);
}
else
grub_putcode (' ', data->term);
{
if (e)
grub_putcode (GRUB_UNICODE_DOWNARROW, data->term);
else
grub_putcode (' ', data->term);
}
grub_term_gotoxy (data->term, grub_term_cursor_x (data->term),
GRUB_TERM_FIRST_ENTRY_Y + data->offset);
grub_term_gotoxy (data->term, grub_term_cursor_x (&data->geo),
data->geo.first_entry_y + data->offset);
}
/* Initialize the screen. If NESTED is non-zero, assume that this menu
is run from another menu or a command-line. If EDIT is non-zero, show
a message for the menu entry editor. */
void
grub_menu_init_page (int nested, int edit, int *num_entries,
grub_menu_init_page (int nested, int edit,
struct grub_term_screen_geometry *geo,
struct grub_term_output *term)
{
grub_uint8_t old_color_normal, old_color_highlight;
int msg_num_lines;
int bottom_message = 1;
int empty_lines = 1;
int version_msg = 1;
geo->border = 1;
geo->first_entry_x = 1 /* margin */ + 1 /* border */;
geo->entry_width = grub_term_width (term) - 5;
geo->first_entry_y = 2 /* two empty lines*/
+ 1 /* GNU GRUB version text */ + 1 /* top border */;
geo->timeout_lines = 2;
/* 3 lines for timeout message and bottom margin. 2 lines for the border. */
*num_entries = grub_term_height (term) - GRUB_TERM_TOP_BORDER_Y
- (print_message (nested, edit, term, 1) + 3) - 2;
geo->num_entries = grub_term_height (term) - geo->first_entry_y
- 1 /* bottom border */
- 1 /* empty line before info message*/
- geo->timeout_lines /* timeout */
- 1 /* empty final line */;
msg_num_lines = print_message (nested, edit, term, 1);
if (geo->num_entries - msg_num_lines < 3
|| geo->entry_width < 10)
{
geo->num_entries += 4;
geo->first_entry_y -= 2;
empty_lines = 0;
geo->first_entry_x -= 1;
geo->entry_width += 1;
}
if (geo->num_entries - msg_num_lines < 3
|| geo->entry_width < 10)
{
geo->num_entries += 2;
geo->first_entry_y -= 1;
geo->first_entry_x -= 1;
geo->entry_width += 2;
geo->border = 0;
}
if (geo->num_entries - msg_num_lines < 3
&& geo->timeout_lines == 2)
{
geo->timeout_lines = 1;
geo->num_entries++;
}
if (geo->num_entries - msg_num_lines < 3)
{
geo->num_entries += 1;
geo->first_entry_y -= 1;
version_msg = 0;
}
if (geo->num_entries - msg_num_lines >= 2)
geo->num_entries -= msg_num_lines;
else
bottom_message = 0;
/* By default, use the same colors for the menu. */
old_color_normal = grub_term_normal_color;
@ -369,13 +389,30 @@ grub_menu_init_page (int nested, int edit, int *num_entries,
grub_parse_color_name_pair (&grub_color_menu_highlight,
grub_env_get ("menu_color_highlight"));
grub_normal_init_page (term);
if (version_msg)
grub_normal_init_page (term, empty_lines);
else
grub_term_cls (term);
grub_term_normal_color = grub_color_menu_normal;
grub_term_highlight_color = grub_color_menu_highlight;
draw_border (term, *num_entries);
if (geo->border)
draw_border (term, geo);
grub_term_normal_color = old_color_normal;
grub_term_highlight_color = old_color_highlight;
print_message (nested, edit, term, 0);
geo->timeout_y = geo->first_entry_y + geo->num_entries
+ geo->border + empty_lines;
if (bottom_message)
{
grub_term_gotoxy (term, GRUB_TERM_MARGIN,
geo->timeout_y);
print_message (nested, edit, term, 0);
geo->timeout_y += msg_num_lines;
}
geo->right_margin = grub_term_width (term)
- geo->first_entry_x
- geo->entry_width - 1;
}
static void
@ -383,13 +420,17 @@ menu_text_print_timeout (int timeout, void *dataptr)
{
const char *msg =
_("The highlighted entry will be executed automatically in %ds.");
const char *msg_terse = _("%ds");
struct menu_viewer_data *data = dataptr;
char *msg_translated;
int posx;
char *msg_translated = 0;
grub_term_gotoxy (data->term, 0, grub_term_height (data->term) - 3);
grub_term_gotoxy (data->term, 0, data->geo.timeout_y);
msg_translated = grub_xasprintf (msg, timeout);
if (data->timeout_msg == TIMEOUT_TERSE
|| data->timeout_msg == TIMEOUT_TERSE_NO_MARGIN)
msg_translated = grub_xasprintf (msg_terse, timeout);
else
msg_translated = grub_xasprintf (msg, timeout);
if (!msg_translated)
{
grub_print_error ();
@ -397,15 +438,29 @@ menu_text_print_timeout (int timeout, void *dataptr)
return;
}
grub_print_message_indented (msg_translated, 3, 0, data->term);
if (data->timeout_msg == TIMEOUT_UNKNOWN)
{
data->timeout_msg = grub_print_message_indented_real (msg_translated,
3, 1, data->term, 1)
<= data->geo.timeout_lines ? TIMEOUT_NORMAL : TIMEOUT_TERSE;
if (data->timeout_msg == TIMEOUT_TERSE)
{
grub_free (msg_translated);
msg_translated = grub_xasprintf (msg_terse, timeout);
if (grub_term_width (data->term) < 10)
data->timeout_msg = TIMEOUT_TERSE_NO_MARGIN;
}
}
grub_print_message_indented (msg_translated,
data->timeout_msg == TIMEOUT_TERSE_NO_MARGIN ? 0 : 3,
data->timeout_msg == TIMEOUT_TERSE_NO_MARGIN ? 0 : 1,
data->term);
grub_free (msg_translated);
posx = grub_term_getxy (data->term) >> 8;
grub_print_spaces (data->term, grub_term_width (data->term) - posx - 1);
grub_term_gotoxy (data->term,
grub_term_cursor_x (data->term),
GRUB_TERM_FIRST_ENTRY_Y + data->offset);
grub_term_cursor_x (&data->geo),
data->geo.first_entry_y + data->offset);
grub_term_refresh (data->term);
}
@ -417,10 +472,10 @@ menu_text_set_chosen_entry (int entry, void *dataptr)
int complete_redraw = 0;
data->offset = entry - data->first;
if (data->offset > data->num_entries - 1)
if (data->offset > data->geo.num_entries - 1)
{
data->first = entry - (data->num_entries - 1);
data->offset = data->num_entries - 1;
data->first = entry - (data->geo.num_entries - 1);
data->offset = data->geo.num_entries - 1;
complete_redraw = 1;
}
if (data->offset < 0)
@ -433,12 +488,12 @@ menu_text_set_chosen_entry (int entry, void *dataptr)
print_entries (data->menu, data);
else
{
print_entry (GRUB_TERM_FIRST_ENTRY_Y + oldoffset, 0,
print_entry (data->geo.first_entry_y + oldoffset, 0,
grub_menu_get_entry (data->menu, data->first + oldoffset),
data->term);
print_entry (GRUB_TERM_FIRST_ENTRY_Y + data->offset, 1,
data);
print_entry (data->geo.first_entry_y + data->offset, 1,
grub_menu_get_entry (data->menu, data->first + data->offset),
data->term);
data);
}
grub_term_refresh (data->term);
}
@ -457,13 +512,26 @@ static void
menu_text_clear_timeout (void *dataptr)
{
struct menu_viewer_data *data = dataptr;
int i;
grub_term_gotoxy (data->term, 0, grub_term_height (data->term) - 3);
grub_print_spaces (data->term, grub_term_width (data->term) - 1);
grub_term_gotoxy (data->term, 0, grub_term_height (data->term) - 2);
grub_print_spaces (data->term, grub_term_width (data->term) - 1);
grub_term_gotoxy (data->term, grub_term_cursor_x (data->term),
GRUB_TERM_FIRST_ENTRY_Y + data->offset);
for (i = 0; i < data->geo.timeout_lines;i++)
{
grub_term_gotoxy (data->term, 0, data->geo.timeout_y + i);
grub_print_spaces (data->term, grub_term_width (data->term) - 1);
}
if (data->geo.num_entries <= 5 && !data->geo.border)
{
grub_term_gotoxy (data->term, data->geo.first_entry_x + data->geo.entry_width
+ data->geo.border + 1,
data->geo.first_entry_y + data->geo.num_entries - 1);
grub_putcode (' ', data->term);
data->geo.timeout_lines = 0;
data->geo.num_entries++;
print_entries (data->menu, data);
}
grub_term_gotoxy (data->term, grub_term_cursor_x (&data->geo),
data->geo.first_entry_y + data->offset);
grub_term_refresh (data->term);
}
@ -498,12 +566,12 @@ grub_menu_try_text (struct grub_term_output *term,
data->first = 0;
grub_term_setcursor (data->term, 0);
grub_menu_init_page (nested, 0, &data->num_entries, data->term);
grub_menu_init_page (nested, 0, &data->geo, data->term);
if (data->offset > data->num_entries - 1)
if (data->offset > data->geo.num_entries - 1)
{
data->first = data->offset - (data->num_entries - 1);
data->offset = data->num_entries - 1;
data->first = data->offset - (data->geo.num_entries - 1);
data->offset = data->geo.num_entries - 1;
}
print_entries (menu, data);

View file

@ -50,7 +50,8 @@ print_ucs4_real (const grub_uint32_t * str,
struct grub_term_output *term, int backlog,
int dry_run, int fixed_tab, unsigned skip_lines,
unsigned max_lines,
grub_uint32_t contchar, struct grub_term_pos *pos);
grub_uint32_t contchar, int fill_right,
struct grub_term_pos *pos);
static struct term_state *term_states = NULL;
@ -132,6 +133,7 @@ grub_set_more (int onoff)
enum
{
GRUB_CP437_UPDOWNARROW = 0x12,
GRUB_CP437_UPARROW = 0x18,
GRUB_CP437_DOWNARROW = 0x19,
GRUB_CP437_RIGHTARROW = 0x1a,
@ -159,6 +161,8 @@ map_code (grub_uint32_t in, struct grub_term_output *term)
return GRUB_CP437_LEFTARROW;
case GRUB_UNICODE_UPARROW:
return GRUB_CP437_UPARROW;
case GRUB_UNICODE_UPDOWNARROW:
return GRUB_CP437_UPDOWNARROW;
case GRUB_UNICODE_RIGHTARROW:
return GRUB_CP437_RIGHTARROW;
case GRUB_UNICODE_DOWNARROW:
@ -249,7 +253,7 @@ grub_puts_terminal (const char *str, struct grub_term_output *term)
}
print_ucs4_real (unicode_str, unicode_last_position, 0, 0, term,
0, 0, 0, 0, -1, 0, 0);
0, 0, 0, 0, -1, 0, 0, 0);
grub_free (unicode_str);
}
@ -562,7 +566,7 @@ print_ucs4_terminal (const grub_uint32_t * str,
int dry_run, int fixed_tab, unsigned skip_lines,
unsigned max_lines,
grub_uint32_t contchar,
int primitive_wrap, struct grub_term_pos *pos)
int primitive_wrap, int fill_right, struct grub_term_pos *pos)
{
const grub_uint32_t *ptr;
grub_ssize_t startwidth = dry_run ? 0 : get_startwidth (term, margin_left);
@ -587,6 +591,7 @@ print_ucs4_terminal (const grub_uint32_t * str,
for (ptr = str; ptr < last_position; ptr++)
{
grub_ssize_t last_width = 0;
switch (*ptr)
{
case GRUB_UNICODE_LRE:
@ -667,10 +672,11 @@ print_ucs4_terminal (const grub_uint32_t * str,
if (!wasn && contchar)
putcode_real (contchar, term, fixed_tab);
if (contchar)
if (fill_right)
fill_margin (term, margin_right);
grub_putcode ('\n', term);
if (!contchar || max_lines != 1)
grub_putcode ('\n', term);
if (state != &local_state && ++state->num_lines
>= (grub_ssize_t) grub_term_height (term) - 2)
{
@ -724,11 +730,10 @@ print_ucs4_terminal (const grub_uint32_t * str,
}
if (line_start < last_position)
lines++;
lines++;
if (!dry_run && !skip_lines && max_lines)
{
const grub_uint32_t *ptr2;
int sp;
for (ptr2 = line_start; ptr2 < last_position; ptr2++)
{
@ -741,12 +746,8 @@ print_ucs4_terminal (const grub_uint32_t * str,
putcode_real (*ptr2, term, fixed_tab);
}
if (contchar)
{
sp = max_width - pos[last_position - str].x + 1;
if (sp > 0)
grub_print_spaces (term, sp);
}
if (fill_right)
fill_margin (term, margin_right);
}
return dry_run ? lines : 0;
}
@ -779,13 +780,14 @@ put_glyphs_terminal (struct grub_unicode_glyph *visual,
int margin_left, int margin_right,
struct grub_term_output *term,
struct term_state *state, int fixed_tab,
grub_uint32_t contchar)
grub_uint32_t contchar,
int fill_right)
{
struct grub_unicode_glyph *visual_ptr;
int since_last_nl = 1;
for (visual_ptr = visual; visual_ptr < visual + visual_len; visual_ptr++)
{
if (visual_ptr->base == '\n' && contchar)
if (visual_ptr->base == '\n' && fill_right)
fill_margin (term, margin_right);
putglyph (visual_ptr, term, fixed_tab);
@ -810,7 +812,7 @@ put_glyphs_terminal (struct grub_unicode_glyph *visual,
}
grub_unicode_destroy_glyph (visual_ptr);
}
if (contchar && since_last_nl)
if (fill_right && since_last_nl)
fill_margin (term, margin_right);
return 0;
@ -832,7 +834,7 @@ print_backlog (struct grub_term_output *term,
state->backlog_ucs4 + state->backlog_len,
margin_left, margin_right, term, state, 0,
state->backlog_fixed_tab, 0, -1, 0, 0,
0);
0, 0);
if (!ret)
{
grub_free (state->free);
@ -849,7 +851,7 @@ print_backlog (struct grub_term_output *term,
ret = put_glyphs_terminal (state->backlog_glyphs,
state->backlog_len,
margin_left, margin_right, term, state,
state->backlog_fixed_tab, 0);
state->backlog_fixed_tab, 0, 0);
if (!ret)
{
grub_free (state->free);
@ -876,7 +878,8 @@ print_ucs4_real (const grub_uint32_t * str,
struct grub_term_output *term, int backlog,
int dry_run, int fixed_tab, unsigned skip_lines,
unsigned max_lines,
grub_uint32_t contchar, struct grub_term_pos *pos)
grub_uint32_t contchar, int fill_right,
struct grub_term_pos *pos)
{
struct term_state *state = NULL;
@ -955,7 +958,7 @@ print_ucs4_real (const grub_uint32_t * str,
{
ret = put_glyphs_terminal (visual_show, visual_len_show, margin_left,
margin_right,
term, state, fixed_tab, contchar);
term, state, fixed_tab, contchar, fill_right);
if (!ret)
grub_free (visual);
@ -966,7 +969,7 @@ print_ucs4_real (const grub_uint32_t * str,
}
return print_ucs4_terminal (str, last_position, margin_left, margin_right,
term, state, dry_run, fixed_tab, skip_lines,
max_lines, contchar, !!contchar, pos);
max_lines, contchar, !!contchar, fill_right, pos);
}
void
@ -980,7 +983,7 @@ grub_print_ucs4_menu (const grub_uint32_t * str,
{
print_ucs4_real (str, last_position, margin_left, margin_right,
term, 0, 0, 1, skip_lines, max_lines,
contchar, pos);
contchar, 1, pos);
}
void
@ -990,7 +993,7 @@ grub_print_ucs4 (const grub_uint32_t * str,
struct grub_term_output *term)
{
print_ucs4_real (str, last_position, margin_left, margin_right,
term, 0, 0, 1, 0, -1, 0, 0);
term, 0, 0, 1, 0, -1, 0, 0, 0);
}
int
@ -1000,7 +1003,7 @@ grub_ucs4_count_lines (const grub_uint32_t * str,
struct grub_term_output *term)
{
return print_ucs4_real (str, last_position, margin_left, margin_right,
term, 0, 1, 1, 0, -1, 0, 0);
term, 0, 1, 1, 0, -1, 0, 0, 0);
}
void
@ -1054,7 +1057,7 @@ grub_xnputs (const char *str, grub_size_t msg_len)
{
int cur;
cur = print_ucs4_real (unicode_str, unicode_last_position, 0, 0,
term, grub_more, 0, 0, 0, -1, 0, 0);
term, grub_more, 0, 0, 0, -1, 0, 0, 0);
if (cur)
backlog = 1;
}