Skip to content

Commit

Permalink
chore: added comfy-table
Browse files Browse the repository at this point in the history
I think I prefer this table library, testing it out on 1 command for now
  • Loading branch information
jdx committed Dec 14, 2024
1 parent 36e7631 commit 2fd8819
Show file tree
Hide file tree
Showing 15 changed files with 208 additions and 143 deletions.
35 changes: 35 additions & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ clap = { version = "4", features = ["env", "derive", "string"] }
clap_mangen = { version = "0.2", optional = true }
color-eyre = "0.6"
color-print = "0.3"
comfy-table = "7.1.3"
confique = { version = "0.3", default-features = false }
console = "0.15"
contracts = "0.6"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ Install mise (other methods [here](https://mise.jdx.dev/getting-started.html)):
```sh-session
$ curl https://mise.run | sh
$ ~/.local/bin/mise --version
2024.12.9 macos-arm64 (a1b2d3e 2024-12-14)
2024.12.8 macos-arm64 (a1b2d3e 2024-12-12)
```

or install a specific a version:
Expand Down
6 changes: 5 additions & 1 deletion docs/cli/config/ls.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
# `mise config ls`

- **Usage**: `mise config ls [--no-header] [-J --json]`
- **Aliases**: `list`
- **Source code**: [`src/cli/config/ls.rs`](https://github.com/jdx/mise/blob/main/src/cli/config/ls.rs)

List config files currently in use
Expand All @@ -17,4 +18,7 @@ Output in JSON format

Examples:

mise config ls
$ mise config ls
Path Tools
~/.config/mise/config.toml pitchfork
~/src/mise/mise.toml actionlint, bun, cargo-binstall, cargo:cargo-edit, cargo:cargo-insta
8 changes: 4 additions & 4 deletions e2e/cli/test_ls
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@

assert "mise use -g dummy@ref:master"
assert "mise use tiny@3"
assert_contains "mise ls" "dummy ref:master ~/.config/mise/config.toml ref:master"
assert_contains "mise ls tiny" "tiny 3.1.0 ~/workdir/mise.toml 3"
assert_contains "mise ls" "dummy ref:master ~/.config/mise/config.toml ref:master"
assert_contains "mise ls tiny" "tiny 3.1.0 ~/workdir/mise.toml 3"
assert "mise i [email protected]"
assert_contains "mise ls" "tiny 2.0.0"
assert_not_contains "mise ls -c" "tiny 2.0.0"
Expand All @@ -24,8 +24,8 @@ assert "cat json | jq -r '.tiny[1].version'" "3.1.0"
assert "cat json | jq -r '.tiny[1].install_path'" "$MISE_DATA_DIR/installs/tiny/3.1.0"

assert "mise rm [email protected]"
assert_contains "mise ls tiny" "tiny 3.1.0 (missing) ~/workdir/mise.toml 3"
assert_contains "mise ls --missing tiny" "tiny 3.1.0 (missing) ~/workdir/mise.toml 3"
assert_contains "mise ls tiny" "tiny 3.1.0 (missing) ~/workdir/mise.toml 3"
assert_contains "mise ls --missing tiny" "tiny 3.1.0 (missing) ~/workdir/mise.toml 3"
assert_not_contains "mise ls --missing tiny" "2.0.0"

assert_fail "mise ls missing-tool" "missing-tool not found in mise tool registry"
Expand Down
8 changes: 4 additions & 4 deletions e2e/cli/test_upgrade
Original file line number Diff line number Diff line change
Expand Up @@ -29,21 +29,21 @@ rm -f .tool-versions
echo "tools.dummy = 'prefix:1'" >mise.toml
mise uninstall dummy --all
mkdir -p "$MISE_DATA_DIR/installs/dummy/1.0.0"
assert "mise ls dummy --outdated" "dummy 1.0.0 (outdated) ~/workdir/mise.toml prefix:1"
assert "mise ls dummy --outdated" "dummy 1.0.0 (outdated) ~/workdir/mise.toml prefix:1"
assert "mise up -n" "Would uninstall [email protected]
Would install [email protected]"
mise up
assert "mise ls dummy" "dummy 1.1.0 ~/workdir/mise.toml prefix:1"
assert "mise ls dummy" "dummy 1.1.0 ~/workdir/mise.toml prefix:1"
assert "mise up dummy -n --bump" "Would uninstall [email protected]
Would install [email protected]
Would bump dummy@prefix:2 in ~/workdir/mise.toml"
mise up dummy --bump
assert "mise ls dummy" "dummy 2.0.0 ~/workdir/mise.toml prefix:2"
assert "mise ls dummy" "dummy 2.0.0 ~/workdir/mise.toml prefix:2"
echo "tools.dummy = 'prefix:1'" >mise.toml
mise uninstall dummy --all
mise i [email protected]
assert "mise up dummy -n --bump" "Would uninstall [email protected]
Would install [email protected]
Would bump dummy@prefix:2 in ~/workdir/mise.toml"
mise up dummy --bump
assert "mise ls dummy" "dummy 2.0.0 ~/workdir/mise.toml prefix:2"
assert "mise ls dummy" "dummy 2.0.0 ~/workdir/mise.toml prefix:2"
4 changes: 2 additions & 2 deletions e2e/cli/test_use
Original file line number Diff line number Diff line change
Expand Up @@ -67,11 +67,11 @@ rm -f ~/.config/mise/config.toml

mise uninstall dummy --all
mise use dummy@system
assert "mise ls dummy" "dummy system ~/workdir/mise.toml system"
assert "mise ls dummy" "dummy system ~/workdir/mise.toml system"

mkdir -p ~/workdir/mydummy
mise use "dummy@path:~/workdir/mydummy"
assert_contains "mise ls dummy" "dummy path:~/workdir/mydummy ~/workdir/mise.toml path:~/workdir/mydummy"
assert_contains "mise ls dummy" "dummy path:~/workdir/mydummy ~/workdir/mise.toml path:~/workdir/mydummy"

cd "$HOME" || exit 1
assert_contains "mise use dummy@system" "mise ~/.config/mise/config.toml tools: dummy@system"
10 changes: 5 additions & 5 deletions e2e/config/test_config_env
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ echo "tools.dummy = '1'" >mise.toml
echo "tools.dummy = '2'" >mise.test.toml
echo "tools.dummy = '3'" >mise.ci.toml

assert "mise ls dummy" "dummy 1.1.0 (missing) ~/workdir/mise.toml 1"
MISE_ENV=test assert "mise ls dummy" "dummy 2.0.0 (missing) ~/workdir/mise.test.toml 2"
MISE_ENV=ci assert "mise ls dummy" "dummy 3 (missing) ~/workdir/mise.ci.toml 3"
MISE_ENV=ci,test assert "mise ls dummy" "dummy 2.0.0 (missing) ~/workdir/mise.test.toml 2"
MISE_ENV=test,ci assert "mise ls dummy" "dummy 3 (missing) ~/workdir/mise.ci.toml 3"
assert "mise ls dummy" "dummy 1.1.0 (missing) ~/workdir/mise.toml 1"
MISE_ENV=test assert "mise ls dummy" "dummy 2.0.0 (missing) ~/workdir/mise.test.toml 2"
MISE_ENV=ci assert "mise ls dummy" "dummy 3 (missing) ~/workdir/mise.ci.toml 3"
MISE_ENV=ci,test assert "mise ls dummy" "dummy 2.0.0 (missing) ~/workdir/mise.test.toml 2"
MISE_ENV=test,ci assert "mise ls dummy" "dummy 3 (missing) ~/workdir/mise.ci.toml 3"
24 changes: 12 additions & 12 deletions e2e/config/test_config_ignore
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@ EOF
cd subdir || exit 1

assert "mise trust --all"
assert "mise cfg" "~/workdir/mise.toml dummy
~/workdir/subdir/mise.toml dummy"
assert "mise ls dummy" "dummy 2.0.0 (missing) ~/workdir/subdir/mise.toml 2.0.0"
assert "mise cfg" "~/workdir/mise.toml dummy
~/workdir/subdir/mise.toml dummy"
assert "mise ls dummy" "dummy 2.0.0 (missing) ~/workdir/subdir/mise.toml 2.0.0"
assert "mise settings get go_download_mirror" "subdir/mise.toml"
assert "mise trust --ignore"
assert "mise cfg" "~/workdir/mise.toml dummy"
assert "mise cfg" "~/workdir/mise.toml dummy"
assert "mise settings get go_download_mirror" "mise.toml"
assert "mise ls dummy" "dummy 1.0.0 (missing) ~/workdir/mise.toml 1.0.0"
assert "mise ls dummy" "dummy 1.0.0 (missing) ~/workdir/mise.toml 1.0.0"
assert "mise trust ."
assert "mise cfg" "~/workdir/mise.toml dummy
~/workdir/subdir/mise.toml dummy"
assert "MISE_IGNORED_CONFIG_PATHS=~/workdir/subdir mise cfg" "~/workdir/mise.toml dummy"
assert "mise cfg" "~/workdir/mise.toml dummy
~/workdir/subdir/mise.toml dummy"
assert "MISE_IGNORED_CONFIG_PATHS=~/workdir/subdir mise cfg" "~/workdir/mise.toml dummy"
assert "MISE_IGNORED_CONFIG_PATHS=~ mise cfg" ""
assert "mise trust --ignore mise.toml"
assert "mise trust --ignore .."
assert "mise cfg" ""
assert "mise trust --all"
assert "mise cfg" "~/workdir/mise.toml dummy
~/workdir/subdir/mise.toml dummy"
assert "mise cfg" "~/workdir/mise.toml dummy
~/workdir/subdir/mise.toml dummy"

rm -rf "$MISE_STATE_DIR"
export MISE_PARANOID=1
assert "mise trust --all"
assert "mise cfg" "~/workdir/mise.toml dummy
~/workdir/subdir/mise.toml dummy"
assert "mise cfg" "~/workdir/mise.toml dummy
~/workdir/subdir/mise.toml dummy"
65 changes: 25 additions & 40 deletions src/cli/config/ls.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
use crate::config::config_file::ConfigFile;
use crate::config::Config;
use crate::file::display_path;
use crate::ui::table;
use console::style;
use crate::ui::table::MiseTable;
use comfy_table::{Attribute, Cell};
use eyre::Result;
use itertools::Itertools;
use tabled::settings::object::Columns;
use tabled::settings::{Modify, Width};
use tabled::Tabled;

/// List config files currently in use
#[derive(Debug, clap::Args)]
#[clap(verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
#[clap(visible_alias="list", verbatim_doc_comment, after_long_help = AFTER_LONG_HELP)]
pub struct ConfigLs {
/// Do not print table header
#[clap(long, alias = "no-headers", verbatim_doc_comment)]
Expand All @@ -33,17 +30,27 @@ impl ConfigLs {
}

fn display(&self) -> Result<()> {
let rows = Config::get()
let config = Config::get();
let configs = config
.config_files
.values()
.rev()
.map(|cf| cf.as_ref().into())
.collect::<Vec<Row>>();
let mut table = tabled::Table::new(rows);
table::default_style(&mut table, self.no_header);
table.with(Modify::new(Columns::last()).with(Width::truncate(40).suffix("…")));
miseprintln!("{table}");
Ok(())
.map(|cf| cf.as_ref())
.collect_vec();
let mut table = MiseTable::new(self.no_header, &["Path", "Tools"]);
for cfg in configs {
let ts = cfg.to_tool_request_set().unwrap();
let tools = ts.list_tools().into_iter().join(", ");
let tools = if tools.is_empty() {
Cell::new("(none)")
.add_attribute(Attribute::Italic)
.add_attribute(Attribute::Dim)
} else {
Cell::new(tools).add_attribute(Attribute::Dim)
};
table.add_row(vec![Cell::new(display_path(cfg.get_path())), tools]);
}
table.truncate(true).print()
}

fn display_json(&self) -> Result<()> {
Expand All @@ -60,7 +67,7 @@ impl ConfigLs {
let plugins = c
.to_tool_request_set()
.unwrap()
.list_plugins()
.list_tools()
.into_iter()
.map(|s| serde_json::Value::String(s.to_string()))
.collect::<Vec<serde_json::Value>>();
Expand All @@ -74,34 +81,12 @@ impl ConfigLs {
}
}

fn format_tools_cell(s: String) -> String {
match s.is_empty() {
true => style("(none)").italic().dim().to_string(),
false => style(s).dim().to_string(),
}
}

#[derive(Tabled)]
#[tabled(rename_all = "PascalCase")]
struct Row {
path: String,
tools: String,
}

impl From<&dyn ConfigFile> for Row {
fn from(cf: &dyn ConfigFile) -> Self {
let path = display_path(cf.get_path());
let ts = cf.to_tool_request_set().unwrap();
let plugins = ts.list_plugins().into_iter().join(", ");
let tools = format_tools_cell(plugins);
Self { path, tools }
}
}

// TODO: fill this out
static AFTER_LONG_HELP: &str = color_print::cstr!(
r#"<bold><underline>Examples:</underline></bold>
$ <bold>mise config ls</bold>
Path Tools
~/.config/mise/config.toml pitchfork
~/src/mise/mise.toml actionlint, bun, cargo-binstall, cargo:cargo-edit, cargo:cargo-insta
"#
);
Loading

0 comments on commit 2fd8819

Please sign in to comment.