Skip to content

Commit 7198f09

Browse files
authored
feature: add option to format tooltip as a table (#13)
1 parent 06d3fa3 commit 7198f09

File tree

2 files changed

+44
-9
lines changed

2 files changed

+44
-9
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ This small program will give you fast updates with less network usage. After you
4848

4949
`--network-interval-seconds` - interval to run checkupdates with network usage.
5050

51+
`--tooltip-align-columns` - format tooltip as a table using given monospaced font.
52+
5153
### How to hide the module when there are no updates available
5254

5355
##### waybar config

src/main.rs

Lines changed: 42 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ fn display_help() {
1515
println!(" --interval-seconds <seconds> Set the interval between updates (default: 5)");
1616
println!(" --network-interval-seconds <seconds> Set the interval between network updates (default: 300)");
1717
println!(" --no-zero-output Don't print '0' when there are no updates available");
18+
println!(" --tooltip-align-columns <font>Format tooltip as a table using given font (default: monospace)");
1819
println!();
1920
}
2021

@@ -27,21 +28,28 @@ fn main() -> Result<(), Error> {
2728
let mut interval_seconds: u32 = 5;
2829
let mut network_interval_seconds: u32 = 300;
2930
let mut clean_output = false;
31+
let mut tooltip_align = false;
32+
let mut tooltip_font = "monospace";
3033
if args.len() > 1 {
3134
for (i, arg) in args.iter().enumerate() {
3235
if arg == "--help" {
3336
display_help();
3437
return Ok(());
3538
} else if arg == "--interval-seconds" && i + 1 < args.len() {
36-
interval_seconds = args[i + 1]
37-
.parse()
38-
.unwrap_or_else(|_| panic!("--interval-seconds must be greater than 0!"));
39+
interval_seconds = args[i + 1].parse().unwrap_or_else(|_| {
40+
panic!("--interval-seconds must be greater than 0!")
41+
});
3942
} else if arg == "--network-interval-seconds" && i + 1 < args.len() {
4043
network_interval_seconds = args[i + 1].parse().unwrap_or_else(|_| {
4144
panic!("--network-interval-seconds must be greater than 0!")
4245
});
4346
} else if arg == "--no-zero-output" {
4447
clean_output = true;
48+
} else if arg == "--tooltip-align-columns" {
49+
tooltip_align = true;
50+
if i + 1 < args.len() && args[i + 1][..1] != *"-" {
51+
tooltip_font = args[i + 1].as_str();
52+
}
4553
}
4654
}
4755
}
@@ -55,15 +63,40 @@ fn main() -> Result<(), Error> {
5563
sync_database();
5664
iter = 0;
5765
}
58-
let (updates, stdout) = get_updates();
66+
let (updates, mut stdout) = get_updates();
5967
if updates > 0 {
68+
if tooltip_align {
69+
let mut padding = [0; 4];
70+
stdout
71+
.split_whitespace()
72+
.enumerate()
73+
.for_each(|(index, word)| {
74+
padding[index % 4] = padding[index % 4].max(word.len())
75+
});
76+
77+
stdout = format!(
78+
"<span font-family='{}'>{}</span>",
79+
tooltip_font,
80+
stdout
81+
.split_whitespace()
82+
.enumerate()
83+
.map(|(index, word)| {
84+
word.to_string() + " ".repeat(padding[index % 4] - word.len()).as_str()
85+
})
86+
.collect::<Vec<String>>()
87+
.chunks(4)
88+
.map(|line| line.join(" "))
89+
.collect::<Vec<String>>()
90+
.join("\n")
91+
);
92+
}
6093
let tooltip = stdout.trim_end().replace("\"", "\\\"").replace("\n", "\\n");
6194
println!("{{\"text\":\"{}\",\"tooltip\":\"{}\",\"class\":\"has-updates\",\"alt\":\"has-updates\"}}", updates, tooltip);
6295
} else {
6396
println!("{{\"text\":{},\"tooltip\":\"System updated\",\"class\": \"updated\",\"alt\":\"updated\"}}", if clean_output {"\"\""} else {"\"0\""});
6497
}
6598
iter += 1;
66-
std::thread::sleep(sleep_duration);
99+
thread::sleep(sleep_duration);
67100
}
68101
}
69102

@@ -84,14 +117,14 @@ fn get_updates() -> (u16, String) {
84117
.args(["--nosync", "--nocolor"])
85118
.output()
86119
.expect("failed to execute process");
87-
return match output.status.code() {
120+
match output.status.code() {
88121
Some(_code) => {
89122
let stdout = String::from_utf8_lossy(&output.stdout).to_string();
90-
if stdout == "" {
123+
if stdout.is_empty() {
91124
return (0, "0".to_string());
92125
}
93-
return ((stdout.split(" -> ").count() as u16) - 1, stdout);
126+
((stdout.split(" -> ").count() as u16) - 1, stdout)
94127
}
95128
None => (0, "0".to_string()),
96-
};
129+
}
97130
}

0 commit comments

Comments
 (0)