line-ed: update fx_array usage
This commit is contained in:
@@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
#include "line-ed.h"
|
#include "line-ed.h"
|
||||||
|
|
||||||
#include <fx/cstr.h>
|
#include <fx/wstr.h>
|
||||||
|
|
||||||
const fx_wchar *line_start(struct line_ed *ed, size_t y)
|
const fx_wchar *line_start(struct line_ed *ed, size_t y)
|
||||||
{
|
{
|
||||||
|
|||||||
+20
-15
@@ -6,31 +6,34 @@
|
|||||||
|
|
||||||
void alloc_empty_history_entry(struct line_ed *ed)
|
void alloc_empty_history_entry(struct line_ed *ed)
|
||||||
{
|
{
|
||||||
fx_string *str = (fx_string *)fx_array_at(
|
fx_value *str_v = fx_array_get_ref(
|
||||||
ed->l_history,
|
ed->l_history,
|
||||||
fx_array_size(ed->l_history) - 1);
|
fx_array_get_size(ed->l_history) - 1);
|
||||||
|
fx_string *str = NULL;
|
||||||
|
fx_value_get_object(str_v, &str);
|
||||||
if (!str || fx_string_get_size(str, FX_STRLEN_NORMAL) > 0) {
|
if (!str || fx_string_get_size(str, FX_STRLEN_NORMAL) > 0) {
|
||||||
str = fx_string_create();
|
str = fx_string_create();
|
||||||
fx_array_append(ed->l_history, (fx_object *)str);
|
fx_array_push_back(ed->l_history, FX_VALUE_OBJECT(str));
|
||||||
|
fx_string_unref(str);
|
||||||
}
|
}
|
||||||
|
|
||||||
ed->l_history_pos = fx_array_size(ed->l_history) - 1;
|
ed->l_history_pos = fx_array_get_size(ed->l_history) - 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
void save_buf_to_history(struct line_ed *ed)
|
void save_buf_to_history(struct line_ed *ed)
|
||||||
{
|
{
|
||||||
fx_string *cur = (fx_string *)fx_array_get(
|
fx_value *cur_v = fx_array_get_ref(ed->l_history, ed->l_history_pos);
|
||||||
ed->l_history,
|
fx_string *cur = NULL;
|
||||||
ed->l_history_pos);
|
fx_value_get_object(cur_v, &cur);
|
||||||
fx_string_clear(cur);
|
fx_string_clear(cur);
|
||||||
fx_string_append_wstr(cur, ed->l_buf);
|
fx_string_append_wstr(cur, ed->l_buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
void append_buf_to_history(struct line_ed *ed)
|
void append_buf_to_history(struct line_ed *ed)
|
||||||
{
|
{
|
||||||
fx_string *cur = (fx_string *)fx_array_get(
|
fx_value *cur_v = fx_array_get_ref(ed->l_history, ed->l_history_pos);
|
||||||
ed->l_history,
|
fx_string *cur = NULL;
|
||||||
ed->l_history_pos);
|
fx_value_get_object(cur_v, &cur);
|
||||||
char s[] = {'\n', 0};
|
char s[] = {'\n', 0};
|
||||||
fx_string_append_cstr(cur, s);
|
fx_string_append_cstr(cur, s);
|
||||||
fx_string_append_wstr(cur, ed->l_buf);
|
fx_string_append_wstr(cur, ed->l_buf);
|
||||||
@@ -38,9 +41,9 @@ void append_buf_to_history(struct line_ed *ed)
|
|||||||
|
|
||||||
void load_buf_from_history(struct line_ed *ed)
|
void load_buf_from_history(struct line_ed *ed)
|
||||||
{
|
{
|
||||||
fx_string *cur = (fx_string *)fx_array_at(
|
fx_value *cur_v = fx_array_get_ref(ed->l_history, ed->l_history_pos);
|
||||||
ed->l_history,
|
fx_string *cur = NULL;
|
||||||
ed->l_history_pos);
|
fx_value_get_object(cur_v, &cur);
|
||||||
size_t len = MIN(
|
size_t len = MIN(
|
||||||
(size_t)(ed->l_buf_end - ed->l_buf - 1),
|
(size_t)(ed->l_buf_end - ed->l_buf - 1),
|
||||||
fx_string_get_size(cur, FX_STRLEN_CODEPOINTS));
|
fx_string_get_size(cur, FX_STRLEN_CODEPOINTS));
|
||||||
@@ -72,11 +75,13 @@ void load_buf_from_history(struct line_ed *ed)
|
|||||||
|
|
||||||
const char *last_history_line(struct line_ed *ed)
|
const char *last_history_line(struct line_ed *ed)
|
||||||
{
|
{
|
||||||
size_t nlines = fx_array_size(ed->l_history);
|
size_t nlines = fx_array_get_size(ed->l_history);
|
||||||
if (nlines < 2) {
|
if (nlines < 2) {
|
||||||
return NULL;
|
return NULL;
|
||||||
}
|
}
|
||||||
|
|
||||||
fx_string *last = (fx_string *)fx_array_at(ed->l_history, nlines - 2);
|
fx_value *last_v = fx_array_get_ref(ed->l_history, nlines - 2);
|
||||||
|
fx_string *last = NULL;
|
||||||
|
fx_value_get_object(last_v, &last);
|
||||||
return fx_string_get_cstr(last);
|
return fx_string_get_cstr(last);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -205,7 +205,7 @@ void arrow_up(struct line_ed *ed)
|
|||||||
|
|
||||||
void arrow_down(struct line_ed *ed)
|
void arrow_down(struct line_ed *ed)
|
||||||
{
|
{
|
||||||
if (ed->l_history_pos == fx_array_size(ed->l_history) - 1) {
|
if (ed->l_history_pos == fx_array_get_size(ed->l_history) - 1) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -6,8 +6,8 @@
|
|||||||
#include "prompt.h"
|
#include "prompt.h"
|
||||||
|
|
||||||
#include <ctype.h>
|
#include <ctype.h>
|
||||||
#include <fx/cstr.h>
|
|
||||||
#include <fx/term/tty.h>
|
#include <fx/term/tty.h>
|
||||||
|
#include <fx/wstr.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
@@ -276,7 +276,7 @@ size_t line_ed_readline(struct line_ed *ed, fx_stringstream *out)
|
|||||||
ed->l_history_pos = append_to_index;
|
ed->l_history_pos = append_to_index;
|
||||||
append_buf_to_history(ed);
|
append_buf_to_history(ed);
|
||||||
} else {
|
} else {
|
||||||
ed->l_history_pos = fx_array_size(ed->l_history) - 1;
|
ed->l_history_pos = fx_array_get_size(ed->l_history) - 1;
|
||||||
|
|
||||||
const char *last = last_history_line(ed);
|
const char *last = last_history_line(ed);
|
||||||
if (!last || fx_wastrcmp(last, ed->l_buf) != 0) {
|
if (!last || fx_wastrcmp(last, ed->l_buf) != 0) {
|
||||||
|
|||||||
@@ -4,8 +4,8 @@
|
|||||||
#include "cursor.h"
|
#include "cursor.h"
|
||||||
#include "line-ed.h"
|
#include "line-ed.h"
|
||||||
|
|
||||||
#include <fx/cstr.h>
|
|
||||||
#include <fx/term/tty.h>
|
#include <fx/term/tty.h>
|
||||||
|
#include <fx/wstr.h>
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user