Skip to content

don't truncate long hostnames if there is space#87

Merged
javalsai merged 1 commit into
javalsai:masterfrom
dariuskl:long-hostnames
Nov 16, 2025
Merged

don't truncate long hostnames if there is space#87
javalsai merged 1 commit into
javalsai:masterfrom
dariuskl:long-hostnames

Conversation

@dariuskl

Copy link
Copy Markdown
Contributor

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.

@javalsai javalsai added the enhancement New feature or request label Nov 16, 2025
@javalsai javalsai self-assigned this Nov 16, 2025
@javalsai

Copy link
Copy Markdown
Owner

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.

@javalsai

Copy link
Copy Markdown
Owner

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.

@dariuskl

Copy link
Copy Markdown
Contributor Author

Ah, right. Will fix that in a bit.

@javalsai

Copy link
Copy Markdown
Owner

Don't worry, I'm working on it and I think I almost have it, easy tweak.

@javalsai

Copy link
Copy Markdown
Owner

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 ptrdiff_t with ssize_t just because I'm more familiar with it, first time seeing that, if you have specific reason to prefer ptrdiff_t please say.

@javalsai

Copy link
Copy Markdown
Owner
image

There's still something about the left alignment that doesn't quite click with me though...

@dariuskl

Copy link
Copy Markdown
Contributor Author

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 ptrdiff_t as a signed size_t because that's what it is essentially. Pretty much the same as ssize_t except that the latter is not standard C.

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.

@javalsai

Copy link
Copy Markdown
Owner

I use ptrdiff_t as a signed size_t because that's what it is essentially. Pretty much the same as ssize_t except that the latter is not standard C.

Feel free to use whichever then, the project has no standard for this rn.

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.

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.
@javalsai

Copy link
Copy Markdown
Owner

wanna add the patch yourself or do I commit on your branch? everything LGTM

@dariuskl

Copy link
Copy Markdown
Contributor Author

Sounds reasonable. I took over your patch (thanks again).

@javalsai javalsai merged commit feeba5c into javalsai:master Nov 16, 2025
10 checks passed
@javalsai

Copy link
Copy Markdown
Owner

Thanks for the PR 🫡

Will see if I can release this in 1.2.4 any time soon.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants