Move thread to start_background_thread function

This commit is contained in:
Dheepak Krishnamurthy 2020-08-04 05:19:06 -06:00
parent a7b4f424e3
commit 953ec46e74

View file

@ -126,41 +126,43 @@ impl TTApp {
colors: TColor::default(), colors: TColor::default(),
}; };
app.update(); app.update();
let handle = { app.start_background_thread();
let tasks = app.tasks.clone();
let filter = app.filter.clone();
thread::spawn(move || loop {
let mut task = Command::new("task");
task.arg("rc.json.array=on");
task.arg("export");
match shlex::split(&filter) {
Some(cmd) => {
for s in cmd {
task.arg(&s);
}
}
None => {
task.arg("");
}
}
let output = task
.output()
.expect("Unable to run `task export`. Check documentation for more information.");
let data = String::from_utf8(output.stdout).unwrap();
let imported = import(data.as_bytes());
if let Ok(i) = imported {
*(tasks.lock().unwrap()) = i;
tasks.lock().unwrap().sort_by(cmp);
}
thread::sleep(Duration::from_millis(250));
})
};
app app
} }
pub fn start_background_thread(&self) {
let tasks = self.tasks.clone();
let filter = self.filter.clone();
thread::spawn(move || loop {
let mut task = Command::new("task");
task.arg("rc.json.array=on");
task.arg("export");
match shlex::split(&filter) {
Some(cmd) => {
for s in cmd {
task.arg(&s);
}
}
None => {
task.arg("");
}
}
let output = task
.output()
.expect("Unable to run `task export`. Check documentation for more information.");
let data = String::from_utf8(output.stdout).unwrap();
let imported = import(data.as_bytes());
if let Ok(i) = imported {
*(tasks.lock().unwrap()) = i;
tasks.lock().unwrap().sort_by(cmp);
}
thread::sleep(Duration::from_millis(250));
});
}
pub fn draw(&mut self, f: &mut Frame<impl Backend>) { pub fn draw(&mut self, f: &mut Frame<impl Backend>) {
let tasks_is_empty = self.tasks.lock().unwrap().is_empty(); let tasks_is_empty = self.tasks.lock().unwrap().is_empty();
let tasks_len = self.tasks.lock().unwrap().len(); let tasks_len = self.tasks.lock().unwrap().len();