Implement Tv command and use it from VT ##visual

* Visual textlogs are now a thing!
This commit is contained in:
pancake 2022-10-06 12:06:03 +02:00
parent 9f6afce36d
commit ee8b803c4e
3 changed files with 82 additions and 1 deletions

View File

@ -216,6 +216,40 @@ static int log_callback_all(RCore *log, int count, const char *line) {
return 0;
}
R_API void r_core_log_view(RCore *core, int num) {
if (num < 1) {
num = 1;
}
int i;
for (i = num - 3; i < num + 3; i++) {
r_cons_printf ("%s", (num == i)? "* ": " ");
if (i < 1) {
r_cons_printf (" ^\n");
continue;
}
if (i >= core->log->last) {
r_cons_printf (" $\n");
continue;
}
if (i < core->log->first) {
r_cons_printf (" ^\n");
continue;
}
const char *msg = r_strpool_get_i (core->log->sp, i);
if (msg) {
char *m = r_str_ndup (msg, 60);
char *nl = strchr (m, '\n');
if (nl) {
*nl = 0;
}
r_cons_printf ("%d %s\n", i, m);
free (m);
} else {
r_cons_printf ("%d ..\n", i);
}
}
}
static int cmd_log(void *data, const char *input) {
RCore *core = (RCore *) data;
const char *arg, *input2;
@ -247,6 +281,9 @@ static int cmd_log(void *data, const char *input) {
}
}
break;
case 'v': // "Tv"
r_core_log_view (core, (int)r_num_math (core->num, input + 2));
break;
case 'l': // "Tl"
r_cons_printf ("%d\n", core->log->last - 1);
break;

View File

@ -1738,6 +1738,49 @@ char *getcommapath(RCore *core) {
return cwd;
}
static void visual_textlogs(RCore *core) {
int index = 1;
while (true) {
r_cons_clear00 ();
const char *title = "[visual-text-logs] Use [hjkl] to move, `i` to insert, `q` to quit\n";
if (r_config_get_i (core->config, "scr.color") > 0) {
r_cons_printf (Color_YELLOW"%s"Color_RESET, title);
} else {
r_cons_printf ("%s", title);
}
r_core_cmdf (core, "Tv %d", index);
r_cons_printf ("--\n");
char *s = r_core_cmd_strf (core, "Tm %d~{}", index);
r_cons_printf ("%s\n", s);
free (s);
r_cons_visual_flush ();
char ch = (ut8)r_cons_readchar ();
switch (ch) {
case 'q':
return;
case 'i':
r_core_cmdf (core, "T `?ie message`");
break;
case 'j':
index++;
break;
case '-':
r_core_cmdf (core, "T-%d", index);
break;
case 'k':
if (index > 1) {
index--;
} else {
index = 1;
}
break;
case ':':
r_core_visual_prompt_input (core);
break;
}
}
}
static void visual_comma(RCore *core) {
bool mouse_state = __holdMouseState (core);
ut64 addr = core->offset + (core->print->cur_enabled? core->print->cur: 0);
@ -2911,7 +2954,7 @@ R_API int r_core_visual_cmd(RCore *core, const char *arg) {
}
break;
case 'T':
visual_closetab (core);
visual_textlogs (core);
break;
case 'n':
r_core_seek_next (core, r_config_get (core->config, "scr.nkey"));

View File

@ -857,6 +857,7 @@ R_API void r_core_undo_down(RCore *core);
/* logs */
typedef int (*RCoreLogCallback)(RCore *core, int count, const char *message);
R_API void r_core_log_free(RCoreLog *log);
R_API void r_core_log_view(RCore *core, int num);
R_API void r_core_log_init(RCoreLog *log);
R_API char *r_core_log_get(RCore *core, int index);
R_API RCoreLog *r_core_log_new(void);