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 Arg::with_name("theme")
92 .help("Set the theme for highlighting"),
95 Arg::with_name("list-themes")
97 .help("Displays supported themes"),
100 SubCommand::with_name("cache")
101 .about("Modify the syntax-definition and theme cache")
103 Arg::with_name("init")
106 .help("Initialize the cache by loading from the config dir"),
109 Arg::with_name("clear")
112 .help("Reset the cache"),
115 Arg::with_name("config-dir")
118 .help("Show the configuration directory"),
121 ArgGroup::with_name("cache-actions")
122 .args(&["init", "clear", "config-dir"])
126 .help_message("Print this help message.")
127 .version_message("Show version information.")
131 pub fn config(&self) -> Result<Config> {
132 let files = self.files();
135 true_color: is_truecolor_terminal(),
136 output_components: self.output_components()?,
137 language: self.matches.value_of("language"),
138 colored_output: match self.matches.value_of("color") {
139 Some("always") => true,
140 Some("never") => false,
141 Some("auto") | _ => self.interactive_output,
143 paging: match self.matches.value_of("paging") {
144 Some("always") => true,
145 Some("never") => false,
146 Some("auto") | _ => if files.contains(&None) {
147 // If we are reading from stdin, only enable paging if we write to an
148 // interactive terminal and if we do not *read* from an interactive
150 self.interactive_output && !atty::is(Stream::Stdin)
152 self.interactive_output
155 term_width: Term::stdout().size().1 as usize,
157 theme: self.matches.value_of("theme"),
161 fn files(&self) -> Vec<Option<&str>> {
175 .unwrap_or_else(|| vec![None]) // read from stdin (None) if no args are given
178 fn output_components(&self) -> Result<OutputComponents> {
179 let matches = &self.matches;
181 values_t!(matches.values_of("style"), OutputComponent)?
183 .map(|style| style.components(self.interactive_output))
184 .fold(HashSet::new(), |mut acc, components| {
185 acc.extend(components.iter().cloned());
192pub struct Config<'a> {
193 pub true_color: bool,
194 pub output_components: OutputComponents,
195 pub language: Option<&'a str>,
196 pub colored_output: bool,
198 pub term_width: usize,
199 pub files: Vec<Option<&'a str>>,
200 pub theme: Option<&'a str>,
203fn is_truecolor_terminal() -> bool {
204 env::var("COLORTERM")
205 .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