-
Notifications
You must be signed in to change notification settings - Fork 256
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
eccd4a7
commit b5a804d
Showing
8 changed files
with
192 additions
and
81 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
/* | ||
* vim:ts=4:sw=4:expandtab | ||
* | ||
* i3 - an improved dynamic tiling window manager | ||
* © 2009 Michael Stapelberg and contributors (see also: LICENSE) | ||
* | ||
*/ | ||
// copied from i3:libi3/format_placeholders.c | ||
#include <stdbool.h> | ||
#include <stdint.h> | ||
#include <stdio.h> | ||
#include <stdlib.h> | ||
#include <string.h> | ||
|
||
#include "i3status.h" | ||
|
||
#ifndef CS_STARTS_WITH | ||
#define CS_STARTS_WITH(string, needle) (strncmp((string), (needle), strlen((needle))) == 0) | ||
#endif | ||
|
||
/* | ||
* Replaces occurrences of the defined placeholders in the format string. | ||
* | ||
*/ | ||
char *format_placeholders(const char *format, placeholder_t *placeholders, int num) { | ||
if (format == NULL) | ||
return NULL; | ||
|
||
/* We have to first iterate over the string to see how much buffer space | ||
* we need to allocate. */ | ||
int buffer_len = strlen(format) + 1; | ||
for (const char *walk = format; *walk != '\0'; walk++) { | ||
for (int i = 0; i < num; i++) { | ||
if (!CS_STARTS_WITH(walk, placeholders[i].name)) | ||
continue; | ||
|
||
buffer_len = buffer_len - strlen(placeholders[i].name) + strlen(placeholders[i].value); | ||
walk += strlen(placeholders[i].name) - 1; | ||
break; | ||
} | ||
} | ||
|
||
/* Now we can parse the format string. */ | ||
char buffer[buffer_len]; | ||
char *outwalk = buffer; | ||
for (const char *walk = format; *walk != '\0'; walk++) { | ||
if (*walk != '%') { | ||
*(outwalk++) = *walk; | ||
continue; | ||
} | ||
|
||
bool matched = false; | ||
for (int i = 0; i < num; i++) { | ||
if (!CS_STARTS_WITH(walk, placeholders[i].name)) { | ||
continue; | ||
} | ||
|
||
matched = true; | ||
outwalk += sprintf(outwalk, "%s", placeholders[i].value); | ||
walk += strlen(placeholders[i].name) - 1; | ||
break; | ||
} | ||
|
||
if (!matched) | ||
*(outwalk++) = *walk; | ||
} | ||
|
||
*outwalk = '\0'; | ||
return sstrdup(buffer); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
POWER_SUPPLY_STATUS=Discharging | ||
POWER_SUPPLY_CURRENT_NOW=1107000 | ||
POWER_SUPPLY_CHARGE_FULL_DESIGN=7800000 | ||
POWER_SUPPLY_CHARGE_NOW=2390000 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
I can %haz literal% % ? |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
general { | ||
output_format = "none" | ||
} | ||
|
||
order += "battery all" | ||
|
||
battery all { | ||
format = "I can %haz literal% % ?" | ||
path = "testcases/020-percentliteral-battery/BAT%d_uevent" | ||
} |