1use atty::{self, Stream};
2use clap::{App as ClapApp, AppSettings, Arg, ArgGroup, ArgMatches, SubCommand};
5use std::collections::HashSet;
7use style::{OutputComponent, OutputComponents, OutputWrap};
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("wrap")
87 .possible_values(&["character", "never"])
88 .default_value("character")
89 .help("When to wrap text"),
92 Arg::with_name("list-languages")
93 .long("list-languages")
94 .help("Displays supported languages"),
97 Arg::with_name("theme")
100 .help("Set the theme for highlighting"),
103 Arg::with_name("list-themes")
105 .help("Displays supported themes"),
108 SubCommand::with_name("cache")
109 .about("Modify the syntax-definition and theme cache")
111 Arg::with_name("init")
114 .help("Initialize the cache by loading from the config dir"),
117 Arg::with_name("clear")
120 .help("Reset the cache"),
123 Arg::with_name("config-dir")
126 .help("Show the configuration directory"),
129 ArgGroup::with_name("cache-actions")
130 .args(&["init", "clear", "config-dir"])
134 .help_message("Print this help message.")
135 .version_message("Show version information.")
139 pub fn config(&self) -> Result<Config> {
140 let files = self.files();
143 true_color: is_truecolor_terminal(),
144 output_components: self.output_components()?,
145 language: self.matches.value_of("language"),
146 output_wrap: if ! self.interactive_output {
147 // We don't have the tty width when piping to another program.
148 // There's no point in wrapping when this is the case.
151 match self.matches.value_of("wrap") {
152 Some("character") => OutputWrap::Character,
153 Some("never") | _ => OutputWrap::None,
156 colored_output: match self.matches.value_of("color") {
157 Some("always") => true,
158 Some("never") => false,
159 Some("auto") | _ => self.interactive_output,
161 paging: match self.matches.value_of("paging") {
162 Some("always") => true,
163 Some("never") => false,
164 Some("auto") | _ => if files.contains(&None) {
165 // If we are reading from stdin, only enable paging if we write to an
166 // interactive terminal and if we do not *read* from an interactive
168 self.interactive_output && !atty::is(Stream::Stdin)
170 self.interactive_output
173 term_width: Term::stdout().size().1 as usize,
175 theme: self.matches.value_of("theme"),
179 fn files(&self) -> Vec<Option<&str>> {
193 .unwrap_or_else(|| vec![None]) // read from stdin (None) if no args are given
196 fn output_components(&self) -> Result<OutputComponents> {
197 let matches = &self.matches;
199 values_t!(matches.values_of("style"), OutputComponent)?
201 .map(|style| style.components(self.interactive_output))
202 .fold(HashSet::new(), |mut acc, components| {
203 acc.extend(components.iter().cloned());
210pub struct Config<'a> {
211 pub true_color: bool,
212 pub output_wrap: OutputWrap,
213 pub output_components: OutputComponents,
214 pub language: Option<&'a str>,
215 pub colored_output: bool,
217 pub term_width: usize,
218 pub files: Vec<Option<&'a str>>,
219 pub theme: Option<&'a str>,
222fn is_truecolor_terminal() -> bool {
223 env::var("COLORTERM")
224 .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