Skip to content

Commit

Permalink
Align doc-comments in --list output (#273)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Nov 30, 2017
1 parent 5f8e1ea commit c5eeb89
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 2 deletions.
28 changes: 26 additions & 2 deletions src/run.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use common::*;

use std::{convert, ffi};
use std::{convert, ffi, cmp};
use clap::{App, Arg, ArgGroup, AppSettings};
use misc::maybe_s;
use configuration::DEFAULT_SHELL;
use unicode_width::UnicodeWidthStr;

macro_rules! die {
($($arg:tt)*) => {{
Expand Down Expand Up @@ -260,6 +261,26 @@ pub fn run() {
}

if matches.is_present("LIST") {
let mut line_widths: Map<&str, usize> = Map::new();

for (name, recipe) in &justfile.recipes {
if recipe.private {
continue;
}

let mut line_width = UnicodeWidthStr::width(*name);

for parameter in &recipe.parameters {
line_width += UnicodeWidthStr::width(format!(" {}", parameter).as_str());
}

if line_width <= 30 {
line_widths.insert(name, line_width);
}
}

let max_line_width = cmp::min(line_widths.values().cloned().max().unwrap_or(0), 30);

let doc_color = color.stdout().doc();
println!("Available recipes:");
for (name, recipe) in &justfile.recipes {
Expand All @@ -275,7 +296,10 @@ pub fn run() {
}
}
if let Some(doc) = recipe.doc {
print!(" {} {}", doc_color.paint("#"), doc_color.paint(doc));
print!(
" {:padding$}{} {}", "", doc_color.paint("#"), doc_color.paint(doc),
padding = max_line_width.saturating_sub(line_widths.get(name).cloned().unwrap_or(max_line_width))
);
}
println!();
}
Expand Down
51 changes: 51 additions & 0 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1039,6 +1039,57 @@ _private-recipe:
status: EXIT_SUCCESS,
}

integration_test! {
name: list_alignment,
justfile: r#"
# this does a thing
hello a b='B ' c='C':
echo {{a}} {{b}} {{c}}
# something else
a Z="\t z":
# this recipe will not appear
_private-recipe:
"#,
args: ("--list"),
stdout: r"Available recipes:
a Z='\t z' # something else
hello a b='B\t' c='C' # this does a thing
",
stderr: "",
status: EXIT_SUCCESS,
}

integration_test! {
name: list_alignment_long,
justfile: r#"
# this does a thing
hello a b='B ' c='C':
echo {{a}} {{b}} {{c}}
# this does another thing
x a b='B ' c='C':
echo {{a}} {{b}} {{c}}
# something else
this-recipe-is-very-very-very-important Z="\t z":
# this recipe will not appear
_private-recipe:
"#,
args: ("--list"),
stdout: r"Available recipes:
hello a b='B\t' c='C' # this does a thing
this-recipe-is-very-very-very-important Z='\t z' # something else
x a b='B\t' c='C' # this does another thing
",
stderr: "",
status: EXIT_SUCCESS,
}

integration_test! {
name: show_suggestion,
justfile: r#"
Expand Down

0 comments on commit c5eeb89

Please sign in to comment.