aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--common/util.c9
-rw-r--r--include/util.h5
-rw-r--r--sway/commands/bar.c4
-rw-r--r--swaybar/ipc.c11
4 files changed, 8 insertions, 21 deletions
diff --git a/common/util.c b/common/util.c
index bd7bed2d..edbbf3f7 100644
--- a/common/util.c
+++ b/common/util.c
@@ -11,15 +11,6 @@ int wrap(int i, int max) {
11 return ((i % max) + max) % max; 11 return ((i % max) + max) % max;
12} 12}
13 13
14int numlen(int n) {
15 int j = n <= 0 ? 1 : 0;
16 while (n) {
17 j++;
18 n /= 10;
19 }
20 return j;
21}
22
23uint32_t parse_color(const char *color) { 14uint32_t parse_color(const char *color) {
24 if (color[0] == '#') { 15 if (color[0] == '#') {
25 ++color; 16 ++color;
diff --git a/include/util.h b/include/util.h
index e3269d6b..1fd772c0 100644
--- a/include/util.h
+++ b/include/util.h
@@ -10,11 +10,6 @@
10int wrap(int i, int max); 10int wrap(int i, int max);
11 11
12/** 12/**
13 * Count number of digits in int, including '-' sign if there is one
14 */
15int numlen(int n);
16
17/**
18 * Given a string that represents an RGB(A) color, return a uint32_t 13 * Given a string that represents an RGB(A) color, return a uint32_t
19 * version of the color. 14 * version of the color.
20 */ 15 */
diff --git a/sway/commands/bar.c b/sway/commands/bar.c
index e9360603..82441f9e 100644
--- a/sway/commands/bar.c
+++ b/sway/commands/bar.c
@@ -1,10 +1,10 @@
1#define _POSIX_C_SOURCE 200809 1#define _POSIX_C_SOURCE 200809
2#include <stdio.h>
2#include <string.h> 3#include <string.h>
3#include <strings.h> 4#include <strings.h>
4#include "sway/commands.h" 5#include "sway/commands.h"
5#include "sway/config.h" 6#include "sway/config.h"
6#include "log.h" 7#include "log.h"
7#include "util.h"
8 8
9// Must be in alphabetical order for bsearch 9// Must be in alphabetical order for bsearch
10static struct cmd_handler bar_handlers[] = { 10static struct cmd_handler bar_handlers[] = {
@@ -89,7 +89,7 @@ struct cmd_results *cmd_bar(int argc, char **argv) {
89 } 89 }
90 90
91 // set bar id 91 // set bar id
92 const int len = 5 + numlen(config->bars->length - 1); // "bar-"+i+\0 92 const int len = snprintf(NULL, 0, "bar-%d", config->bars->length - 1) + 1;
93 bar->id = malloc(len * sizeof(char)); 93 bar->id = malloc(len * sizeof(char));
94 if (bar->id) { 94 if (bar->id) {
95 snprintf(bar->id, len, "bar-%d", config->bars->length - 1); 95 snprintf(bar->id, len, "bar-%d", config->bars->length - 1);
diff --git a/swaybar/ipc.c b/swaybar/ipc.c
index 29b782bb..dbb593fb 100644
--- a/swaybar/ipc.c
+++ b/swaybar/ipc.c
@@ -1,5 +1,6 @@
1#define _POSIX_C_SOURCE 200809 1#define _POSIX_C_SOURCE 200809
2#include <limits.h> 2#include <limits.h>
3#include <stdio.h>
3#include <string.h> 4#include <string.h>
4#include <strings.h> 5#include <strings.h>
5#include <json-c/json.h> 6#include <json-c/json.h>
@@ -9,6 +10,7 @@
9#include "ipc-client.h" 10#include "ipc-client.h"
10#include "list.h" 11#include "list.h"
11#include "log.h" 12#include "log.h"
13#include "util.h"
12 14
13void ipc_send_workspace_command(struct swaybar *bar, const char *ws) { 15void ipc_send_workspace_command(struct swaybar *bar, const char *ws) {
14 const char *fmt = "workspace \"%s\""; 16 const char *fmt = "workspace \"%s\"";
@@ -372,15 +374,14 @@ bool ipc_get_workspaces(struct swaybar *bar) {
372 ws->label = strdup(ws->name); 374 ws->label = strdup(ws->name);
373 // ws->num will be -1 if workspace name doesn't begin with int. 375 // ws->num will be -1 if workspace name doesn't begin with int.
374 if (ws->num != -1) { 376 if (ws->num != -1) {
375 size_t len_offset = numlen(ws->num); 377 size_t len_offset = snprintf(NULL, 0, "%d", ws->num);
376 if (bar->config->strip_workspace_name) { 378 if (bar->config->strip_workspace_name) {
377 free(ws->label); 379 free(ws->label);
378 ws->label = malloc(len_offset + 1 * sizeof(char)); 380 ws->label = malloc(len_offset + 1);
379 ws->label[len_offset] = '\0'; 381 snprintf(ws->label, len_offset + 1, "%d", ws->num);
380 strncpy(ws->label, ws->name, len_offset);
381 } else if (bar->config->strip_workspace_numbers) { 382 } else if (bar->config->strip_workspace_numbers) {
382 len_offset += ws->label[len_offset] == ':'; 383 len_offset += ws->label[len_offset] == ':';
383 if (strlen(ws->name) > len_offset) { 384 if (ws->name[len_offset] != '\0') {
384 free(ws->label); 385 free(ws->label);
385 // Strip number prefix [1-?:] using len_offset. 386 // Strip number prefix [1-?:] using len_offset.
386 ws->label = strdup(ws->name + len_offset); 387 ws->label = strdup(ws->name + len_offset);