don't truncate long hostnames if there is space#87
Conversation
6059c5f to
d402639
Compare
|
Originally I intended to display the hostname with the same horizontal space as if it were the username, password and session head thingys. But I guess that now it makes sense to use all that horizontal space. Will make sure the code is good and merge it. Just remembered I have a release pending (#86) but uni is eating up my life t-t. If I get myself to it I will also push this into that release. |
|
Only problem with the PR is that the hostname was made to be calculated statically once given the available size and then used over and over. Now it's much more dynamic and it recalculates it every update, that also means it's leaking the allocated memory of the previous hostname every time it's displayed. |
|
Ah, right. Will fix that in a bit. |
|
Don't worry, I'm working on it and I think I almost have it, easy tweak. |
|
I think this patch should do: diff --git a/src/ui.c b/src/ui.c
index cacac1d..8f8c3ee 100644
--- a/src/ui.c
+++ b/src/ui.c
@@ -109,8 +109,8 @@ static char* fmt_time(const char* fmt) {
}
}
-char* trunc_gethostname(const size_t MAXLEN, const char* const ELLIPSIS) {
- if (utf8len(ELLIPSIS) > MAXLEN) return NULL;
+char* trunc_gethostname(size_t maxlen, const char* const ELLIPSIS) {
+ if (utf8len(ELLIPSIS) > maxlen) return NULL;
size_t alloc_size = HOST_NAME_MAX + strlen(ELLIPSIS) + 1;
char* buf = malloc(alloc_size);
if (!buf) return NULL;
@@ -120,8 +120,8 @@ char* trunc_gethostname(const size_t MAXLEN, const char* const ELLIPSIS) {
return NULL;
}
- if (utf8len(buf) > MAXLEN) {
- size_t end = utf8trunc(buf, MAXLEN - utf8len(ELLIPSIS));
+ if (utf8len(buf) > maxlen) {
+ size_t end = utf8trunc(buf, maxlen - utf8len(ELLIPSIS));
strcpy(&buf[end], ELLIPSIS);
}
return buf;
@@ -346,19 +346,29 @@ void print_head() {
size_t len_tm = utf8len(fmtd_time);
// calculate the space available for the host name
- ptrdiff_t hostname_size = BOX_WIDTH - BOX_HMARGIN - 1 - len_tm - VALUES_SEPR;
+ ssize_t hostname_size = BOX_WIDTH - (BOX_HMARGIN * 2) - len_tm - VALUES_SEPR;
if (hostname_size < 0) hostname_size = 0;
// hostname doesn't just change on runtime,
// but the length of the time string might
- char* hostname = trunc_gethostname(hostname_size, g_config->strings.ellipsis);
- if (!hostname) hostname = "unknown";
+ static char* NULLABLE hostname = NULL;
+ static size_t hostname_calcd_size;
+
+ // save the truncated hostname and the length it truncated to,
+ // if said length changes recalculate this (and free previous str)
+ if (!hostname || hostname_calcd_size != hostname_size) {
+ if (hostname) free(hostname);
+ hostname = trunc_gethostname(hostname_size, g_config->strings.ellipsis);
+ hostname_calcd_size = hostname_size;
+ }
clean_line(box_start, HEAD_ROW);
+
// put hostname
if (hostname_size)
- printf("\x1b[%dG\x1b[%sm%s\x1b[%sm", box_start.x + BOX_HMARGIN,
- g_config->colors.e_hostname, hostname, g_config->colors.fg);
+ printf("\x1b[%dG\x1b[%sm%s\x1b[%sm", box_start.x + 1 + BOX_HMARGIN,
+ g_config->colors.e_hostname, hostname ? hostname : "unknown",
+ g_config->colors.fg);
// put date
printf("\x1b[%dG\x1b[%sm%s\x1b[%sm",Also replaced |
|
Thanks for taking care of that. Didn't want to cause work for you. It's a neat solution. I would've gone for dynamically ellipsizing the string, but that would've ended up harder to read, I think. I use Regarding the alignment, perhaps it would make sense to right-align for short strings and right-align when it exceeds the column width? Strings that are just a little too long might still look slightly off, though. |
Feel free to use whichever then, the project has no standard for this rn.
Maybe, but seems like too much cases handling for what could just be what I'm used to. I think it's fine how it ended up, if someone dislikes it I could always add that behavior in future changes. |
Instead of restricting the length of the hostname field to the size of the column, the hostname is allowed to utilize all space that is not taken up by the time string.
|
wanna add the patch yourself or do I commit on your branch? everything LGTM |
d402639 to
aa134d4
Compare
|
Sounds reasonable. I took over your patch (thanks again). |
|
Thanks for the PR 🫡 Will see if I can release this in 1.2.4 any time soon. |
Started using lidm today, and I really like it! Just one minor thing that struck me as inconvenient: My hostname is not crazy long, but it still is longer than the <15 chars that the column allows for. Given there is a lot of space available, and a blank line separates it visually from the tabular input fields below, I thought it would be fine to let it extend.
This patch allows the hostname string to take as much horizontal space as is left by the time string. For separation, the same value is used as for the separation between columns.