Random cleanup

This commit is contained in:
Přemysl Eric Janouch 2016-12-30 06:46:38 +01:00
parent bc631570c3
commit 958cb6e912
Signed by: p
GPG Key ID: B715679E3A361BE6
1 changed files with 95 additions and 123 deletions

188
hex.c
View File

@ -353,59 +353,61 @@ app_visible_rows (void)
} }
static void static void
app_draw_view (void) app_make_row (struct row_buffer *buf, int64_t addr, int attrs)
{ {
int64_t end_addr = g_ctx.data_offset + g_ctx.data_len; char *row_addr_str = xstrdup_printf ("%08" PRIx64, addr);
for (int y = 0; y < app_visible_rows (); y++) row_buffer_append (buf, row_addr_str, attrs);
{
int64_t row_addr = g_ctx.view_top + y * ROW_SIZE;
if (row_addr >= end_addr)
break;
int row_attrs = (row_addr / ROW_SIZE & 1)
? APP_ATTR (ODD) : APP_ATTR (EVEN);
struct row_buffer buf;
row_buffer_init (&buf);
char *row_addr_str = xstrdup_printf ("%08" PRIx64, row_addr);
row_buffer_append (&buf, row_addr_str, row_attrs);
free (row_addr_str); free (row_addr_str);
struct str ascii; struct str ascii;
str_init (&ascii); str_init (&ascii);
str_append (&ascii, " "); str_append (&ascii, " ");
int64_t end_addr = g_ctx.data_offset + g_ctx.data_len;
for (int x = 0; x < ROW_SIZE; x++) for (int x = 0; x < ROW_SIZE; x++)
{ {
if (x % 8 == 0) row_buffer_append (&buf, " ", row_attrs); if (x % 8 == 0) row_buffer_append (buf, " ", attrs);
if (x % 2 == 0) row_buffer_append (&buf, " ", row_attrs); if (x % 2 == 0) row_buffer_append (buf, " ", attrs);
int64_t cell_addr = row_addr + x; int64_t cell_addr = addr + x;
if (cell_addr < g_ctx.data_offset if (cell_addr < g_ctx.data_offset
|| cell_addr >= end_addr) || cell_addr >= end_addr)
{ {
row_buffer_append (&buf, " ", row_attrs); row_buffer_append (buf, " ", attrs);
str_append_c (&ascii, ' '); str_append_c (&ascii, ' ');
} }
else else
{ {
uint8_t cell = g_ctx.data[cell_addr - g_ctx.data_offset]; uint8_t cell = g_ctx.data[cell_addr - g_ctx.data_offset];
char *hex = xstrdup_printf ("%02x", cell); char *hex = xstrdup_printf ("%02x", cell);
row_buffer_append (&buf, hex, row_attrs); row_buffer_append (buf, hex, attrs);
free (hex); free (hex);
if (cell >= 32 && cell < 127) str_append_c (&ascii, (cell >= 32 && cell < 127) ? cell : '.');
str_append_c (&ascii, cell);
else
str_append_c (&ascii, '.');
} }
} }
row_buffer_append (&buf, ascii.str, row_attrs); row_buffer_append (buf, ascii.str, attrs);
str_free (&ascii); str_free (&ascii);
}
move (y, 0); static void
app_flush_buffer (&buf, COLS, row_attrs); app_draw_view (void)
{
move (0, 0);
int64_t end_addr = g_ctx.data_offset + g_ctx.data_len;
for (int y = 0; y < app_visible_rows (); y++)
{
int64_t addr = g_ctx.view_top + y * ROW_SIZE;
if (addr >= end_addr)
break;
int attrs = (addr / ROW_SIZE & 1) ? APP_ATTR (ODD) : APP_ATTR (EVEN);
struct row_buffer buf;
row_buffer_init (&buf);
app_make_row (&buf, addr, attrs);
app_flush_buffer (&buf, COLS, attrs);
} }
} }
@ -460,28 +462,22 @@ app_draw_footer (void)
{ {
move (app_visible_rows (), 0); move (app_visible_rows (), 0);
// XXX: can we get rid of this and still make it look acceptable?
chtype a_normal = APP_ATTR (BAR);
chtype a_active = APP_ATTR (BAR_HL);
struct row_buffer buf; struct row_buffer buf;
row_buffer_init (&buf); row_buffer_init (&buf);
row_buffer_append (&buf, APP_TITLE, APP_ATTR (BAR));
row_buffer_append (&buf, APP_TITLE, a_normal);
if (g_ctx.filename) if (g_ctx.filename)
{ {
row_buffer_append (&buf, " ", a_normal); row_buffer_append (&buf, " ", APP_ATTR (BAR));
char *filename = (char *) u8_strconv_from_locale (g_ctx.filename); char *filename = (char *) u8_strconv_from_locale (g_ctx.filename);
row_buffer_append (&buf, filename, a_active); row_buffer_append (&buf, filename, APP_ATTR (BAR_HL));
free (filename); free (filename);
} }
struct str right; struct str right;
str_init (&right); str_init (&right);
str_append_printf (&right, "%08" PRIx64 " ", g_ctx.view_cursor); str_append_printf (&right, " %08" PRIx64, g_ctx.view_cursor);
str_append_printf (&right, str_append (&right, g_ctx.endianity == ENDIANITY_LE ? " LE " : " BE ");
"%s ", g_ctx.endianity == ENDIANITY_LE ? "LE" : "BE");
int64_t top = g_ctx.view_top; int64_t top = g_ctx.view_top;
int64_t bot = g_ctx.view_top + app_visible_rows () * ROW_SIZE; int64_t bot = g_ctx.view_top + app_visible_rows () * ROW_SIZE;
@ -503,9 +499,9 @@ app_draw_footer (void)
str_append_printf (&right, "%2d%%", (int) (100 * cur / max)); str_append_printf (&right, "%2d%%", (int) (100 * cur / max));
} }
row_buffer_align (&buf, COLS - right.len, a_normal); row_buffer_align (&buf, COLS - right.len, APP_ATTR (BAR));
row_buffer_append (&buf, right.str, a_normal); row_buffer_append (&buf, right.str, APP_ATTR (BAR));
app_flush_buffer (&buf, COLS, a_normal); app_flush_buffer (&buf, COLS, APP_ATTR (BAR));
int64_t end_addr = g_ctx.data_offset + g_ctx.data_len; int64_t end_addr = g_ctx.data_offset + g_ctx.data_len;
if (g_ctx.view_cursor < g_ctx.data_offset if (g_ctx.view_cursor < g_ctx.data_offset
@ -585,16 +581,18 @@ app_on_refresh (void *user_data)
static bool static bool
app_fix_view_range (void) app_fix_view_range (void)
{ {
if (g_ctx.view_top < g_ctx.data_offset / ROW_SIZE * ROW_SIZE) int64_t data_view_start = g_ctx.data_offset / ROW_SIZE * ROW_SIZE;
if (g_ctx.view_top < data_view_start)
{ {
g_ctx.view_top = g_ctx.data_offset / ROW_SIZE * ROW_SIZE; g_ctx.view_top = data_view_start;
app_invalidate (); app_invalidate ();
return false; return false;
} }
// If the contents are at least as long as the screen, always fill it // If the contents are at least as long as the screen, always fill it
int64_t max_view_top = ((g_ctx.data_offset + g_ctx.data_len - 1) int64_t last_byte = g_ctx.data_offset + g_ctx.data_len - 1;
/ ROW_SIZE - app_visible_rows () + 1) * ROW_SIZE; int64_t max_view_top =
(last_byte / ROW_SIZE - app_visible_rows () + 1) * ROW_SIZE;
// But don't let that suggest a negative offset // But don't let that suggest a negative offset
max_view_top = MAX (max_view_top, 0); max_view_top = MAX (max_view_top, 0);
@ -623,14 +621,14 @@ app_ensure_selection_visible (void)
if (too_high > 0) if (too_high > 0)
app_scroll (-too_high); app_scroll (-too_high);
int too_low = g_ctx.view_cursor / ROW_SIZE int too_low = g_ctx.view_cursor / ROW_SIZE - g_ctx.view_top / ROW_SIZE
- (g_ctx.view_top / ROW_SIZE + app_visible_rows () - 1); - app_visible_rows () + 1;
if (too_low > 0) if (too_low > 0)
app_scroll (too_low); app_scroll (too_low);
} }
static bool static bool
app_move_selection (int diff) app_move_cursor_by_rows (int diff)
{ {
// TODO: disallow partial up/down movement // TODO: disallow partial up/down movement
int64_t fixed = g_ctx.view_cursor += diff * ROW_SIZE; int64_t fixed = g_ctx.view_cursor += diff * ROW_SIZE;
@ -649,23 +647,12 @@ app_move_selection (int diff)
enum action enum action
{ {
ACTION_NONE, ACTION_NONE, ACTION_QUIT, ACTION_REDRAW, ACTION_TOGGLE_ENDIANITY,
ACTION_QUIT,
ACTION_REDRAW,
ACTION_TOGGLE_ENDIANITY, ACTION_SCROLL_UP, ACTION_GOTO_TOP, ACTION_GOTO_PAGE_PREVIOUS,
ACTION_SCROLL_DOWN, ACTION_GOTO_BOTTOM, ACTION_GOTO_PAGE_NEXT,
ACTION_SCROLL_UP, ACTION_UP, ACTION_DOWN, ACTION_LEFT, ACTION_RIGHT,
ACTION_SCROLL_DOWN,
ACTION_GOTO_TOP,
ACTION_GOTO_BOTTOM,
ACTION_GOTO_PAGE_PREVIOUS,
ACTION_GOTO_PAGE_NEXT,
ACTION_UP,
ACTION_DOWN,
ACTION_LEFT,
ACTION_RIGHT,
ACTION_COUNT ACTION_COUNT
}; };
@ -677,31 +664,9 @@ app_process_action (enum action action)
{ {
switch (action) switch (action)
{ {
case ACTION_QUIT:
app_quit ();
break;
case ACTION_REDRAW:
clear ();
app_invalidate ();
break;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
case ACTION_TOGGLE_ENDIANITY:
g_ctx.endianity = (g_ctx.endianity == ENDIANITY_LE)
? ENDIANITY_BE : ENDIANITY_LE;
app_invalidate ();
break;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
// XXX: these should rather be parametrized // XXX: these should rather be parametrized
case ACTION_SCROLL_UP: case ACTION_SCROLL_UP: app_scroll (-1); break;
app_scroll (-1); case ACTION_SCROLL_DOWN: app_scroll (1); break;
break;
case ACTION_SCROLL_DOWN:
app_scroll (1);
break;
case ACTION_GOTO_TOP: case ACTION_GOTO_TOP:
g_ctx.view_cursor = g_ctx.data_offset; g_ctx.view_cursor = g_ctx.data_offset;
@ -711,7 +676,7 @@ app_process_action (enum action action)
break; break;
case ACTION_GOTO_BOTTOM: case ACTION_GOTO_BOTTOM:
if (!g_ctx.data_len) if (!g_ctx.data_len)
break; return false;
g_ctx.view_cursor = g_ctx.data_offset + g_ctx.data_len - 1; g_ctx.view_cursor = g_ctx.data_offset + g_ctx.data_len - 1;
g_ctx.view_skip_nibble = false; g_ctx.view_skip_nibble = false;
@ -721,19 +686,16 @@ app_process_action (enum action action)
case ACTION_GOTO_PAGE_PREVIOUS: case ACTION_GOTO_PAGE_PREVIOUS:
app_scroll (-app_visible_rows ()); app_scroll (-app_visible_rows ());
app_move_selection (-app_visible_rows ()); app_move_cursor_by_rows (-app_visible_rows ());
break; break;
case ACTION_GOTO_PAGE_NEXT: case ACTION_GOTO_PAGE_NEXT:
app_scroll (app_visible_rows ()); app_scroll (app_visible_rows ());
app_move_selection (app_visible_rows ()); app_move_cursor_by_rows (app_visible_rows ());
break; break;
case ACTION_UP: case ACTION_UP: app_move_cursor_by_rows (-1); break;
app_move_selection (-1); case ACTION_DOWN: app_move_cursor_by_rows (1); break;
break;
case ACTION_DOWN:
app_move_selection (1);
break;
case ACTION_LEFT: case ACTION_LEFT:
if (g_ctx.view_skip_nibble) if (g_ctx.view_skip_nibble)
g_ctx.view_skip_nibble = false; g_ctx.view_skip_nibble = false;
@ -744,8 +706,8 @@ app_process_action (enum action action)
g_ctx.view_skip_nibble = true; g_ctx.view_skip_nibble = true;
g_ctx.view_cursor--; g_ctx.view_cursor--;
}
app_ensure_selection_visible (); app_ensure_selection_visible ();
}
app_invalidate (); app_invalidate ();
break; break;
case ACTION_RIGHT: case ACTION_RIGHT:
@ -758,17 +720,26 @@ app_process_action (enum action action)
g_ctx.view_skip_nibble = false; g_ctx.view_skip_nibble = false;
g_ctx.view_cursor++; g_ctx.view_cursor++;
}
app_ensure_selection_visible (); app_ensure_selection_visible ();
}
app_invalidate (); app_invalidate ();
break; break;
// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - case ACTION_QUIT:
app_quit ();
case ACTION_NONE: case ACTION_NONE:
break; break;
case ACTION_REDRAW:
clear ();
app_invalidate ();
break;
case ACTION_TOGGLE_ENDIANITY:
g_ctx.endianity = (g_ctx.endianity == ENDIANITY_LE)
? ENDIANITY_BE : ENDIANITY_LE;
app_invalidate ();
break;
default: default:
beep ();
return false; return false;
} }
return true; return true;
@ -809,7 +780,7 @@ app_process_left_mouse_click (int line, int column)
return false; return false;
g_ctx.view_cursor = g_ctx.view_top + line * ROW_SIZE + offset; g_ctx.view_cursor = g_ctx.view_top + line * ROW_SIZE + offset;
return app_move_selection (0); return app_move_cursor_by_rows (0);
} }
return true; return true;
} }
@ -986,12 +957,13 @@ app_on_tty_readable (const struct pollfd *fd, void *user_data)
{ {
int y, x, button; int y, x, button;
termo_mouse_event_t type; termo_mouse_event_t type;
bool success;
if (termo_interpret_mouse (g_ctx.tk, &event, &type, &button, &y, &x)) if (termo_interpret_mouse (g_ctx.tk, &event, &type, &button, &y, &x))
{ success = app_process_mouse (type, y, x, button);
if (!app_process_mouse (type, y, x, button)) else
beep (); success = app_process_termo_event (&event);
}
else if (!app_process_termo_event (&event)) if (!success)
beep (); beep ();
} }