Skip to content

Commit

Permalink
Merge pull request #367 from Nukesor/filter-status
Browse files Browse the repository at this point in the history
Filter status
  • Loading branch information
Nukesor authored Oct 1, 2022
2 parents b0e6c35 + a33e171 commit a322493
Show file tree
Hide file tree
Showing 40 changed files with 1,722 additions and 288 deletions.
26 changes: 14 additions & 12 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ crossbeam-channel = "0.5"
crossterm = "0.25"
ctrlc = { version = "3", features = ["termination"] }
handlebars = "4"
pest = "2"
pest_derive = "2"
shell-escape = "0.1"
simplelog = { version = "0.12", default-features = false }
tempfile = "3"
Expand Down
4 changes: 4 additions & 0 deletions client/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ pub enum SubCommand {

/// Display the current status of all tasks.
Status {
/// Users can specify a custom query to filter for specific values, order by a column
/// or limit the amount of tasks listed.
query: Vec<String>,

/// Print the current state as json to stdout.
/// This does not include the output of tasks.
/// Use `log -j` if you want everything.
Expand Down
15 changes: 8 additions & 7 deletions client/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ impl Client {
let subcommand = opt.cmd.unwrap_or(SubCommand::Status {
json: false,
group: None,
query: Vec::new(),
});

Ok(Client {
Expand Down Expand Up @@ -194,7 +195,7 @@ impl Client {
label,
} => {
let message = edit(&mut self.stream, *task_id, *command, *path, *label).await?;
self.handle_response(message);
self.handle_response(message)?;
Ok(true)
}
SubCommand::Wait {
Expand Down Expand Up @@ -292,7 +293,7 @@ impl Client {
let mut response = receive_message(&mut self.stream).await?;

// Check if we can receive the response from the daemon
while self.handle_response(response) {
while self.handle_response(response)? {
response = receive_message(&mut self.stream).await?;
}

Expand All @@ -304,7 +305,7 @@ impl Client {
///
/// If this function returns `Ok(true)`, the parent function will continue to receive
/// and handle messages from the daemon. Otherwise the client will simply exit.
fn handle_response(&self, message: Message) -> bool {
fn handle_response(&self, message: Message) -> Result<bool> {
match message {
Message::Success(text) => print_success(&self.style, &text),
Message::Failure(text) => {
Expand All @@ -313,7 +314,7 @@ impl Client {
}
Message::StatusResponse(state) => {
let tasks = state.tasks.iter().map(|(_, task)| task.clone()).collect();
print_state(*state, tasks, &self.subcommand, &self.style, &self.settings)
print_state(*state, tasks, &self.subcommand, &self.style, &self.settings)?;
}
Message::LogResponse(task_logs) => {
print_logs(task_logs, &self.subcommand, &self.style, &self.settings)
Expand All @@ -322,13 +323,13 @@ impl Client {
Message::Stream(text) => {
print!("{}", text);
io::stdout().flush().unwrap();
return true;
return Ok(true);
}
Message::Close => return false,
Message::Close => return Ok(false),
_ => error!("Received unhandled response message"),
};

false
Ok(false)
}

/// Prints a warning and prompt for given action and tasks.
Expand Down
2 changes: 1 addition & 1 deletion client/commands/format_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ pub async fn format_state(
.await
.context("Failed to get the current state from daemon")?;

print_state(state, tasks, command, style, settings);
print_state(state, tasks, command, style, settings)?;

Ok(())
}
73 changes: 49 additions & 24 deletions client/display/helper.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,8 @@
use std::collections::BTreeMap;

use pueue_lib::task::{Task, TaskStatus};
use chrono::Local;

/// This is a helper function for working with tables when calling `pueue status`.
///
/// By default, several columns aren't shown until there's at least one task with relevant data.
/// This function determines whether any of those columns should be shown.
pub fn has_special_columns(tasks: &[Task]) -> (bool, bool, bool) {
// Check whether there are any delayed tasks.
let has_delayed_tasks = tasks.iter().any(|task| {
matches!(
task.status,
TaskStatus::Stashed {
enqueue_at: Some(_)
}
)
});

// Check whether there are any tasks with dependencies.
let has_dependencies = tasks.iter().any(|task| !task.dependencies.is_empty());

// Check whether there are any tasks a label.
let has_labels = tasks.iter().any(|task| task.label.is_some());

(has_delayed_tasks, has_dependencies, has_labels)
}
use pueue_lib::{settings::Settings, task::Task};

/// Sort given tasks by their groups.
/// This is needed to print a table for each group.
Expand All @@ -40,3 +18,50 @@ pub fn sort_tasks_by_group(tasks: Vec<Task>) -> BTreeMap<String, Vec<Task>> {

sorted_task_groups
}

/// Returns the formatted `start` and `end` text for a given task.
///
/// 1. If the start || end is today, skip the date.
/// 2. Otherwise show the date in both.
///
/// If the task doesn't have a start and/or end yet, an empty string will be returned
/// for the respective field.
pub fn formatted_start_end(task: &Task, settings: &Settings) -> (String, String) {
// Get the start time.
// If the task didn't start yet, just return two empty strings.
let start = match task.start {
Some(start) => start,
None => return ("".into(), "".into()),
};

// If the task started today, just show the time.
// Otherwise show the full date and time.
let started_today = start >= Local::today().and_hms(0, 0, 0);
let formatted_start = if started_today {
start
.format(&settings.client.status_time_format)
.to_string()
} else {
start
.format(&settings.client.status_datetime_format)
.to_string()
};

// Get finish time, if already set. Otherwise only return the formatted start.
let end = match task.end {
Some(end) => end,
None => return (formatted_start, "".into()),
};

// If the task ended today we only show the time.
// In all other circumstances, we show the full date.
let finished_today = end >= Local::today().and_hms(0, 0, 0);
let formatted_end = if finished_today {
end.format(&settings.client.status_time_format).to_string()
} else {
end.format(&settings.client.status_datetime_format)
.to_string()
};

(formatted_start, formatted_end)
}
1 change: 1 addition & 0 deletions client/display/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ pub mod helper;
mod log;
mod state;
pub mod style;
pub mod table_builder;

use crossterm::style::Color;

Expand Down
Loading

0 comments on commit a322493

Please sign in to comment.