add max_chars property to all texts (#414)

This commit is contained in:
Felix Kratz 2023-10-15 15:51:30 +02:00
parent 934c089595
commit dc59f2e36a
4 changed files with 43 additions and 3 deletions

View file

@ -977,6 +977,7 @@ void bar_manager_handle_system_woke(struct bar_manager* bar_manager) {
bar_manager_custom_events_trigger(bar_manager,
COMMAND_SUBSCRIBE_SYSTEM_WOKE,
NULL );
bar_manager_display_changed(bar_manager);
}
}

View file

@ -95,6 +95,7 @@
#define PROPERTY_IGNORE_ASSOCIATION "ignore_association"
#define PROPERTY_EVENT_PORT "mach_helper"
#define PROPERTY_PERCENTAGE "percentage"
#define PROPERTY_MAX_CHARS "max_chars"
#define DOMAIN_BAR "--bar"
#define PROPERTY_POSITION "position"

View file

@ -16,10 +16,35 @@ static void text_prepare_line(struct text* text) {
array_count(keys),
&kCFTypeDictionaryKeyCallBacks,
&kCFTypeDictionaryValueCallBacks);
CFStringRef string;
if (text->max_chars > 0) {
uint32_t len = strlen(text->string) + 4;
char buffer[len];
memset(buffer, 0, len);
CFStringRef string = CFStringCreateWithCString(NULL,
text->string,
kCFStringEncodingUTF8);
char* read = text->string;
char* write = buffer;
uint32_t counter = 0;
while (*read) {
if ((*read & 0xC0) != 0x80) counter++;
if (counter > text->max_chars) {
*write++ = 0xE2;
*write++ = 0x80;
*write++ = 0xA6;
break;
}
*write++ = *read++;
}
string = CFStringCreateWithCString(NULL,
buffer,
kCFStringEncodingUTF8);
} else {
string = CFStringCreateWithCString(NULL,
text->string,
kCFStringEncodingUTF8);
}
if (!string) string = CFStringCreateWithCString(NULL,
"Warning: Malformed UTF-8 string",
@ -54,6 +79,15 @@ static void text_destroy_line(struct text* text) {
text->line.line = NULL;
}
bool text_set_max_chars(struct text* text, uint32_t max_chars) {
if (text->max_chars == max_chars) return false;
text->max_chars = max_chars;
if (strlen(text->string) > text->max_chars) {
text_set_string(text, text->string, true);
}
return strlen(text->string) > text->max_chars;
}
bool text_set_string(struct text* text, char* string, bool forced) {
if (!string) return false;
if (!forced && text->string && strcmp(text->string, string) == 0) {
@ -87,6 +121,7 @@ void text_init(struct text* text) {
text->padding_left = 0;
text->padding_right = 0;
text->y_offset = 0;
text->max_chars = 0;
text->align = POSITION_LEFT;
text->string = string_copy("");
@ -419,6 +454,8 @@ bool text_parse_sub_domain(struct text* text, FILE* rsp, struct token property,
}
return changed;
} else if (token_equals(property, PROPERTY_MAX_CHARS)) {
return text_set_max_chars(text, token_to_int(get_token(&message)));
}
else {
struct key_value_pair key_value_pair = get_key_value_pair(property.text,

View file

@ -21,6 +21,7 @@ struct text {
int padding_left;
int padding_right;
uint32_t custom_width;
uint32_t max_chars;
CGRect bounds;