1use atty::{self, Stream};
2use clap::{App as ClapApp, AppSettings, Arg, ArgGroup, ArgMatches, SubCommand};
5use std::collections::HashSet;
7use style::{OutputComponent, OutputComponents, OutputWrap};
13 pub matches: ArgMatches<'static>,
14 interactive_output: bool,
18 pub fn new() -> Self {
19 let interactive_output = atty::is(Stream::Stdout);
22 let interactive_output = interactive_output && ansi_term::enable_ansi_support().is_ok();
25 matches: Self::matches(interactive_output),
30 fn matches(interactive_output: bool) -> ArgMatches<'static> {
31 let clap_color_setting = if interactive_output {
32 AppSettings::ColoredHelp
34 AppSettings::ColorNever
37 ClapApp::new(crate_name!())
38 .version(crate_version!())
39 .global_setting(clap_color_setting)
40 .global_setting(AppSettings::DeriveDisplayOrder)
41 .global_setting(AppSettings::UnifiedHelpMessage)
42 .global_setting(AppSettings::NextLineHelp)
43 .setting(AppSettings::InferSubcommands)
44 .setting(AppSettings::ArgsNegateSubcommands)
45 .setting(AppSettings::DisableHelpSubcommand)
46 .setting(AppSettings::VersionlessSubcommands)
48 .about(crate_description!())
50 Arg::with_name("language")
53 .help("Set the language for highlighting")
57 Arg::with_name("FILE")
58 .help("File(s) to print")
63 Arg::with_name("style")
68 "auto", "full", "plain", "changes", "header", "grid", "numbers",
70 .default_value("auto")
71 .help("Additional info to display along with content"),
74 Arg::with_name("color")
77 .possible_values(&["auto", "never", "always"])
78 .default_value("auto")
79 .help("When to use colors"),
82 Arg::with_name("paging")
85 .possible_values(&["auto", "never", "always"])
86 .default_value("auto")
87 .help("When to use the pager"),
90 Arg::with_name("wrap")
93 .possible_values(&["character", "never"])
94 .default_value("character")
95 .help("When to wrap text"),
98 Arg::with_name("list-languages")
99 .long("list-languages")
100 .help("Displays supported languages"),
103 Arg::with_name("theme")
106 .help("Set the theme for highlighting"),
109 Arg::with_name("list-themes")
111 .help("Displays supported themes"),
114 SubCommand::with_name("cache")
115 .about("Modify the syntax-definition and theme cache")
117 Arg::with_name("init")
120 .help("Initialize the cache by loading from the config dir"),
123 Arg::with_name("clear")
126 .help("Reset the cache"),
129 Arg::with_name("config-dir")
132 .help("Show the configuration directory"),
135 ArgGroup::with_name("cache-actions")
136 .args(&["init", "clear", "config-dir"])
140 .help_message("Print this help message.")
141 .version_message("Show version information.")
145 pub fn config(&self) -> Result<Config> {
146 let files = self.files();
149 true_color: is_truecolor_terminal(),
150 output_components: self.output_components()?,
151 language: self.matches.value_of("language"),
152 output_wrap: if !self.interactive_output {
153 // We don't have the tty width when piping to another program.
154 // There's no point in wrapping when this is the case.
157 match self.matches.value_of("wrap") {
158 Some("character") => OutputWrap::Character,
159 Some("never") | _ => OutputWrap::None,
162 colored_output: match self.matches.value_of("color") {
163 Some("always") => true,
164 Some("never") => false,
165 Some("auto") | _ => self.interactive_output,
167 paging: match self.matches.value_of("paging") {
168 Some("always") => true,
169 Some("never") => false,
170 Some("auto") | _ => if files.contains(&None) {
171 // If we are reading from stdin, only enable paging if we write to an
172 // interactive terminal and if we do not *read* from an interactive
174 self.interactive_output && !atty::is(Stream::Stdin)
176 self.interactive_output
179 term_width: Term::stdout().size().1 as usize,
181 theme: self.matches.value_of("theme"),
185 fn files(&self) -> Vec<Option<&str>> {
199 .unwrap_or_else(|| vec![None]) // read from stdin (None) if no args are given
202 fn output_components(&self) -> Result<OutputComponents> {
203 let matches = &self.matches;
205 values_t!(matches.values_of("style"), OutputComponent)?
207 .map(|style| style.components(self.interactive_output))
208 .fold(HashSet::new(), |mut acc, components| {
209 acc.extend(components.iter().cloned());
216pub struct Config<'a> {
217 pub true_color: bool,
218 pub output_wrap: OutputWrap,
219 pub output_components: OutputComponents,
220 pub language: Option<&'a str>,
221 pub colored_output: bool,
223 pub term_width: usize,
224 pub files: Vec<Option<&'a str>>,
225 pub theme: Option<&'a str>,
228fn is_truecolor_terminal() -> bool {
229 env::var("COLORTERM")
230 .map(|colorterm| colorterm == "truecolor" || colorterm == "24bit")
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list