Goal
To be able to add formatted text to a textarea object. Basically a hybrid of lv_label_set_text_fmt and lv_textarea_add_text.
More in-depth description
For a label you can use both lv_label_set_text and lv_label_set_text_fmt for added generic text and formatted text to a label. Currently for a textarea some of the options you have are lv_textarea_set_text, lv_textarea_add_char, and lv_textarea_add_text.
I would like to see the feature added for something like lv_textarea_add_text_fmt. There comes a few times where I want to add formatted text to a textarea and end up having to create a temp string and use either lv_snprintf or snprintf, then lv_textarea_add_text with that now formatted temp string.
I’ve given a go at trying to implement this myself, although I’m not sure if I’ve done it correctly (as in if there’s memory leaks, efficient, etc.), but it appears to work the way I intended it.
First Attempt at the code
I began by just examining lv_textarea_add_text and lv_label_set_text_fmt and trying to merge them.
In lv_textarea.h
/**
* Insert a formatted text to the current cursor position
* @param obj pointer to a text area object
* @param fmt `printf`-like format
* @example lv_textarea_add_text_fmt(ta, "%d user", user_num);
*/
void lv_textarea_add_text_fmt(lv_obj_t * obj, const char * fmt, ...);
In lv_textarea.c
void lv_textarea_add_text_fmt(lv_obj_t * obj, const char * fmt, ...)
{
LV_ASSERT_OBJ(obj, MY_CLASS);
LV_ASSERT_NULL(fmt);
if (fmt == NULL) return;
lv_textarea_t* ta = (lv_textarea_t*)obj;
const char* temp;
va_list args;
va_start(args, fmt);
temp = _lv_txt_set_text_vfmt(fmt, args);
va_end(args);
lv_label_ins_text(ta->label, ta->cursor.pos, temp);
lv_textarea_clear_selection(obj);
lv_textarea_set_cursor_pos(obj, lv_textarea_get_cursor_pos(obj) + _lv_txt_get_encoded_length(temp));
lv_event_send(obj, LV_EVENT_VALUE_CHANGED, NULL);
}
Any thoughts on the matter would be appreciated.