Skip to content

Commit

Permalink
default block section seems to work; cfg options revised appropriately
Browse files Browse the repository at this point in the history
  • Loading branch information
domsson committed Oct 1, 2020
1 parent 5e6e8c2 commit cb3481a
Show file tree
Hide file tree
Showing 5 changed files with 149 additions and 161 deletions.
14 changes: 7 additions & 7 deletions cfg/succaderc.1
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,17 @@ name = "testbar"
blocks = "desktop | user | date time"
height = 24
areas = 16
foreground = "#000000"
background = "#ce7942"
underline = true
foreground = "#550000"
background = "#555555"
line-width = 3
line-color = "#333333"
label-foreground = "#333333"
block-prefix = " "
block-suffix = " "
block-margin = 4
font = 6x13

[default]
label-foreground = "#FF2222"
prefix = " "
suffix = " "

[desktop]
command = "bspc query -D -d focused --names"
trigger = "bspc subscribe"
Expand Down
7 changes: 6 additions & 1 deletion src/cfg.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,11 @@ int cfg_has(const cfg_s *cfg, size_t idx)
return (idx < cfg->size && cfg->type[idx] != 0);
}

cfg_opt_type_e cfg_type(const cfg_s *cfg, size_t idx)
{
return cfg->type[idx];
}

void cfg_set_int(const cfg_s *cfg, size_t idx, int val)
{
if (idx >= cfg->size)
Expand Down Expand Up @@ -114,7 +119,7 @@ int cfg_get_int(const cfg_s *cfg, size_t idx)
float cfg_get_float(const cfg_s *cfg, size_t idx)
{
return (idx < cfg->size && cfg->type[idx] == OPT_TYPE_FLOAT) ?
cfg->opts[idx].f : 0;
cfg->opts[idx].f : 0.0;
}

char *cfg_get_str(const cfg_s *cfg, size_t idx)
Expand Down
70 changes: 12 additions & 58 deletions src/loadini.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <stdlib.h> // NULL, size_t, EXIT_SUCCESS, EXIT_FAILURE, ...
#include <string.h> // strlen(), strcmp(), ...
#include <float.h> // DBL_MAX
#include <string.h> // strdup()
#include "ini.h" // https://github.com/benhoyt/inih
#include "succade.h" // defines, structs, all that stuff

Expand Down Expand Up @@ -31,7 +30,7 @@ int lemon_ini_handler(void *data, const char *section, const char *name, const c
cfg_set_str(lc, LEMON_OPT_BG, is_quoted(value) ? unquote(value) : strdup(value));
return 1;
}
if (equals(name, "line-color") || equals(name, "line") || equals(name, "lc"))
if (equals(name, "line-color") || equals(name, "lc"))
{
cfg_set_str(lc, LEMON_OPT_LC, is_quoted(value) ? unquote(value) : strdup(value));
return 1;
Expand All @@ -41,16 +40,6 @@ int lemon_ini_handler(void *data, const char *section, const char *name, const c
cfg_set_int(lc, LEMON_OPT_LW, atoi(value));
return 1;
}
if (equals(name, "overline") || equals(name, "ol"))
{
cfg_set_int(lc, LEMON_OPT_OL, equals(value, "true"));
return 1;
}
if (equals(name, "underline") || equals(name, "ul"))
{
cfg_set_int(lc, LEMON_OPT_UL, equals(value, "true"));
return 1;
}
if (equals(name, "height") || equals(name, "h"))
{
cfg_set_int(lc, LEMON_OPT_HEIGHT, atoi(value));
Expand Down Expand Up @@ -91,26 +80,6 @@ int lemon_ini_handler(void *data, const char *section, const char *name, const c
cfg_set_int(lc, LEMON_OPT_AREAS, atoi(value));
return 1;
}
if (equals(name, "block-margin") || equals(name, "margin"))
{
cfg_set_int(lc, LEMON_OPT_BLOCK_MARGIN, atoi(value));
return 1;
}
if (equals(name, "block-padding") || equals(name, "padding"))
{
cfg_set_int(lc, LEMON_OPT_BLOCK_PADDING, atoi(value));
return 1;
}
if (equals(name, "block-prefix") || equals(name, "prefix"))
{
cfg_set_str(lc, LEMON_OPT_BLOCK_PREFIX, is_quoted(value) ? unquote(value) : strdup(value));
return 1;
}
if (equals(name, "block-suffix") || equals(name, "suffix"))
{
cfg_set_str(lc, LEMON_OPT_BLOCK_SUFFIX, is_quoted(value) ? unquote(value) : strdup(value));
return 1;
}
if (equals(name, "blocks") || equals(name, "format"))
{
cfg_set_str(lc, LEMON_OPT_FORMAT, is_quoted(value) ? unquote(value) : strdup(value));
Expand All @@ -131,31 +100,6 @@ int lemon_ini_handler(void *data, const char *section, const char *name, const c
cfg_set_str(lc, LEMON_OPT_AFFIX_FONT, is_quoted(value) ? unquote(value) : strdup(value));
return 1;
}
if (equals(name, "block-background") || equals(name, "block-bg"))
{
cfg_set_str(lc, LEMON_OPT_BLOCK_BG, is_quoted(value) ? unquote(value) : strdup(value));
return 1;
}
if (equals(name, "label-background") || equals(name, "label-bg"))
{
cfg_set_str(lc, LEMON_OPT_LABEL_BG, is_quoted(value) ? unquote(value) : strdup(value));
return 1;
}
if (equals(name, "label-foreground") || equals(name, "label-fg"))
{
cfg_set_str(lc, LEMON_OPT_LABEL_FG, is_quoted(value) ? unquote(value) : strdup(value));
return 1;
}
if (equals(name, "affix-background") || equals(name, "affix-bg"))
{
cfg_set_str(lc, LEMON_OPT_AFFIX_BG, is_quoted(value) ? unquote(value) : strdup(value));
return 1;
}
if (equals(name, "affix-foreground") || equals(name, "affix-fg"))
{
cfg_set_str(lc, LEMON_OPT_AFFIX_FG, is_quoted(value) ? unquote(value) : strdup(value));
return 1;
}

// Unknown section or name
return 0;
Expand Down Expand Up @@ -233,6 +177,16 @@ int block_ini_handler(void *data, const char *section, const char *name, const c
cfg_set_int(bc, BLOCK_OPT_MARGIN_RIGHT, atoi(value));
return 1;
}
if (equals(name, "padding-left"))
{
cfg_set_int(bc, BLOCK_OPT_PADDING_LEFT, atoi(value));
return 1;
}
if (equals(name, "padding-right"))
{
cfg_set_int(bc, BLOCK_OPT_PADDING_RIGHT, atoi(value));
return 1;
}
if (equals(name, "prefix"))
{
cfg_set_str(bc, BLOCK_OPT_PREFIX, is_quoted(value) ? unquote(value) : strdup(value));
Expand Down
113 changes: 74 additions & 39 deletions src/succade.c
Original file line number Diff line number Diff line change
Expand Up @@ -328,11 +328,10 @@ static double time_to_wait(state_s *state, double now)
* string this function is putting together, otherwise truncation will happen.
* Alternatively, set `len` to 0 to let this function calculate the buffer.
*/
static char *blockstr(const thing_s *lemon, const thing_s *block)
static char *blockstr(const thing_s *block)
{
// for convenience
const cfg_s *bcfg = &block->cfg;
const cfg_s *lcfg = &lemon->cfg;

char action_start[(5 * strlen(block->sid)) + 64];
char action_end[24];
Expand Down Expand Up @@ -383,29 +382,29 @@ static char *blockstr(const thing_s *lemon, const thing_s *block)
// calculating the padding (fixed width) of the block, no?

size_t diff = 0;
char *result = (cfg_has(bcfg, BLOCK_OPT_RAW) && cfg_get_int(bcfg, BLOCK_OPT_RAW)) ?
char *result = cfg_get_int(bcfg, BLOCK_OPT_RAW) ?
strdup(block->output) : escape(block->output, '%', &diff);
int min_width = cfg_get_int(bcfg, BLOCK_OPT_WIDTH) + diff;

const char *block_fg = strsel(cfg_get_str(bcfg, BLOCK_OPT_FG), "-", "-");
const char *block_bg = strsel(cfg_get_str(bcfg, BLOCK_OPT_BG), cfg_get_str(lcfg, LEMON_OPT_BLOCK_BG), "-");
const char *label_fg = strsel(cfg_get_str(bcfg, BLOCK_OPT_LABEL_FG), cfg_get_str(lcfg, LEMON_OPT_LABEL_FG), "-");
const char *label_bg = strsel(cfg_get_str(bcfg, BLOCK_OPT_LABEL_BG), cfg_get_str(lcfg, LEMON_OPT_LABEL_BG), "-");
const char *affix_fg = strsel(cfg_get_str(bcfg, BLOCK_OPT_AFFIX_FG), cfg_get_str(lcfg, LEMON_OPT_AFFIX_FG), "-");
const char *affix_bg = strsel(cfg_get_str(bcfg, BLOCK_OPT_AFFIX_BG), cfg_get_str(lcfg, LEMON_OPT_AFFIX_BG), "-");

int padding = cfg_get_int(lcfg, LEMON_OPT_BLOCK_PADDING);
int margin = cfg_get_int(lcfg, LEMON_OPT_BLOCK_MARGIN);
int margin_l = cfg_has(bcfg, BLOCK_OPT_MARGIN_LEFT) ? cfg_get_int(bcfg, BLOCK_OPT_MARGIN_LEFT) : -1;
int margin_r = cfg_has(bcfg, BLOCK_OPT_MARGIN_RIGHT) ? cfg_get_int(bcfg, BLOCK_OPT_MARGIN_RIGHT) : -1;
int ol = cfg_has(bcfg, BLOCK_OPT_OL) ? cfg_get_int(bcfg, BLOCK_OPT_OL) : cfg_get_int(lcfg, LEMON_OPT_OL);
int ul = cfg_has(bcfg, BLOCK_OPT_UL) ? cfg_get_int(bcfg, BLOCK_OPT_UL) : cfg_get_int(lcfg, LEMON_OPT_UL);
const char *lc = strsel(cfg_get_str(bcfg, BLOCK_OPT_LC), cfg_get_str(lcfg, LEMON_OPT_LC) , "-");

const char *prefix = strsel(cfg_get_str(bcfg, BLOCK_OPT_PREFIX), cfg_get_str(lcfg, LEMON_OPT_BLOCK_PREFIX), "");
const char *suffix = strsel(cfg_get_str(bcfg, BLOCK_OPT_PREFIX), cfg_get_str(lcfg, LEMON_OPT_BLOCK_SUFFIX), "");
const char *label = strsel(cfg_get_str(bcfg, BLOCK_OPT_LABEL), "", "");
//const char *unit = strsel(cfg_get_str(bcfg, BLOCK_OPT_UNIT), "", "");
const char *block_fg = strsel(cfg_get_str(bcfg, BLOCK_OPT_FG), "-", "");
const char *block_bg = strsel(cfg_get_str(bcfg, BLOCK_OPT_BG), "-", "");
const char *label_fg = strsel(cfg_get_str(bcfg, BLOCK_OPT_LABEL_FG), "-", "");
const char *label_bg = strsel(cfg_get_str(bcfg, BLOCK_OPT_LABEL_BG), "-", "");
const char *affix_fg = strsel(cfg_get_str(bcfg, BLOCK_OPT_AFFIX_FG), "-", "");
const char *affix_bg = strsel(cfg_get_str(bcfg, BLOCK_OPT_AFFIX_BG), "-", "");
const char *lc = strsel(cfg_get_str(bcfg, BLOCK_OPT_LC), "-", "");

const char *prefix = strsel(cfg_get_str(bcfg, BLOCK_OPT_PREFIX), "", "");
const char *suffix = strsel(cfg_get_str(bcfg, BLOCK_OPT_PREFIX), "", "");
const char *label = strsel(cfg_get_str(bcfg, BLOCK_OPT_LABEL), "", "");
//const char *unit = strsel(cfg_get_str(bcfg, BLOCK_OPT_UNIT), "", "");

int padding_l = cfg_get_int(bcfg, BLOCK_OPT_PADDING_LEFT);
int padding_r = cfg_get_int(bcfg, BLOCK_OPT_PADDING_RIGHT);
int margin_l = cfg_get_int(bcfg, BLOCK_OPT_MARGIN_LEFT);
int margin_r = cfg_get_int(bcfg, BLOCK_OPT_MARGIN_RIGHT);
int ol = cfg_get_int(bcfg, BLOCK_OPT_OL);
int ul = cfg_get_int(bcfg, BLOCK_OPT_UL);

// TODO currently we are adding the format thingies for label,
// prefix and suffix, even if those are empty anyway, which
Expand All @@ -416,29 +415,34 @@ static char *blockstr(const thing_s *lemon, const thing_s *block)

char *str = malloc(BUFFER_BLOCK_STR);
snprintf(str, BUFFER_BLOCK_STR,
"%%{O%d}"
"%s%%{F%s B%s U%s %co %cu}"
"%%{T3 F%s B%s}%s"
"%%{T2 F%s B%s}%s"
"%%{T1 F%s B%s}%*s%*s%*s"
"%%{T3 F%s B%s}%s"
"%%{T- F- B- U- -o -u}%s"
"%%{O%d}",
"%%{O%d}" // margin left
"%s" // action start
"%%{F%s B%s U%s %co %cu}" // format start
"%%{T3 F%s B%s}%s" // prefix
"%%{T2 F%s B%s}%s" // label
"%%{T1 F%s B%s}%*s%*s%*s" // block
"%%{T3 F%s B%s}%s" // suffix
"%%{T- F- B- U- -o -u}" // format end
"%s" // action end
"%%{O%d}", // margin right
// margin left
margin_l >= 0 ? margin_l : margin,
// start
action_start, block_fg, block_bg, lc, (ol ? '+' : '-'), (ul ? '+' : '-'),
margin_l,
// action start
action_start,
// format start
block_fg, block_bg, lc, (ol ? '+' : '-'), (ul ? '+' : '-'),
// prefix
affix_fg, affix_bg, prefix,
// label
label_fg, label_bg, label,
// block
block_fg, block_bg, padding, "", min_width, result, padding, "",
block_fg, block_bg, padding_l, "", min_width, result, padding_r, "",
// suffix
affix_fg, affix_bg, suffix,
// end
// action end
action_end,
margin_r >= 0 ? margin_r : margin
// margin right
margin_r
);

free(result);
Expand Down Expand Up @@ -468,7 +472,6 @@ static char *barstr(const state_s *state)
}

// For convenience...
const thing_s *bar = &state->lemon;
size_t num_blocks = state->num_blocks;

// Short blocks like temperature, volume or battery, will usually use
Expand All @@ -493,7 +496,7 @@ static char *barstr(const state_s *state)

int block_align = cfg_get_int(&block->cfg, BLOCK_OPT_ALIGN);

char *block_str = blockstr(bar, block);
char *block_str = blockstr(block);
size_t block_str_len = strlen(block_str);
if (block_align != last_align)
{
Expand Down Expand Up @@ -671,7 +674,7 @@ int block_cfg_handler(void *data, const char *section, const char *name, const c
}

// Find the block whose name fits the section name
thing_s *block = get_block(state, section);
thing_s *block = equals(section, ALBEDO_SID) ? &state->albedo : get_block(state, section);

// Abort if we couldn't find that block
if (block == NULL)
Expand Down Expand Up @@ -1275,6 +1278,16 @@ int main(int argc, char **argv)
return EXIT_FAILURE;
}

//
// ALBEDO
//

thing_s *albedo = &(state.albedo); // For convenience
albedo->sid = ALBEDO_SID;
albedo->t_type = THING_BLOCK;
albedo->b_type = BLOCK_NONE;
cfg_init(&albedo->cfg, ALBEDO_SID, BLOCK_OPT_COUNT);

//
// BLOCKS
//
Expand Down Expand Up @@ -1308,6 +1321,28 @@ int main(int argc, char **argv)
char *block_bin = cfg_get_str(&block->cfg, BLOCK_OPT_BIN);
char *block_cmd = block_bin ? block_bin : block->sid;
block->child = make_child(&state, block_cmd, 0, 1, 1);

// TODO merge albedo (default config) with this block's config
for (int i = 0; i < BLOCK_OPT_COUNT; ++i)
{
if (cfg_has(&albedo->cfg, i) && !cfg_has(&block->cfg, i))
{
switch (cfg_type(&albedo->cfg, i))
{
case OPT_TYPE_INT:
cfg_set_int(&block->cfg, i, cfg_get_int(&albedo->cfg, i));
break;
case OPT_TYPE_FLOAT:
cfg_set_float(&block->cfg, i, cfg_get_float(&albedo->cfg, i));
break;
case OPT_TYPE_STRING:
cfg_set_str(&block->cfg, i, strdup(cfg_get_str(&albedo->cfg, i)));
break;
default:
break;
}
}
}
}

//
Expand Down
Loading

0 comments on commit cb3481a

Please sign in to comment.