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

Add "--list-languages" command line argument #79

Merged
merged 15 commits into from
May 8, 2018
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
51 changes: 51 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ pub struct Options<'a> {
pub language: Option<&'a str>,
pub colored_output: bool,
pub paging: bool,
pub term_width: usize,
}

enum OutputType {
Expand Down Expand Up @@ -453,6 +454,11 @@ fn run() -> Result<()> {
.default_value("auto")
.help("When to use the pager"),
)
.arg(
Arg::with_name("list-languages")
.long("list-languages")
.help("Displays supported languages"),
)
.subcommand(
SubCommand::with_name("cache")
.about("Modify the syntax-definition and theme cache")
Expand Down Expand Up @@ -547,6 +553,7 @@ fn run() -> Result<()> {
interactive_terminal
},
},
term_width: console::Term::stdout().size().1 as usize,
};

let assets =
Expand All @@ -559,6 +566,50 @@ fn run() -> Result<()> {
)
})?;

if app_matches.is_present("list-languages") {
let languages = assets.syntax_set.syntaxes();

let longest = languages
.iter()
.filter(|s| !s.hidden && s.file_extensions.len() > 0)
.map(|s| s.name.len())
.max()
.unwrap_or(32); // Fallback width if they have no language definitions.

let separator = " ";
for lang in languages {
if lang.hidden || lang.file_extensions.len() == 0 {
continue;
}
print!("{:width$}{}", lang.name, separator, width = longest);

// Line-wrapping for the possible file extension overflow.
let desired_width = options.term_width - longest - separator.len();
// Number of characters on this line so far, wrap before `desired_width`
let mut num_chars = 0;

let comma_separator = ", ";
let mut extension = lang.file_extensions.iter().peekable();
while let Some(word) = extension.next() {
// If we can't fit this word in, then create a line break and align it in.
let new_chars = word.len() + comma_separator.len();
if num_chars + new_chars >= desired_width {
num_chars = 0;
print!("\n{:width$}{}", "", separator, width = longest);
}

num_chars += new_chars;
print!("{}", Green.paint(word as &str));
if extension.peek().is_some() {
print!("{}", comma_separator);
}
}
println!();
}

return Ok(());
}

let mut output_type = get_output_type(options.paging);
let handle = output_type.handle()?;
let mut printer = Printer::new(handle, &options);
Expand Down
8 changes: 1 addition & 7 deletions src/printer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use ansi_term::Style;
use console::Term;
use errors::*;
use std::io::Write;
use syntect::highlighting;
Expand All @@ -11,16 +10,12 @@ const PANEL_WIDTH: usize = 7;
pub struct Printer<'a> {
handle: &'a mut Write,
colors: Colors,
term_width: usize,
options: &'a Options<'a>,
pub line_changes: Option<LineChanges>,
}

impl<'a> Printer<'a> {
pub fn new(handle: &'a mut Write, options: &'a Options) -> Self {
let (_, term_width) = Term::stdout().size();
let term_width = term_width as usize;

let colors = if options.colored_output {
Colors::colored()
} else {
Expand All @@ -30,7 +25,6 @@ impl<'a> Printer<'a> {
Printer {
handle,
colors,
term_width,
options,
line_changes: None,
}
Expand Down Expand Up @@ -141,7 +135,7 @@ impl<'a> Printer<'a> {
}

fn print_horizontal_line(&mut self, grid_char: char) -> Result<()> {
let hline = "─".repeat(self.term_width - (PANEL_WIDTH + 1));
let hline = "─".repeat(self.options.term_width - (PANEL_WIDTH + 1));
let hline = format!("{}{}{}", "─".repeat(PANEL_WIDTH), grid_char, hline);

writeln!(self.handle, "{}", self.colors.grid.paint(hline))?;
Expand Down