Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(task): extend mise tasks output #1845

Merged
merged 4 commits into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 53 additions & 4 deletions src/cli/tasks/ls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use console::truncate_str;
use eyre::Result;
use itertools::Itertools;
use tabled::Tabled;

use crate::config::{Config, Settings};
Expand All @@ -21,9 +22,35 @@ pub struct TasksLs {
#[clap(long, alias = "no-headers", verbatim_doc_comment)]
pub no_header: bool,

/// Show all columns
#[clap(short = 'x', long, verbatim_doc_comment)]
pub extended: bool,

/// Show hidden tasks
#[clap(long, verbatim_doc_comment)]
pub hidden: bool,

/// Sort by column. Default is name.
#[clap(long, value_name = "COLUMN", verbatim_doc_comment)]
pub sort: Option<SortColumn>,

/// Sort order. Default is asc.
#[clap(long, verbatim_doc_comment)]
pub sort_order: Option<SortOrder>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum SortColumn {
Name,
Alias,
Description,
Source,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, clap::ValueEnum)]
pub enum SortOrder {
Asc,
Desc,
}

impl TasksLs {
Expand All @@ -35,20 +62,41 @@ impl TasksLs {
.tasks()?
.values()
.filter(|t| self.hidden || !t.hide)
.sorted_by(|a, b| self.sort(a, b))
.map(|t| t.into())
.collect::<Vec<Row>>();

let mut table = tabled::Table::new(rows);
table::default_style(&mut table, self.no_header);
// hide columns alias
if !self.extended {
table::disable_columns(&mut table, vec![1]);
}
miseprintln!("{table}");

Ok(())
}

fn sort(&self, a: &Task, b: &Task) -> std::cmp::Ordering {
let cmp = match self.sort.unwrap_or(SortColumn::Name) {
SortColumn::Alias => a.aliases.join(", ").cmp(&b.aliases.join(", ")),
SortColumn::Description => a.description.cmp(&b.description),
SortColumn::Source => a.config_source.cmp(&b.config_source),
_ => a.name.cmp(&b.name),
};

match self.sort_order.unwrap_or(SortOrder::Asc) {
SortOrder::Desc => cmp.reverse(),
_ => cmp,
}
}
}

#[derive(Tabled)]
#[tabled(rename_all = "PascalCase")]
struct Row {
name: String,
alias: String,
description: String,
// command: String,
source: String,
Expand All @@ -59,6 +107,7 @@ impl From<&Task> for Row {
// let cmd = tasks.command_string().unwrap_or_default();
Self {
name: style::nbold(&task.name).bright().to_string(),
alias: style::ndim(&task.aliases.join(", ")).dim().to_string(),
description: style::nblue(truncate(&task.description, 40)).to_string(),
// command: style::ndim(truncate(&cmd, 20)).dim().to_string(),
source: display_path(&task.config_source),
Expand Down Expand Up @@ -87,10 +136,10 @@ mod tests {
#[test]
fn test_task_ls() {
assert_cli_snapshot!("t", "--no-headers", @r###"
configtask ~/config/config.toml
filetask This is a test build script ~/cwd/.mise/tasks/filetask
lint ~/config/config.toml
test ~/config/config.toml
configtask ~/config/config.toml
filetask This is a test build script ~/cwd/.mise/tasks/filetask
lint ~/config/config.toml
test ~/config/config.toml
"###);
}
}
6 changes: 6 additions & 0 deletions src/ui/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,3 +37,9 @@ pub fn default_style(table: &mut Table, no_headers: bool) {
.with(Modify::new(Columns::first()).with(Padding::new(0, 1, 0, 0)))
.with(Modify::new(Columns::last()).with(Padding::zero()));
}

pub fn disable_columns(table: &mut Table, col_idxs: Vec<usize>) {
for idx in col_idxs {
table.with(Disable::column(Columns::single(idx)));
}
}
Loading