Skip to content

Commit

Permalink
fix: improve output of mise tasks
Browse files Browse the repository at this point in the history
  • Loading branch information
jdx committed Dec 14, 2024
1 parent a09d4c2 commit f93dfcc
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 80 deletions.
13 changes: 9 additions & 4 deletions e2e/tasks/test_task_ls
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ cat <<'EOF' >mytasks/filetask2
EOF
chmod +x mytasks/filetask2

assert "mise task ls" "filetask2 ~/workdir/mytasks/filetask2
lint ~/workdir/mise.toml
test ~/workdir/tasks.toml
test-with-args ~/workdir/tasks.toml"
assert "mise task ls" "filetask2
lint
test
test-with-args"

assert "mise task -x ls" "filetask2 ./mytasks/filetask2
lint ./mise.toml
test ./tasks.toml
test-with-args ./tasks.toml"
10 changes: 5 additions & 5 deletions e2e/tasks/test_task_run_file
Original file line number Diff line number Diff line change
Expand Up @@ -66,8 +66,8 @@ console.log(`MYVAR: ${process.env.MYVAR}`);
EOF
chmod +x .mise/tasks/jstask
assert "mise run jstask" "MYVAR: 1"
assert "mise tasks" "configtask ~/workdir/.mise.toml
filetask ~/workdir/.mise/tasks/filetask
jstask This is a test build script ~/workdir/.mise/tasks/jstask
lint ~/workdir/.mise.toml
test ~/workdir/.mise.toml"
assert "mise tasks -x" "configtask ./.mise.toml
filetask ./.mise/tasks/filetask
jstask ./.mise/tasks/jstask This is a test build script
lint ./.mise.toml
test ./.mise.toml"
6 changes: 5 additions & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,11 @@ impl Cli {
Ok(cmd)
} else {
if let Some(task) = self.task {
if Config::get().tasks()?.contains_key(&task) {
if Config::get()
.tasks()?
.iter()
.any(|(_, t)| t.is_match(&task))
{
return Ok(Commands::Run(run::Run {
task,
args: self.task_args.unwrap_or_default(),
Expand Down
79 changes: 29 additions & 50 deletions src/cli/tasks/ls.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
use console::truncate_str;
use comfy_table::{Attribute, Cell, Row};
use eyre::Result;
use itertools::Itertools;
use tabled::Tabled;

use crate::config::Config;
use crate::file::display_path;
use crate::file::{display_path, display_rel_path};
use crate::task::Task;
use crate::ui::info::trim_line_end_whitespace;
use crate::ui::{style, table};
use crate::ui::table::MiseTable;

/// List available tasks to execute
/// These may be included from the config file or from the project's .mise/tasks directory
Expand All @@ -20,30 +18,30 @@ use crate::ui::{style, table};
#[clap(verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
pub struct TasksLs {
/// Do not print table header
#[clap(long, alias = "no-headers", verbatim_doc_comment)]
#[clap(long, alias = "no-headers", global = true, verbatim_doc_comment)]
pub no_header: bool,

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

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

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

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

/// Output in JSON format
#[clap(short = 'J', long, verbatim_doc_comment)]
#[clap(short = 'J', global = true, long, verbatim_doc_comment)]
pub json: bool,

#[clap(long, hide = true)]
#[clap(long, global = true, hide = true)]
pub usage: bool,
}

Expand Down Expand Up @@ -83,16 +81,18 @@ impl TasksLs {
}

fn display(&self, tasks: Vec<Task>) -> Result<()> {
let rows = tasks.iter().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]);
let mut table = MiseTable::new(
self.no_header,
if self.extended {
&["Name", "Aliases", "Source", "Description"]
} else {
&["Name", "Description"]
},
);
for task in tasks {
table.add_row(self.task_to_row(&task));
}
let table = format!("{table}");
miseprintln!("{}", trim_line_end_whitespace(&table));
Ok(())
table.print()
}

fn display_usage(&self, tasks: Vec<Task>) -> Result<()> {
Expand All @@ -117,7 +117,7 @@ impl TasksLs {
.filter(|t| self.hidden || !t.hide)
.map(|task| {
let mut inner = serde_json::Map::new();
inner.insert("name".to_string(), task.name.into());
inner.insert("name".to_string(), task.display_name().into());
if !task.aliases.is_empty() {
inner.insert("aliases".to_string(), task.aliases.join(", ").into());
}
Expand Down Expand Up @@ -149,39 +149,18 @@ impl TasksLs {
_ => cmp,
}
}
}

#[derive(Tabled)]
#[tabled(rename_all = "PascalCase")]
struct Row {
name: String,
alias: String,
description: String,
// command: String,
source: String,
}

impl From<&Task> for Row {
fn from(task: &Task) -> Self {
// 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),
fn task_to_row(&self, task: &Task) -> Row {
let mut row = vec![Cell::new(task.display_name()).add_attribute(Attribute::Bold)];
if self.extended {
row.push(Cell::new(task.aliases.join(", ")));
row.push(Cell::new(display_rel_path(&task.config_source)));
}
row.push(Cell::new(&task.description).add_attribute(Attribute::Dim));
row.into()
}
}

fn first_line(s: &str) -> &str {
s.lines().next().unwrap_or_default()
}

fn truncate(s: &str, len: usize) -> String {
first_line(&truncate_str(s, len, "…")).to_string()
}

// TODO: fill this out
static AFTER_LONG_HELP: &str = color_print::cstr!(
r#"<bold><underline>Examples:</underline></bold>
Expand Down
17 changes: 17 additions & 0 deletions src/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,23 @@ impl Task {
Ok(task)
}

/// prints the task name without an extension
pub fn display_name(&self) -> String {
self.name
.rsplitn(2, '.')
.last()
.unwrap_or_default()
.to_string()
}

pub fn is_match(&self, pat: &str) -> bool {
if self.name == pat || self.aliases.contains(&pat.to_string()) {
return true;
}
let pat = pat.rsplitn(2, '.').last().unwrap_or_default();
self.name == pat || self.aliases.contains(&pat.to_string())
}

pub fn task_dir() -> PathBuf {
let config = Config::get();
let cwd = dirs::CWD.clone().unwrap_or_default();
Expand Down
14 changes: 0 additions & 14 deletions src/ui/style.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use std::path::Path;

use crate::env::TERM_WIDTH;
use crate::file::display_path;
use console::{style, StyledObject};

Expand Down Expand Up @@ -64,18 +63,10 @@ pub fn nstyle<D>(val: D) -> StyledObject<D> {
style(val).for_stdout()
}

pub fn nblue<D>(val: D) -> StyledObject<D> {
nstyle(val).blue()
}

pub fn ncyan<D>(val: D) -> StyledObject<D> {
nstyle(val).cyan()
}

pub fn nbold<D>(val: D) -> StyledObject<D> {
nstyle(val).bold()
}

pub fn nunderline<D>(val: D) -> StyledObject<D> {
nstyle(val).underlined()
}
Expand All @@ -91,8 +82,3 @@ pub fn nred<D>(val: D) -> StyledObject<D> {
pub fn ndim<D>(val: D) -> StyledObject<D> {
nstyle(val).dim()
}

#[allow(unused)]
pub fn truncate_str(s: impl AsRef<str>) -> String {
console::truncate_str(s.as_ref(), *TERM_WIDTH - 14, "…").to_string()
}
6 changes: 0 additions & 6 deletions src/ui/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,6 @@ pub fn default_style(table: &mut Table, no_headers: bool) {
.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(Remove::column(Columns::single(idx)));
}
}

pub struct MiseTable {
table: comfy_table::Table,
truncate: bool,
Expand Down

0 comments on commit f93dfcc

Please sign in to comment.