Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
TASTE
uPython-mirror
Commits
2acfb7c0
Commit
2acfb7c0
authored
May 17, 2015
by
Damien George
Browse files
lib/mp-readline: Export readline_push_history function.
parent
c754d801
Changes
2
Hide whitespace changes
Inline
Side-by-side
lib/mp-readline/readline.c
View file @
2acfb7c0
...
...
@@ -81,10 +81,10 @@ STATIC void erase_line_from_cursor(void) {
typedef
struct
_readline_t
{
vstr_t
*
line
;
in
t
orig_line_len
;
size_
t
orig_line_len
;
int
escape_seq
;
int
hist_cur
;
in
t
cursor_pos
;
size_
t
cursor_pos
;
char
escape_seq_buf
[
1
];
const
char
*
prompt
;
}
readline_t
;
...
...
@@ -92,7 +92,7 @@ typedef struct _readline_t {
STATIC
readline_t
rl
;
int
readline_process_char
(
int
c
)
{
in
t
last_line_len
=
rl
.
line
->
len
;
size_
t
last_line_len
=
rl
.
line
->
len
;
int
redraw_step_back
=
0
;
bool
redraw_from_cursor
=
false
;
int
redraw_step_forward
=
0
;
...
...
@@ -112,17 +112,7 @@ int readline_process_char(int c) {
}
else
if
(
c
==
'\r'
)
{
// newline
mp_hal_stdout_tx_str
(
"
\r\n
"
);
if
(
rl
.
line
->
len
>
rl
.
orig_line_len
&&
(
MP_STATE_PORT
(
readline_hist
)[
0
]
==
NULL
||
strcmp
(
MP_STATE_PORT
(
readline_hist
)[
0
],
rl
.
line
->
buf
+
rl
.
orig_line_len
)
!=
0
))
{
// a line which is not empty and different from the last one
// so update the history
char
*
most_recent_hist
=
str_dup_maybe
(
vstr_null_terminated_str
(
rl
.
line
)
+
rl
.
orig_line_len
);
if
(
most_recent_hist
!=
NULL
)
{
for
(
int
i
=
READLINE_HIST_SIZE
-
1
;
i
>
0
;
i
--
)
{
MP_STATE_PORT
(
readline_hist
)[
i
]
=
MP_STATE_PORT
(
readline_hist
)[
i
-
1
];
}
MP_STATE_PORT
(
readline_hist
)[
0
]
=
most_recent_hist
;
}
}
readline_push_history
(
vstr_null_terminated_str
(
rl
.
line
)
+
rl
.
orig_line_len
);
return
0
;
}
else
if
(
c
==
27
)
{
// escape sequence
...
...
@@ -149,7 +139,7 @@ int readline_process_char(int c) {
redraw_from_cursor
=
true
;
}
else
{
// one match
for
(
in
t
i
=
0
;
i
<
compl_len
;
++
i
)
{
for
(
mp_uint_
t
i
=
0
;
i
<
compl_len
;
++
i
)
{
vstr_ins_byte
(
rl
.
line
,
rl
.
cursor_pos
+
i
,
*
compl_str
++
);
}
// set redraw parameters
...
...
@@ -184,7 +174,7 @@ int readline_process_char(int c) {
rl
.
escape_seq
=
ESEQ_NONE
;
if
(
c
==
'A'
)
{
// up arrow
if
(
rl
.
hist_cur
+
1
<
READLINE_HIST_SIZE
&&
MP_STATE_PORT
(
readline_hist
)[
rl
.
hist_cur
+
1
]
!=
NULL
)
{
if
(
rl
.
hist_cur
+
1
<
(
int
)
READLINE_HIST_SIZE
&&
MP_STATE_PORT
(
readline_hist
)[
rl
.
hist_cur
+
1
]
!=
NULL
)
{
// increase hist num
rl
.
hist_cur
+=
1
;
// set line to history
...
...
@@ -312,3 +302,19 @@ int readline(vstr_t *line, const char *prompt) {
}
}
}
void
readline_push_history
(
const
char
*
line
)
{
if
(
line
[
0
]
!=
'\0'
&&
(
MP_STATE_PORT
(
readline_hist
)[
0
]
==
NULL
||
strcmp
(
MP_STATE_PORT
(
readline_hist
)[
0
],
line
)
!=
0
))
{
// a line which is not empty and different from the last one
// so update the history
char
*
most_recent_hist
=
str_dup_maybe
(
line
);
if
(
most_recent_hist
!=
NULL
)
{
for
(
int
i
=
READLINE_HIST_SIZE
-
1
;
i
>
0
;
i
--
)
{
MP_STATE_PORT
(
readline_hist
)[
i
]
=
MP_STATE_PORT
(
readline_hist
)[
i
-
1
];
}
MP_STATE_PORT
(
readline_hist
)[
0
]
=
most_recent_hist
;
}
}
}
lib/mp-readline/readline.h
View file @
2acfb7c0
...
...
@@ -32,6 +32,7 @@
void
readline_init0
(
void
);
int
readline
(
vstr_t
*
line
,
const
char
*
prompt
);
void
readline_push_history
(
const
char
*
line
);
void
readline_init
(
vstr_t
*
line
,
const
char
*
prompt
);
void
readline_note_newline
(
const
char
*
prompt
);
...
...
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment