mirror of
https://github.com/lvgl/lvgl.git
synced 2024-11-26 19:23:46 +08:00
feat(observer): add subject snprintf (#7250)
Co-authored-by: Fabian Zäh <Fabian.Zaeh@livetec.de> Co-authored-by: Liam <30486941+liamHowatt@users.noreply.github.com>
This commit is contained in:
parent
f677c25abc
commit
2d06699aa0
@ -132,7 +132,7 @@ void lv_subject_init_string(lv_subject_t * subject, char * buf, char * prev_buf,
|
||||
void lv_subject_copy_string(lv_subject_t * subject, const char * buf)
|
||||
{
|
||||
if(subject->type != LV_SUBJECT_TYPE_STRING) {
|
||||
LV_LOG_WARN("Subject type is not LV_SUBJECT_TYPE_INT");
|
||||
LV_LOG_WARN("Subject type is not LV_SUBJECT_TYPE_STRING");
|
||||
return;
|
||||
}
|
||||
|
||||
@ -147,6 +147,28 @@ void lv_subject_copy_string(lv_subject_t * subject, const char * buf)
|
||||
|
||||
}
|
||||
|
||||
void lv_subject_snprintf(lv_subject_t * subject, const char * format, ...)
|
||||
{
|
||||
if(subject->type != LV_SUBJECT_TYPE_STRING) {
|
||||
LV_LOG_WARN("Subject type is not LV_SUBJECT_TYPE_STRING");
|
||||
return;
|
||||
}
|
||||
|
||||
if(subject->size < 1) return;
|
||||
|
||||
if(subject->prev_value.pointer) {
|
||||
lv_strlcpy((char *)subject->prev_value.pointer, subject->value.pointer, subject->size);
|
||||
}
|
||||
|
||||
va_list va;
|
||||
va_start(va, format);
|
||||
const int ret = lv_vsnprintf((char *)subject->value.pointer, subject->size, format, va);
|
||||
LV_UNUSED(ret);
|
||||
va_end(va);
|
||||
|
||||
lv_subject_notify(subject);
|
||||
}
|
||||
|
||||
const char * lv_subject_get_string(lv_subject_t * subject)
|
||||
{
|
||||
if(subject->type != LV_SUBJECT_TYPE_STRING) {
|
||||
|
@ -117,6 +117,13 @@ void lv_subject_init_string(lv_subject_t * subject, char * buf, char * prev_buf,
|
||||
*/
|
||||
void lv_subject_copy_string(lv_subject_t * subject, const char * buf);
|
||||
|
||||
/**
|
||||
* Print a formated string to a subject. It will notify all the observers as well.
|
||||
* @param subject pointer to the subject
|
||||
* @param format the format string
|
||||
*/
|
||||
void lv_subject_snprintf(lv_subject_t * subject, const char * format, ...) LV_FORMAT_ATTRIBUTE(2, 3);
|
||||
|
||||
/**
|
||||
* Get the current value of an string subject
|
||||
* @param subject pointer to the subject
|
||||
|
@ -94,10 +94,20 @@ void test_observer_string(void)
|
||||
TEST_ASSERT_EQUAL_STRING("how are you?", lv_subject_get_string(&subject));
|
||||
TEST_ASSERT_EQUAL_STRING("my name is John", lv_subject_get_previous_string(&subject));
|
||||
|
||||
lv_subject_snprintf(&subject, "I ate %d pizzas", 10);
|
||||
TEST_ASSERT_EQUAL_STRING("I ate 10 pizzas", lv_subject_get_string(&subject));
|
||||
TEST_ASSERT_EQUAL_STRING("how are you?", lv_subject_get_previous_string(&subject));
|
||||
|
||||
lv_subject_snprintf(&subject, "%d: %s", 1, "Coding is fun !");
|
||||
TEST_ASSERT_EQUAL_STRING("1: Coding is fun !", lv_subject_get_string(&subject));
|
||||
TEST_ASSERT_EQUAL_STRING("I ate 10 pizzas", lv_subject_get_previous_string(&subject));
|
||||
|
||||
|
||||
|
||||
/*Clip long text*/
|
||||
lv_subject_copy_string(&subject, "text to be clipped to 32 chars.this should be clipped");
|
||||
TEST_ASSERT_EQUAL_STRING("text to be clipped to 32 chars.", lv_subject_get_string(&subject));
|
||||
TEST_ASSERT_EQUAL_STRING("how are you?", lv_subject_get_previous_string(&subject));
|
||||
TEST_ASSERT_EQUAL_STRING("1: Coding is fun !", lv_subject_get_previous_string(&subject));
|
||||
|
||||
/*Check if the previous string is clipped correctly*/
|
||||
lv_subject_copy_string(&subject, "a");
|
||||
|
Loading…
Reference in New Issue
Block a user