Skip to content

Commit

Permalink
add clock format support
Browse files Browse the repository at this point in the history
  • Loading branch information
somdoron committed Mar 30, 2020
1 parent 80b9fe7 commit 9dc81e4
Show file tree
Hide file tree
Showing 7 changed files with 43 additions and 4 deletions.
3 changes: 3 additions & 0 deletions doc/spacebar.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,9 @@ Settings
*status_bar_clock_icon* ['<sym>']::
Specify a symbol to represent the current time.

*status_bar_clock_format* ['<sym>']::
Specify a format for the current time, according to the strftime function.

Exit Codes
----------

Expand Down
1 change: 1 addition & 0 deletions examples/spacebarrc
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,6 @@ spacebar -m config status_bar_space_icon_strip I II III IV V VI VII VIII IX X
spacebar -m config status_bar_power_icon_strip  
spacebar -m config status_bar_space_icon 
spacebar -m config status_bar_clock_icon 
spacebar -m config status_bar_clock_format "%d/%m/%y %R"

echo "spacebar configuration loaded.."
3 changes: 2 additions & 1 deletion src/bar.c
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,8 @@ void bar_refresh(struct bar *bar)
struct tm *timeinfo = localtime(&rawtime);
if (timeinfo) {
char time[255];
snprintf(time, sizeof(time), "%02d:%02d", timeinfo->tm_hour, timeinfo->tm_min);
strftime(time, sizeof(time), g_bar_manager._clock_format, timeinfo);
//snprintf(time, sizeof(time), "%02d:%02d", timeinfo->tm_hour, timeinfo->tm_min);
struct bar_line time_line = bar_prepare_line(g_bar_manager.t_font, time, g_bar_manager.foreground_color);
CGPoint t_pos = bar_align_line(bar, time_line, ALIGN_RIGHT, ALIGN_CENTER);
bar_draw_line(bar, time_line, t_pos.x, t_pos.y);
Expand Down
25 changes: 24 additions & 1 deletion src/bar_manager.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,23 @@ void bar_manager_set_text_font(struct bar_manager *bar_manager, char *font_strin
bar_manager->t_font = bar_create_font(bar_manager->t_font_prop);
bar_manager->space_underline = bar_prepare_line(bar_manager->t_font, "______", rgba_color_from_hex(0xffd4d232));
bar_manager->power_underline = bar_prepare_line(bar_manager->t_font, "__________", rgba_color_from_hex(0xffd75f5f));
bar_manager->clock_underline = bar_prepare_line(bar_manager->t_font, "__________", rgba_color_from_hex(0xff458588));

time_t rawtime;
time(&rawtime);
struct tm *timeinfo = localtime(&rawtime);
if (timeinfo) {
char time[255];
strftime(time, sizeof(time), g_bar_manager._clock_format, timeinfo);
char underline[255] = {0};

for (int i = 0; i < strlen(time) + 4; ++i)
underline[i] = '_';

bar_manager->clock_underline = bar_prepare_line(bar_manager->t_font, underline, rgba_color_from_hex(0xff458588));
}
else
bar_manager->clock_underline = bar_prepare_line(bar_manager->t_font, "__________", rgba_color_from_hex(0xff458588));

bar_manager_refresh(bar_manager);
}

Expand Down Expand Up @@ -146,6 +162,12 @@ void bar_manager_set_clock_icon(struct bar_manager *bar_manager, char *icon)
bar_manager_refresh(bar_manager);
}

void bar_manager_set_clock_format(struct bar_manager *bar_manager, char *format)
{
bar_manager->_clock_format = format;
bar_manager_set_text_font(bar_manager, bar_manager->t_font_prop);
}

void bar_manager_set_space_icon(struct bar_manager *bar_manager, char *icon)
{
if (bar_manager->space_icon.line) {
Expand Down Expand Up @@ -194,6 +216,7 @@ void bar_manager_init(struct bar_manager *bar_manager)
bar_manager_set_background_color(bar_manager, 0xff202020);
bar_manager_set_foreground_color(bar_manager, 0xffa8a8a8);
bar_manager_set_clock_icon(bar_manager, string_copy(" "));
bar_manager_set_clock_format(bar_manager, string_copy("%R"));
bar_manager_set_space_icon(bar_manager, string_copy("*"));
bar_manager_set_power_strip(bar_manager, NULL);
}
Expand Down
2 changes: 2 additions & 0 deletions src/bar_manager.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ struct bar_manager
char **_space_icon_strip;
char **_power_icon_strip;
char *_clock_icon;
char *_clock_format;
char *_space_icon;
struct rgba_color foreground_color;
struct rgba_color background_color;
Expand All @@ -33,6 +34,7 @@ void bar_manager_set_icon_font(struct bar_manager *bar_manager, char *font_strin
void bar_manager_set_space_strip(struct bar_manager *bar_manager, char **icon_strip);
void bar_manager_set_power_strip(struct bar_manager *bar_manager, char **icon_strip);
void bar_manager_set_clock_icon(struct bar_manager *bar_manager, char *icon);
void bar_manager_set_clock_format(struct bar_manager *bar_manager, char *format);
void bar_manager_set_space_icon(struct bar_manager *bar_manager, char *icon);

void bar_manager_display_changed(struct bar_manager *bar_manager);
Expand Down
11 changes: 10 additions & 1 deletion src/message.c
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ extern bool g_verbose;
#define COMMAND_CONFIG_BAR_POWER_STRIP "status_bar_power_icon_strip"
#define COMMAND_CONFIG_BAR_SPACE_ICON "status_bar_space_icon"
#define COMMAND_CONFIG_BAR_CLOCK_ICON "status_bar_clock_icon"
#define COMMAND_CONFIG_BAR_CLOCK_FORMAT "status_bar_clock_format"

/* --------------------------------COMMON ARGUMENTS----------------------------- */
#define ARGUMENT_COMMON_VAL_ON "on"
Expand Down Expand Up @@ -187,7 +188,15 @@ static void handle_domain_config(FILE *rsp, struct token domain, char *message)
} else {
bar_manager_set_clock_icon(&g_bar_manager, token_to_string(token));
}
} else {
} else if (token_equals(command, COMMAND_CONFIG_BAR_CLOCK_FORMAT)) {
struct token token = get_token(&message);
if (!token_is_valid(token)) {
fprintf(rsp, "%s\n", g_bar_manager._clock_icon ? g_bar_manager._clock_icon : "");
} else {
bar_manager_set_clock_format(&g_bar_manager, token_to_string(token));
}
}
else {
daemon_fail(rsp, "unknown command '%.*s' for domain '%.*s'\n", command.length, command.text, domain.length, domain.text);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/spacebar.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
#define CONFIG_OPT_SHRT "-c"

#define MAJOR 0
#define MINOR 3
#define MINOR 4
#define PATCH 0

extern int SLSMainConnectionID(void);
Expand Down

0 comments on commit 9dc81e4

Please sign in to comment.