Skip to content
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

Open
wants to merge 22 commits into
base: master
Choose a base branch
from

Conversation

hfytr
Copy link

@hfytr hfytr commented Dec 1, 2024

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:

3 0.5
│├──┤

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.

EDIT: emojis "just work." I'll take it.
EDIT2: everything good now
@alex-courtis
Copy link
Owner

This looks great - very easy to define.

I'll test and review tomorrow.

@alex-courtis
Copy link
Owner

Happy to help out with tests; they should be straightforward unit.

@hfytr
Copy link
Author

hfytr commented Dec 1, 2024

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.

@alex-courtis
Copy link
Owner

alex-courtis commented Dec 2, 2024

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:

  • update this branch
  • write and push tests

@hfytr
Copy link
Author

hfytr commented Dec 2, 2024

Looks good! Thanks for putting up with me not following CONTRIBUTING.md. For future, how do I run checks locally?

@alex-courtis
Copy link
Owner

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. cppcheck is the useful one: https://github.com/alex-courtis/wideriver/blob/master/CONTRIBUTING.md#lint

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) {
Copy link
Owner

@alex-courtis alex-courtis Dec 2, 2024

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

Copy link
Owner

@alex-courtis alex-courtis left a 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 move description_info into layout_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);
Copy link
Owner

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];
Copy link
Owner

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.

inc/enum.h Show resolved Hide resolved
@alex-courtis
Copy link
Owner

NOTE 2: There is no clang-format, so my formatting is likely wrong.

Sorry, I have to get to that...

What IDE do you use? vim gg=G is fine.

@alex-courtis
Copy link
Owner

alex-courtis commented Dec 7, 2024

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;
}

Archim Jhunjhunwala and others added 2 commits December 7, 2024 17:09
@hfytr
Copy link
Author

hfytr commented Dec 7, 2024

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.

@alex-courtis
Copy link
Owner

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).

Looks good, moved it to the very hidden function from enum.c

EDIT: Your tests were using (the now nonexistent) description_info, so i switched them to layout_description() instead.

Updated layout_description to use string replace. Changed the layout functions to be caller frees, as per the original comments.

Sorry for the horrible git history.

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 doc/templ/40.man.readme.md with the new options.
Don't worry if you can't run make doc, it's pretty hacky.

@alex-courtis
Copy link
Owner

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 "
Copy link
Owner

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.

@@ -190,6 +191,39 @@ bool cfg_set_border_width_monocle(const char *s) {
}
}

bool cfg_set_layout_format(const char *s) {
Copy link
Owner

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants