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 a690ed8 commit e09947a
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 64 deletions.
2 changes: 1 addition & 1 deletion src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ 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
65 changes: 22 additions & 43 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 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
13 changes: 13 additions & 0 deletions src/task/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,19 @@ impl Task {
task.render(config_root)?;
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();
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 e09947a

Please sign in to comment.