-
Notifications
You must be signed in to change notification settings - Fork 4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[Issue #21] Adds the ability to put count and ratio in the layout. #22
base: master
Are you sure you want to change the base?
Conversation
EDIT: emojis "just work." I'll take it. EDIT2: everything good now
This looks great - very easy to define. I'll test and review tomorrow. |
Happy to help out with tests; they should be straightforward unit. |
Hello, it seems that I ran make test, and then make iwyu. (thoughtlessly) Incorporating the suggestions of make iwyu broke the tests. Anyway, should be good now. |
Inconsistent iwyu is becoming a problem, I think I'll just remove it. I've just made a config change #23 to cppcheck, for versions greater than 2.7 (CI) It looks like everything is passing now, nice work. If it's OK with you I'll:
|
Looks good! Thanks for putting up with me not following CONTRIBUTING.md. For future, how do I run checks locally? |
You're all good. It looks like you've been running all of them. Not sure how your environment's setup, but you can manually check cross-compiler: make CC=gcc clean all
make CC=clang clean all I'll deal with iwyu |
src/layout.c
Outdated
|
||
return desc; | ||
} | ||
|
||
const char *description_debug(const struct Demand* const demand, const struct Tag* const tag) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We can delete this now :)
description_info
can then be moved into layout_description
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looking good, I'm using it right now.
I've added a test suite for layout and added one basic test for cfg_set_layout_format
.
Please
- add
{n}
- delete
description_debug
and movedescription_info
intolayout_description
- complete test cases for
cfg_set_layout_format
I'll think about how we can simplify description_info
. I've seen something similar but I can't recall it right now.
I'm unavailable until next weekend.
src/cfg.c
Outdated
escaped = 0; | ||
} | ||
|
||
strcpy(c.layout_format, s); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This will need to be a strncpy - it will trip the linux package security scanners.
src/layout.c
Outdated
break; | ||
} | ||
|
||
static char desc[LAYOUT_FORMAT_LEN+5]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This works, however it's likely to trip the linux package security scanners.
You're correctly using snprintfs and checking bounds, however the scanners are very strict and won't be able to follow the complex logic here.
First thoughts are that we could just a man 3 regexec
, which is known to safely handle buffers and allocation.
Alternatively, we could use strstr
or strtok
to find the exact tokens instead of looping, as we don't really need a complex pattern.
Sorry, I have to get to that... What IDE do you use? vim |
We could use a simple string replacer. Let me know if that makes sense and I'll push it. static char desc[LAYOUT_FORMAT_LEN+5];
snprintf(sratio, 10, "%g", ratio);
snprintf(scount, 10, "%d", count);
char *counted = replace_string(cfg->layout_format, "{c}", scount);
char *ratioed = replace_string(counted, "{r}", sratio);
free(counted);
char *imaged = replace_string(ratioed, "{l}", image);
free(ratioed);
strncpy(desc, imaged, LAYOUT_FORMAT_LEN + 5);
free(imaged);
return desc; // return a copy of str with instances of target substituted with replacement
// call frees
char *replace_string(const char * const str, const char * const target, const char * const replacement) {
char *match = strstr(str, target);
if (!match) {
return strdup(str);
}
// allocate result
size_t len = strlen(str) + strlen(replacement) - strlen(target) + 1;
char *res = calloc(len, sizeof(char));
// find string up to match
size_t len_pre = match - str + 1;
char pre[len_pre];
snprintf(pre, len_pre, "%s", str);
// format
snprintf(res, len, "%s%s%s", pre, replacement, match + strlen(target));
// recurse
char *next = replace_string(res, target, replacement);
free(res);
return next;
} |
OK, so I added {n}, changed some things to use strn functions. my impl of {n} is pretty bad, as I wasn't sure how to integrate with the existing switch / case. Also, I/you will have to change it to use your string replacer (which LGTM). EDIT: Your tests were using (the now nonexistent) description_info, so i switched them to layout_description() instead. Sorry for the horrible git history. |
Looks good, moved it to the very hidden function from enum.c
Updated layout_description to use string replace. Changed the layout functions to be caller frees, as per the original comments.
Don't worry about git history, it will be squashed on merge. We do need to take care of the documentation - man and readme. I'd be grateful if you could update |
Unfortunately I'm out of time; I'll get back to this next weekend. This one will be a slow burn... |
@@ -17,59 +17,77 @@ void usage(const int status) { | |||
"\n" | |||
" --stack %s|%s|%s %s\n" | |||
"\n" | |||
" --count-master count %d %d <= count\n" | |||
" --ratio-master ratio %.02f %.1g <= ratio <= %.1g\n" | |||
" --count-master count " |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please don't change formatting for existing lines.
…gop-truncation" This reverts commit 136b995.
@@ -190,6 +191,39 @@ bool cfg_set_border_width_monocle(const char *s) { | |||
} | |||
} | |||
|
|||
bool cfg_set_layout_format(const char *s) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't quite understand the failure cases of this function; I might be missing the point, but could it just be "garbage in, garbage out"?
Can you please add tests to tst-args_cli.c
to cover these cases?
This PR adds a --layout-format flag which enables setting the layout to an arbitrary string containing count {c}, ratio {r}, and layout image {l}. for example passing --layout-format "{c} {r}\n{l}" may result in something like:
This flag is optional, and takes the default value {l}, which should have the same behaviour as before this PR. Within the string, brackets are allowed using \{, \}. \n, \r, \t, \v, and nerdfont icons are also supported.
Tests have not been written, and I am definitely not a C programmer, so I would be more surprised than not if the code has no vulnerabilities / edge cases that I am missing.
NOTE 1: I committed the flake.lock / flake.nix for future contributors, but its up to you if you want to keep them.
NOTE 2: There is no clang-format, so my formatting is likely wrong.