1use atty::{self, Stream};
2use clap::{App as ClapApp, AppSettings, Arg, ArgGroup, ArgMatches, SubCommand};
5use std::collections::HashSet;
7use style::{OutputComponent, OutputComponents};
10 pub matches: ArgMatches<'static>,
11 interactive_output: bool,
15 pub fn new() -> Self {
16 let interactive_output = atty::is(Stream::Stdout);
19 matches: Self::matches(interactive_output),
24 fn matches(interactive_output: bool) -> ArgMatches<'static> {
25 let clap_color_setting = if interactive_output {
26 AppSettings::ColoredHelp
28 AppSettings::ColorNever
31 ClapApp::new(crate_name!())
32 .version(crate_version!())
33 .global_setting(clap_color_setting)
34 .global_setting(AppSettings::DeriveDisplayOrder)
35 .global_setting(AppSettings::UnifiedHelpMessage)
36 .global_setting(AppSettings::NextLineHelp)
37 .setting(AppSettings::InferSubcommands)
38 .setting(AppSettings::ArgsNegateSubcommands)
39 .setting(AppSettings::DisableHelpSubcommand)
40 .setting(AppSettings::VersionlessSubcommands)
42 .about(crate_description!())
44 Arg::with_name("language")
47 .help("Set the language for highlighting")
51 Arg::with_name("FILE")
52 .help("File(s) to print")
57 Arg::with_name("style")
62 "auto", "full", "plain", "changes", "header", "grid", "numbers",
64 .default_value("auto")
65 .help("Additional info to display along with content"),
68 Arg::with_name("color")
71 .possible_values(&["auto", "never", "always"])
72 .default_value("auto")
73 .help("When to use colors"),
76 Arg::with_name("paging")
79 .possible_values(&["auto", "never", "always"])
80 .default_value("auto")
81 .help("When to use the pager"),
84 Arg::with_name("list-languages")
85 .long("list-languages")
86 .help("Displays supported languages"),
89 SubCommand::with_name("cache")
90 .about("Modify the syntax-definition and theme cache")
92 Arg::with_name("init")
95 .help("Initialize the cache by loading from the config dir"),
98 Arg::with_name("clear")
101 .help("Reset the cache"),
104 Arg::with_name("config-dir")
107 .help("Show the configuration directory"),
110 ArgGroup::with_name("cache-actions")
111 .args(&["init", "clear", "config-dir"])
115 .help_message("Print this help message.")
116 .version_message("Show version information.")
120 pub fn config(&self) -> Result<Config> {
121 let files = self.files();
124 true_color: is_truecolor_terminal(),
125 output_components: self.output_components()?,
126 language: self.matches.value_of("language"),
127 colored_output: match self.matches.value_of("color") {
128 Some("always") => true,
129 Some("never") => false,
130 Some("auto") | _ => self.interactive_output,
132 paging: match self.matches.value_of("paging") {
133 Some("always") => true,
134 Some("never") => false,
135 Some("auto") | _ => if files.contains(&None) {
136 // If we are reading from stdin, only enable paging if we write to an
137 // interactive terminal and if we do not *read* from an interactive
139 self.interactive_output && !atty::is(Stream::Stdin)
141 self.interactive_output
144 term_width: Term::stdout().size().1 as usize,
149 fn files(&self) -> Vec<Option<&str>> {
163 .unwrap_or_else(|| vec![None]) // read from stdin (None) if no args are given
166 fn output_components(&self) -> Result<OutputComponents> {
167 let matches = &self.matches;
169 values_t!(matches.values_of("style"), OutputComponent)?
171 .map(|style| style.components(self.interactive_output))
172 .fold(HashSet::new(), |mut acc, components| {
173 acc.extend(components.iter().cloned());
180pub struct Config<'a> {
181 pub true_color: bool,
182 pub output_components: OutputComponents,
183 pub language: Option<&'a str>,
184 pub colored_output: bool,
186 pub term_width: usize,
187 pub files: Vec<Option<&'a str>>,
190fn is_truecolor_terminal() -> bool {
191 env::var("COLORTERM")
192 .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