diff options
author | Damien George <damien.p.george@gmail.com> | 2015-04-26 17:55:31 +0100 |
---|---|---|
committer | Damien George <damien.p.george@gmail.com> | 2015-04-29 00:46:26 +0100 |
commit | a1a2c411b25bc5a709f719a8a955727f9d6e228f (patch) | |
tree | 9112861a8f12b886444e581a057de3369f0d37b7 /lib/mp-readline/readline.c | |
parent | b7a4f15b34a490040d87fbcffc728c9c3f64b85a (diff) | |
download | micropython-a1a2c411b25bc5a709f719a8a955727f9d6e228f.tar.gz micropython-a1a2c411b25bc5a709f719a8a955727f9d6e228f.zip |
py, readline: Add tab autocompletion for REPL.
Can complete names in the global namespace, as well as a chain of
attributes, eg pyb.Pin.board.<tab> will give a list of all board pins.
Costs 700 bytes ROM on Thumb2 arch, but greatly increases usability of
REPL prompt.
Diffstat (limited to 'lib/mp-readline/readline.c')
-rw-r--r-- | lib/mp-readline/readline.c | 23 |
1 files changed, 23 insertions, 0 deletions
diff --git a/lib/mp-readline/readline.c b/lib/mp-readline/readline.c index ce2a75905a..f119fb620a 100644 --- a/lib/mp-readline/readline.c +++ b/lib/mp-readline/readline.c @@ -29,6 +29,7 @@ #include <string.h> #include "py/mpstate.h" +#include "py/repl.h" #include "readline.h" #ifdef MICROPY_HAL_H #include MICROPY_HAL_H @@ -134,6 +135,28 @@ int readline_process_char(int c) { redraw_step_back = 1; redraw_from_cursor = true; } + #if MICROPY_HELPER_REPL + } else if (c == 9) { + // tab magic + const char *compl_str; + mp_uint_t compl_len = mp_repl_autocomplete(rl.line->buf + rl.orig_line_len, rl.cursor_pos - rl.orig_line_len, &mp_plat_print, &compl_str); + if (compl_len == 0) { + // no match + } else if (compl_len == (mp_uint_t)(-1)) { + // many matches + mp_hal_stdout_tx_str(rl.prompt); + mp_hal_stdout_tx_strn(rl.line->buf + rl.orig_line_len, rl.cursor_pos - rl.orig_line_len); + redraw_from_cursor = true; + } else { + // one match + for (int i = 0; i < compl_len; ++i) { + vstr_ins_byte(rl.line, rl.cursor_pos + i, *compl_str++); + } + // set redraw parameters + redraw_from_cursor = true; + redraw_step_forward = compl_len; + } + #endif } else if (32 <= c && c <= 126) { // printable character vstr_ins_char(rl.line, rl.cursor_pos, c); |