I’m trying to write a status generator for I3, and for updating the volume indicator it requires that the program can handle SIGUSR1 requests to update the bar. I already run the generation function in a loop every 1 sec and just need to handle those requests. I’m using Tokio for the project since it can handle requests like that out of the box. My code (tell me if you need more context):
pub async fn run_bar(&mut self) {
let interval = Duration::from_millis(self.speed);
let og_pw_num = self.pw_color_num;
let mut func = || {
let mut statusline: String = String::new();
let mut pw_color_num: usize = 0;
pw_color_num = og_pw_num;
let fn_list_size = self.function_list.len().clone();
for (i, mut x) in self.function_list.iter_mut().enumerate() {
let og_pw_num_2 = pw_color_num;
if (pw_color_num == self.pw_colors.len()) {
pw_color_num = og_pw_num;
}
if (self.is_powerline) {
if (i == 0) {
//statusline.push('');
statusline.push_str(&format!(
"<span foreground='{}'></span>",
self.pw_colors[pw_color_num]
));
} else if pw_color_num != 0 {
statusline.push_str(&format!(
"<span background='{}' foreground='{}'></span>",
self.pw_colors[pw_color_num],
self.pw_colors[pw_color_num - 1]
));
} else {
statusline.push_str(&format!(
"<span background='{}' foreground='{}'></span>",
self.pw_colors[pw_color_num],
self.pw_colors[og_pw_num_2 - 1]
));
}
// statusline.push_str(&format!("<span background='{}' foreground='{}'>", self.bg, self.fg));
statusline.push_str(&format!(
"<span background='{}' foreground='{}'> ",
self.pw_colors[pw_color_num], self.fg
));
}
// always ran because they are the base command
statusline.push_str(x.prefix().as_str());
statusline.push_str(x.plug_out().as_str());
if (self.is_powerline) {
statusline.push_str(" </span>");
if (i == fn_list_size - 1 && self.end_pw_line) {
statusline.push_str(&format!(
"<span foreground='{}' background='{}'></span>",
self.pw_colors[pw_color_num], self.bg
))
}
pw_color_num += 1;
}
if (i != fn_list_size - 1 && !self.is_powerline) {
statusline.push_str(self.seperator.as_str())
}
}
println!("{}", statusline);
};
loop {
let mut next_time = Instant::now() + interval;
func();
sleep(next_time - Instant::now());
// next_time += interval;
}
// let mut sigint = signal(SignalKind::user_defined1()).unwrap();
// let sigusr1 = async { loop{ match sigint.recv().await {
// Some(()) => func(),
// None => eprintln!("Stream terminated before receiving SIGINT signal"),
// }}};
// let main = async {loop {
// func();
//
// }};
//tokio::join!(main, sigusr1);
}
I tried to tokio::join! the sigusr1 waiting event and the loop but pretty obviously it wouldn’t let me run since it tries to both take a mutable variable and use it.
tux7k is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.