Skip to content

Commit 1129de5

Browse files
pfalcondpgeorge
authored andcommitted
unix: Support readline history saving to file, improves interactive usage.
1 parent 143c341 commit 1129de5

4 files changed

Lines changed: 19 additions & 1 deletion

File tree

unix/input.c

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
#if MICROPY_USE_READLINE
3636
#include <readline/readline.h>
3737
#include <readline/history.h>
38+
#include <readline/tilde.h>
3839
#endif
3940

4041
char *prompt(char *p) {
@@ -62,6 +63,18 @@ char *prompt(char *p) {
6263
return line;
6364
}
6465

66+
void prompt_read_history(void) {
67+
#if MICROPY_USE_READLINE_HISTORY
68+
read_history(tilde_expand("~/.micropython.history"));
69+
#endif
70+
}
71+
72+
void prompt_write_history(void) {
73+
#if MICROPY_USE_READLINE_HISTORY
74+
write_history(tilde_expand("~/.micropython.history"));
75+
#endif
76+
}
77+
6578
STATIC mp_obj_t mp_builtin_input(uint n_args, const mp_obj_t *args) {
6679
if (n_args == 1) {
6780
mp_obj_print(args[0], PRINT_STR);

unix/input.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
char *prompt(char *p);
2-
2+
void prompt_read_history(void);
3+
void prompt_write_history(void);

unix/main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -301,6 +301,8 @@ STATIC void set_sys_argv(char *argv[], int argc, int start_arg) {
301301
#endif
302302

303303
int main(int argc, char **argv) {
304+
prompt_read_history();
305+
304306
mp_stack_set_limit(32768);
305307

306308
pre_process_options(argc, argv);
@@ -477,6 +479,7 @@ int main(int argc, char **argv) {
477479
#endif
478480

479481
//printf("total bytes = %d\n", m_get_total_bytes_allocated());
482+
prompt_write_history();
480483
return ret & 0xff;
481484
}
482485

unix/mpconfigport.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@
4646
#define MICROPY_MALLOC_USES_ALLOCATED_SIZE (1)
4747
#define MICROPY_MEM_STATS (1)
4848
#define MICROPY_DEBUG_PRINTERS (1)
49+
#define MICROPY_USE_READLINE_HISTORY (1)
4950
#define MICROPY_HELPER_REPL (1)
5051
#define MICROPY_HELPER_LEXER_UNIX (1)
5152
#define MICROPY_ENABLE_SOURCE_LINE (1)

0 commit comments

Comments
 (0)