Skip to content

Commit

Permalink
add max_chars property to all texts (#414)
Browse files Browse the repository at this point in the history
  • Loading branch information
FelixKratz committed Oct 15, 2023
1 parent 934c089 commit dc59f2e
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 3 deletions.
1 change: 1 addition & 0 deletions src/bar_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
1 change: 1 addition & 0 deletions src/misc/defines.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
43 changes: 40 additions & 3 deletions src/text.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);

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++;
}

CFStringRef string = CFStringCreateWithCString(NULL,
text->string,
kCFStringEncodingUTF8);
string = CFStringCreateWithCString(NULL,
buffer,
kCFStringEncodingUTF8);
} else {
string = CFStringCreateWithCString(NULL,
text->string,
kCFStringEncodingUTF8);

}

if (!string) string = CFStringCreateWithCString(NULL,
"Warning: Malformed UTF-8 string",
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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("");
Expand Down Expand Up @@ -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,
Expand Down
1 change: 1 addition & 0 deletions src/text.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ struct text {
int padding_left;
int padding_right;
uint32_t custom_width;
uint32_t max_chars;

CGRect bounds;

Expand Down

0 comments on commit dc59f2e

Please sign in to comment.