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 let interactive_output = interactive_output && ansi_term::enable_ansi_support().is_ok();
22 matches: Self::matches(interactive_output),
27 fn matches(interactive_output: bool) -> ArgMatches<'static> {
28 let clap_color_setting = if interactive_output {
29 AppSettings::ColoredHelp
31 AppSettings::ColorNever
34 ClapApp::new(crate_name!())
35 .version(crate_version!())
36 .global_setting(clap_color_setting)
37 .global_setting(AppSettings::DeriveDisplayOrder)
38 .global_setting(AppSettings::UnifiedHelpMessage)
39 .global_setting(AppSettings::NextLineHelp)
40 .setting(AppSettings::InferSubcommands)
41 .setting(AppSettings::ArgsNegateSubcommands)
42 .setting(AppSettings::DisableHelpSubcommand)
43 .setting(AppSettings::VersionlessSubcommands)
45 .about(crate_description!())
47 Arg::with_name("language")
50 .help("Set the language for highlighting")
54 Arg::with_name("FILE")
55 .help("File(s) to print")
60 Arg::with_name("style")
65 "auto", "full", "plain", "changes", "header", "grid", "numbers",
67 .default_value("auto")
68 .help("Additional info to display along with content"),
71 Arg::with_name("color")
74 .possible_values(&["auto", "never", "always"])
75 .default_value("auto")
76 .help("When to use colors"),
79 Arg::with_name("paging")
82 .possible_values(&["auto", "never", "always"])
83 .default_value("auto")
84 .help("When to use the pager"),
87 Arg::with_name("list-languages")
88 .long("list-languages")
89 .help("Displays supported languages"),
92 Arg::with_name("theme")
95 .help("Set the theme for highlighting"),
98 Arg::with_name("list-themes")
100 .help("Displays supported themes"),
103 SubCommand::with_name("cache")
104 .about("Modify the syntax-definition and theme cache")
106 Arg::with_name("init")
109 .help("Initialize the cache by loading from the config dir"),
112 Arg::with_name("clear")
115 .help("Reset the cache"),
118 Arg::with_name("config-dir")
121 .help("Show the configuration directory"),
124 ArgGroup::with_name("cache-actions")
125 .args(&["init", "clear", "config-dir"])
129 .help_message("Print this help message.")
130 .version_message("Show version information.")
134 pub fn config(&self) -> Result<Config> {
135 let files = self.files();
138 true_color: is_truecolor_terminal(),
139 output_components: self.output_components()?,
140 language: self.matches.value_of("language"),
141 colored_output: match self.matches.value_of("color") {
142 Some("always") => true,
143 Some("never") => false,
144 Some("auto") | _ => self.interactive_output,
146 paging: match self.matches.value_of("paging") {
147 Some("always") => true,
148 Some("never") => false,
149 Some("auto") | _ => if files.contains(&None) {
150 // If we are reading from stdin, only enable paging if we write to an
151 // interactive terminal and if we do not *read* from an interactive
153 self.interactive_output && !atty::is(Stream::Stdin)
155 self.interactive_output
158 term_width: Term::stdout().size().1 as usize,
160 theme: self.matches.value_of("theme"),
164 fn files(&self) -> Vec<Option<&str>> {
178 .unwrap_or_else(|| vec![None]) // read from stdin (None) if no args are given
181 fn output_components(&self) -> Result<OutputComponents> {
182 let matches = &self.matches;
184 values_t!(matches.values_of("style"), OutputComponent)?
186 .map(|style| style.components(self.interactive_output))
187 .fold(HashSet::new(), |mut acc, components| {
188 acc.extend(components.iter().cloned());
195pub struct Config<'a> {
196 pub true_color: bool,
197 pub output_components: OutputComponents,
198 pub language: Option<&'a str>,
199 pub colored_output: bool,
201 pub term_width: usize,
202 pub files: Vec<Option<&'a str>>,
203 pub theme: Option<&'a str>,
206fn is_truecolor_terminal() -> bool {
207 env::var("COLORTERM")
208 .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