i3status-rust icon indicating copy to clipboard operation
i3status-rust copied to clipboard

block custom format

Open ayoubelmhamdi opened this issue 2 years ago • 7 comments

[[block]]
block = "custom"
command = "get_bandwith"
interval = 1
  // ...
  unsigned long diff_packet = update_tx_packets(tx_packets) / 1024;
  char s[5];
  snprintf(s, sizeof(s), "%4lu", diff_packet);
  printf("%4s kb\n", s);

I am currently padding my numbers with blank spaces when they are less than 4 characters long. However, the output appears to be trimmed by i3status-rs. I would prefer to avoid having the blocks in my status bar move around when the status is updated.

ayoubelmhamdi avatar Feb 28 '23 22:02 ayoubelmhamdi

https://github.com/greshake/i3status-rust/blob/master/src/blocks/custom.rs#L242

True, we do trim the normal output. As a workaround, you can use JSON mode though:

[[block]]
block = "custom"
json = true
command = "get_bandwith"
interval = 1
  // ...
  printf("{\"text\":\"%4s kb\"}\n", s);

Since this trimming was like this for a long time i don't know if it makes sense to change it now since it's basically expected behavior, but @MaxVerevkin might have some better insight. Maybe it's only done to get rid of newlines?

GladOSkar avatar Feb 28 '23 23:02 GladOSkar

think's for your help. I think the better way to handle trimming is to make a custom block support some format like:

UPDATE

[[block]]
block = "custom"
command = "get_bandwith"
format = "{custom:4}"

that's easy to understand and more expected

ayoubelmhamdi avatar Mar 01 '23 00:03 ayoubelmhamdi

That's also not a bad idea imo, maybe awk-style with $1,$2/$@ for the individual words/whole line of the output?

GladOSkar avatar Mar 01 '23 01:03 GladOSkar

Maybe it's only done to get rid of newlines?

Probably. And it's been there since at least v0.10 :)

Also how about extending the json protocol like this?

{
    "vals": [
        "bandwith": 1234
    ]
}
[[block]]
block = "custom"
command = "get_bandwith"
format = " $bandwith.eng(w:4) "

MaxVerevkin avatar Mar 01 '23 07:03 MaxVerevkin

That looks neat but doesn't really solve the issue at hand since @ayoubelmhamdi is looking for a solution without using JSON i think? Both would be nice though

GladOSkar avatar Mar 01 '23 10:03 GladOSkar

I find it relatively straightforward to manipulate the output utilizing the format field exclusively, while handling JSON is not particularly difficult.

ayoubelmhamdi avatar Mar 01 '23 22:03 ayoubelmhamdi

I had the same issue and I already use JSON.

I was going to say that the spaces are still trimmed, but I looked more closely and it's just that the spaces width is smaller than other characters. Even if I use a pure Mono font patched with Nerd-fonts in mono.

I would just like to leave here that you should use U+2002, a '1 en' width space for padding, like seen here.

It works nice with the json custom block.

Edit: U+2007 would be even better it should be the width of a digit.

tkapias avatar Apr 18 '23 11:04 tkapias