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:
Fabian Zäh 2024-11-18 11:17:38 +01:00 committed by GitHub
parent f677c25abc
commit 2d06699aa0
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 41 additions and 2 deletions

View File

@ -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) {

View File

@ -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

View File

@ -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");