delta_lib/cli.rs
1use std::collections::{HashMap, HashSet};
2use std::ffi::OsString;
3use std::path::PathBuf;
4
5use bat::assets::HighlightingAssets;
6use clap::{AppSettings, ColorChoice, FromArgMatches, IntoApp, Parser};
7use lazy_static::lazy_static;
8use syntect::highlighting::Theme as SyntaxTheme;
9use syntect::parsing::SyntaxSet;
10
11use crate::config::delta_unreachable;
12use crate::env::DeltaEnv;
13use crate::git_config::{GitConfig, GitConfigEntry};
14use crate::options;
15use crate::utils;
16use crate::utils::bat::output::PagingMode;
17
18#[derive(Parser)]
19#[clap(
20 name = "delta",
21 about = "A viewer for git and diff output",
22 version,
23 color = ColorChoice::Always,
24 setting(AppSettings::DeriveDisplayOrder),
25 term_width(0),
26 after_long_help = "\
27GIT CONFIG
28----------
29
30By default, delta takes settings from a section named \"delta\" in git config files, if one is
31present. The git config file to use for delta options will usually be ~/.gitconfig, but delta
32follows the rules given in https://git-scm.com/docs/git-config#FILES. Most delta options can be
33given in a git config file, using the usual option names but without the initial '--'. An example
34is
35
36[delta]
37 line-numbers = true
38 zero-style = dim syntax
39
40FEATURES
41--------
42A feature is a named collection of delta options in git config. An example is:
43
44[delta \"my-delta-feature\"]
45 syntax-theme = Dracula
46 plus-style = bold syntax \"#002800\"
47
48To activate those options, you would use:
49
50delta --features my-delta-feature
51
52A feature name may not contain whitespace. You can activate multiple features:
53
54[delta]
55 features = my-highlight-styles-colors-feature my-line-number-styles-feature
56
57If more than one feature sets the same option, the last one wins.
58
59STYLES
60------
61
62All options that have a name like --*-style work the same way. It is very similar to how
63colors/styles are specified in a gitconfig file:
64https://git-scm.com/docs/git-config#Documentation/git-config.txt-color
65
66Here is an example:
67
68--minus-style 'red bold ul \"#ffeeee\"'
69
70That means: For removed lines, set the foreground (text) color to 'red', make it bold and
71 underlined, and set the background color to '#ffeeee'.
72
73See the COLORS section below for how to specify a color. In addition to real colors, there are 4
74special color names: 'auto', 'normal', 'raw', and 'syntax'.
75
76Here is an example of using special color names together with a single attribute:
77
78--minus-style 'syntax bold auto'
79
80That means: For removed lines, syntax-highlight the text, and make it bold, and do whatever delta
81 normally does for the background.
82
83The available attributes are: 'blink', 'bold', 'dim', 'hidden', 'italic', 'reverse', 'strike',
84and 'ul' (or 'underline').
85
86The attribute 'omit' is supported by commit-style, file-style, and hunk-header-style, meaning to
87remove the element entirely from the output.
88
89A complete description of the style string syntax follows:
90
91- If the input that delta is receiving already has colors, and you want delta to output those
92 colors unchanged, then use the special style string 'raw'. Otherwise, delta will strip any colors
93 from its input.
94
95- A style string consists of 0, 1, or 2 colors, together with an arbitrary number of style
96 attributes, all separated by spaces.
97
98- The first color is the foreground (text) color. The second color is the background color.
99 Attributes can go in any position.
100
101- This means that in order to specify a background color you must also specify a foreground (text)
102 color.
103
104- If you want delta to choose one of the colors automatically, then use the special color 'auto'.
105 This can be used for both foreground and background.
106
107- If you want the foreground/background color to be your terminal's foreground/background color,
108 then use the special color 'normal'.
109
110- If you want the foreground text to be syntax-highlighted according to its language, then use the
111 special foreground color 'syntax'. This can only be used for the foreground (text).
112
113- The minimal style specification is the empty string ''. This means: do not apply any colors or
114 styling to the element in question.
115
116COLORS
117------
118
119There are four ways to specify a color (this section applies to foreground and background colors
120within a style string):
121
1221. CSS color name
123
124 Any of the 140 color names used in CSS: https://www.w3schools.com/colors/colors_groups.asp
125
1262. RGB hex code
127
128 An example of using an RGB hex code is:
129 --file-style=\"#0e7c0e\"
130
1313. ANSI color name
132
133 There are 8 ANSI color names:
134 black, red, green, yellow, blue, magenta, cyan, white.
135
136 In addition, all of them have a bright form:
137 brightblack, brightred, brightgreen, brightyellow, brightblue, brightmagenta, brightcyan, brightwhite.
138
139 An example of using an ANSI color name is:
140 --file-style=\"green\"
141
142 Unlike RGB hex codes, ANSI color names are just names: you can choose the exact color that each
143 name corresponds to in the settings of your terminal application (the application you use to
144 enter commands at a shell prompt). This means that if you use ANSI color names, and you change
145 the color theme used by your terminal, then delta's colors will respond automatically, without
146 needing to change the delta command line.
147
148 \"purple\" is accepted as a synonym for \"magenta\". Color names and codes are case-insensitive.
149
1504. ANSI color number
151
152 An example of using an ANSI color number is:
153 --file-style=28
154
155 There are 256 ANSI color numbers: 0-255. The first 16 are the same as the colors described in
156 the \"ANSI color name\" section above. See https://en.wikipedia.org/wiki/ANSI_escape_code#8-bit.
157 Specifying colors like this is useful if your terminal only supports 256 colors (i.e. doesn\'t
158 support 24-bit color).
159
160
161LINE NUMBERS
162------------
163
164To display line numbers, use --line-numbers.
165
166Line numbers are displayed in two columns. Here's what it looks like by default:
167
168 1 ⋮ 1 │ unchanged line
169 2 ⋮ │ removed line
170 ⋮ 2 │ added line
171
172In that output, the line numbers for the old (minus) version of the file appear in the left column,
173and the line numbers for the new (plus) version of the file appear in the right column. In an
174unchanged (zero) line, both columns contain a line number.
175
176The following options allow the line number display to be customized:
177
178--line-numbers-left-format: Change the contents of the left column
179--line-numbers-right-format: Change the contents of the right column
180--line-numbers-left-style: Change the style applied to the left column
181--line-numbers-right-style: Change the style applied to the right column
182--line-numbers-minus-style: Change the style applied to line numbers in minus lines
183--line-numbers-zero-style: Change the style applied to line numbers in unchanged lines
184--line-numbers-plus-style: Change the style applied to line numbers in plus lines
185
186Options --line-numbers-left-format and --line-numbers-right-format allow you to change the contents
187of the line number columns. Their values are arbitrary format strings, which are allowed to contain
188the placeholders {nm} for the line number associated with the old version of the file and {np} for
189the line number associated with the new version of the file. The placeholders support a subset of
190the string formatting syntax documented here: https://doc.rust-lang.org/std/fmt/#formatting-parameters.
191Specifically, you can use the alignment and width syntax.
192
193For example, the default value of --line-numbers-left-format is '{nm:^4}⋮'. This means that the
194left column should display the minus line number (nm), center-aligned, padded with spaces to a
195width of 4 characters, followed by a unicode dividing-line character (⋮).
196
197Similarly, the default value of --line-numbers-right-format is '{np:^4}│'. This means that the
198right column should display the plus line number (np), center-aligned, padded with spaces to a
199width of 4 characters, followed by a unicode dividing-line character (│).
200
201Use '<' for left-align, '^' for center-align, and '>' for right-align.
202
203
204If something isn't working correctly, or you have a feature request, please open an issue at
205https://github.com/dandavison/delta/issues.
206
207For a short help summary, please use delta -h.
208"
209)]
210pub struct Opt {
211 #[clap(long = "blame-code-style", value_name = "STYLE")]
212 /// Style string for the code section of a git blame line.
213 ///
214 /// By default the code will be syntax-highlighted with the same background color as the blame
215 /// format section of the line (the background color is determined by blame-palette). E.g.
216 /// setting this option to 'syntax' will syntax-highlight the code with no background color.
217 pub blame_code_style: Option<String>,
218
219 #[clap(
220 long = "blame-format",
221 default_value = "{timestamp:<15} {author:<15.14} {commit:<8}",
222 value_name = "FMT"
223 )]
224 /// Format string for git blame commit metadata.
225 ///
226 /// Available placeholders are "{timestamp}", "{author}", and "{commit}".
227 pub blame_format: String,
228
229 #[clap(long = "blame-palette", value_name = "COLORS")]
230 /// Background colors used for git blame lines (space-separated string).
231 ///
232 /// Lines added by the same commit are painted with the same color; colors are recycled as
233 /// needed.
234 pub blame_palette: Option<String>,
235
236 #[clap(
237 long = "blame-separator-format",
238 default_value = "│{n:^4}│",
239 value_name = "FMT"
240 )]
241 /// Separator between the blame format and the code section of a git blame line.
242 ///
243 /// Contains the line number by default. Possible values are "none" to disable line numbers or a format
244 /// string. This may contain one "{n:}" placeholder and will display the line number on every line.
245 /// A type may be added after all other format specifiers and can be separated by '_':
246 /// If type is set to 'block' (e.g. "{n:^4_block}") the line number will only be shown when a new blame
247 /// block starts; or if it is set to 'every-N' the line will be show with every block and every
248 /// N-th (modulo) line.
249 pub blame_separator_format: String,
250
251 #[clap(long = "blame-separator-style", value_name = "STYLE")]
252 /// Style string for the blame-separator-format.
253 pub blame_separator_style: Option<String>,
254
255 #[clap(
256 long = "blame-timestamp-format",
257 default_value = "%Y-%m-%d %H:%M:%S %z",
258 value_name = "FMT"
259 )]
260 /// Format of `git blame` timestamp in raw git output received by delta.
261 pub blame_timestamp_format: String,
262
263 #[clap(long = "blame-timestamp-output-format", value_name = "FMT")]
264 /// Format string for git blame timestamp output.
265 ///
266 /// This string is used for formatting the timestamps in git blame output. It must follow
267 /// the `strftime` format syntax specification. If it is not present, the timestamps will
268 /// be formatted in a human-friendly but possibly less accurate form.
269 ///
270 /// See: (https://docs.rs/chrono/latest/chrono/format/strftime/index.html)
271 pub blame_timestamp_output_format: Option<String>,
272
273 #[clap(long = "color-only")]
274 /// Do not alter the input structurally in any way.
275 ///
276 /// But color and highlight hunk lines according to your delta configuration. This is mainly
277 /// intended for other tools that use delta.
278 pub color_only: bool,
279
280 #[clap(
281 long = "commit-decoration-style",
282 default_value = "",
283 value_name = "STYLE"
284 )]
285 /// Style string for the commit hash decoration.
286 ///
287 /// See STYLES section. The style string should contain one of the special attributes 'box',
288 /// 'ul' (underline), 'ol' (overline), or the combination 'ul ol'.
289 pub commit_decoration_style: String,
290
291 #[clap(
292 long = "commit-regex",
293 default_value = r"^commit ",
294 value_name = "REGEX"
295 )]
296 /// Regular expression used to identify the commit line when parsing git output.
297 pub commit_regex: String,
298
299 #[clap(long = "commit-style", default_value = "raw", value_name = "STYLE")]
300 /// Style string for the commit hash line.
301 ///
302 /// See STYLES section. The style 'omit' can be used to remove the commit hash line from the
303 /// output.
304 pub commit_style: String,
305
306 #[clap(long = "dark")]
307 /// Use default colors appropriate for a dark terminal background.
308 ///
309 /// For more control, see the style options and --syntax-theme.
310 pub dark: bool,
311
312 #[clap(long = "default-language", value_name = "LANG")]
313 /// Default language used for syntax highlighting.
314 ///
315 /// Used when the language cannot be inferred from a filename. It will typically make sense to
316 /// set this in per-repository git config (.git/config)
317 pub default_language: Option<String>,
318
319 #[clap(long = "diff-highlight")]
320 /// Emulate diff-highlight.
321 ///
322 /// (https://github.com/git/git/tree/master/contrib/diff-highlight)
323 pub diff_highlight: bool,
324
325 #[clap(long = "diff-so-fancy")]
326 /// Emulate diff-so-fancy.
327 ///
328 /// (https://github.com/so-fancy/diff-so-fancy)
329 pub diff_so_fancy: bool,
330
331 #[clap(long = "diff-stat-align-width", default_value = "48", value_name = "N")]
332 /// Width allocated for file paths in a diff stat section.
333 ///
334 /// If a relativized file path exceeds this width then the diff stat will be misaligned.
335 pub diff_stat_align_width: usize,
336
337 #[clap(long = "features", value_name = "FEATURES")]
338 /// Names of delta features to activate (space-separated).
339 ///
340 /// A feature is a named collection of delta options in ~/.gitconfig. See FEATURES section. The
341 /// environment variable DELTA_FEATURES can be set to a space-separated list of feature names.
342 /// If this is preceded with a + character, the features from the environment variable will be added
343 /// to those specified in git config. E.g. DELTA_FEATURES=+side-by-side can be used to activate
344 /// side-by-side temporarily (use DELTA_FEATURES=+ to go back to just the features from git config).
345 pub features: Option<String>,
346
347 #[clap(
348 long = "file-added-label",
349 default_value = "added:",
350 value_name = "STRING"
351 )]
352 /// Text to display before an added file path.
353 ///
354 /// Used in the default value of navigate-regex.
355 pub file_added_label: String,
356
357 #[clap(
358 long = "file-copied-label",
359 default_value = "copied:",
360 value_name = "STRING"
361 )]
362 /// Text to display before a copied file path.
363 pub file_copied_label: String,
364
365 #[clap(
366 long = "file-decoration-style",
367 default_value = "blue ul",
368 value_name = "STYLE"
369 )]
370 /// Style string for the file decoration.
371 ///
372 /// See STYLES section. The style string should contain one of the special attributes 'box',
373 /// 'ul' (underline), 'ol' (overline), or the combination 'ul ol'.
374 pub file_decoration_style: String,
375
376 #[clap(
377 long = "file-modified-label",
378 default_value = "",
379 value_name = "STRING"
380 )]
381 /// Text to display before a modified file path.
382 ///
383 /// Used in the default value of navigate-regex.
384 pub file_modified_label: String,
385
386 #[clap(
387 long = "file-removed-label",
388 default_value = "removed:",
389 value_name = "STRING"
390 )]
391 /// Text to display before a removed file path.
392 ///
393 /// Used in the default value of navigate-regex.
394 pub file_removed_label: String,
395
396 #[clap(
397 long = "file-renamed-label",
398 default_value = "renamed:",
399 value_name = "STRING"
400 )]
401 /// Text to display before a renamed file path.
402 ///
403 /// Used in the default value of navigate-regex.
404 pub file_renamed_label: String,
405
406 #[clap(long = "file-style", default_value = "blue", value_name = "STYLE")]
407 /// Style string for the file section.
408 ///
409 /// See STYLES section. The style 'omit' can be used to remove the file section from the output.
410 pub file_style: String,
411
412 #[structopt(long = "file-transformation", value_name = "SED_CMD")]
413 /// Sed-style command transforming file paths for display.
414 pub file_regex_replacement: Option<String>,
415
416 #[clap(long = "grep-context-line-style", value_name = "STYLE")]
417 /// Style string for non-matching lines of grep output.
418 ///
419 /// See STYLES section. Defaults to zero-style.
420 pub grep_context_line_style: Option<String>,
421
422 #[clap(long = "grep-file-style", value_name = "STYLE")]
423 /// Style string for file paths in grep output.
424 ///
425 /// See STYLES section. Defaults to hunk-header-file-path-style.
426 pub grep_file_style: Option<String>,
427
428 #[clap(long = "grep-line-number-style", value_name = "STYLE")]
429 /// Style string for line numbers in grep output.
430 ///
431 /// See STYLES section. Defaults to hunk-header-line-number-style.
432 pub grep_line_number_style: Option<String>,
433
434 #[clap(long = "grep-match-line-style", value_name = "STYLE")]
435 /// Style string for matching lines of grep output.
436 ///
437 /// See STYLES section. Defaults to plus-style.
438 pub grep_match_line_style: Option<String>,
439
440 #[clap(long = "grep-match-word-style", value_name = "STYLE")]
441 /// Style string for the matching substrings within a matching line of grep output.
442 ///
443 /// See STYLES section. Defaults to plus-style.
444 pub grep_match_word_style: Option<String>,
445
446 #[clap(
447 long = "grep-separator-symbol",
448 default_value = ":",
449 value_name = "STRING"
450 )]
451 /// Separator symbol printed after the file path and line number in grep output.
452 ///
453 /// Defaults to ":" for both match and context lines, since many terminal emulators recognize
454 /// constructs like "/path/to/file:7:". However, standard grep output uses "-" for context
455 /// lines: set this option to "keep" to keep the original separator symbols.
456 pub grep_separator_symbol: String,
457
458 #[clap(
459 long = "hunk-header-decoration-style",
460 default_value = "blue box",
461 value_name = "STYLE"
462 )]
463 /// Style string for the hunk-header decoration.
464 ///
465 /// See STYLES section. The style string should contain one of the special attributes 'box',
466 /// 'ul' (underline), 'ol' (overline), or the combination 'ul ol'.
467 pub hunk_header_decoration_style: String,
468
469 #[clap(
470 long = "hunk-header-file-style",
471 default_value = "blue",
472 value_name = "STYLE"
473 )]
474 /// Style string for the file path part of the hunk-header.
475 ///
476 /// See STYLES section. The file path will only be displayed if hunk-header-style contains the
477 /// 'file' special attribute.
478 pub hunk_header_file_style: String,
479
480 #[clap(
481 long = "hunk-header-line-number-style",
482 default_value = "blue",
483 value_name = "STYLE"
484 )]
485 /// Style string for the line number part of the hunk-header.
486 ///
487 /// See STYLES section. The line number will only be displayed if hunk-header-style contains the
488 /// 'line-number' special attribute.
489 pub hunk_header_line_number_style: String,
490
491 #[clap(
492 long = "hunk-header-style",
493 default_value = "line-number syntax",
494 value_name = "STYLE"
495 )]
496 /// Style string for the hunk-header.
497 ///
498 /// See STYLES section. Special attributes 'file' and 'line-number' can be used to include the
499 /// file path, and number of first hunk line, in the hunk header. The style 'omit' can be used
500 /// to remove the hunk header section from the output.
501 pub hunk_header_style: String,
502
503 #[clap(long = "hunk-label", default_value = "", value_name = "STRING")]
504 /// Text to display before a hunk header.
505 ///
506 /// Used in the default value of navigate-regex.
507 pub hunk_label: String,
508
509 #[clap(long = "hyperlinks")]
510 /// Render commit hashes, file names, and line numbers as hyperlinks.
511 ///
512 /// Following the hyperlink spec for terminal emulators:
513 /// https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda. By default, file names
514 /// and line numbers link to the local file using a file URL, whereas commit hashes link to the
515 /// commit in GitHub, if the remote repository is hosted by GitHub. See
516 /// --hyperlinks-file-link-format for full control over the file URLs emitted. Hyperlinks are
517 /// supported by several common terminal emulators. To make them work, you must use less version
518 /// >= 581 with the -R flag (or use -r with older less versions, but this will break e.g.
519 /// --navigate). If you use tmux, then you will also need a patched fork of tmux (see
520 /// https://github.com/dandavison/tmux).
521 pub hyperlinks: bool,
522
523 #[clap(long = "hyperlinks-commit-link-format", value_name = "FMT")]
524 /// Format string for commit hyperlinks (requires --hyperlinks).
525 ///
526 /// The placeholder "{commit}" will be replaced by the commit hash. For example:
527 /// --hyperlinks-commit-link-format='https://mygitrepo/{commit}/'
528 pub hyperlinks_commit_link_format: Option<String>,
529
530 #[clap(
531 long = "hyperlinks-file-link-format",
532 default_value = "file://{path}",
533 value_name = "FMT"
534 )]
535 /// Format string for file hyperlinks (requires --hyperlinks).
536 ///
537 /// The placeholders "{path}" and "{line}" will be replaced by the absolute file path and the
538 /// line number, respectively. The default value of this option creates hyperlinks using
539 /// standard file URLs; your operating system should open these in the application registered
540 /// for that file type. However, these do not make use of the line number. In order for the link
541 /// to open the file at the correct line number, you could use a custom URL format such as
542 /// "file-line://{path}:{line}" and register an application to handle the custom "file-line" URL
543 /// scheme by opening the file in your editor/IDE at the indicated line number. See
544 /// https://github.com/dandavison/open-in-editor for an example.
545 pub hyperlinks_file_link_format: String,
546
547 #[clap(
548 long = "inline-hint-style",
549 default_value = "blue",
550 value_name = "STYLE"
551 )]
552 /// Style string for short inline hint text.
553 ///
554 /// This styles certain content added by delta to the original diff such as special characters
555 /// to highlight tabs, and the symbols used to indicate wrapped lines. See STYLES section.
556 pub inline_hint_style: String,
557
558 #[clap(
559 long = "inspect-raw-lines",
560 default_value = "true",
561 value_name = "true|false"
562 )]
563 /// Kill-switch for --color-moved support.
564 ///
565 /// Whether to examine ANSI color escape sequences in raw lines received from Git and handle
566 /// lines colored in certain ways specially. This is on by default: it is how Delta supports
567 /// Git's --color-moved feature. Set this to "false" to disable this behavior.
568 pub inspect_raw_lines: String,
569
570 #[clap(long = "keep-plus-minus-markers")]
571 /// Prefix added/removed lines with a +/- character, as git does.
572 ///
573 /// By default, delta does not emit any prefix, so code can be copied directly from delta's
574 /// output.
575 pub keep_plus_minus_markers: bool,
576
577 #[clap(long = "light")]
578 /// Use default colors appropriate for a light terminal background.
579 ///
580 /// For more control, see the style options and --syntax-theme.
581 pub light: bool,
582
583 #[clap(long = "line-buffer-size", default_value = "32", value_name = "N")]
584 /// Size of internal line buffer.
585 ///
586 /// Delta compares the added and removed versions of nearby lines in order to detect and
587 /// highlight changes at the level of individual words/tokens. Therefore, nearby lines must be
588 /// buffered internally before they are painted and emitted. Increasing this value might improve
589 /// highlighting of some large diff hunks. However, setting this to a high value will adversely
590 /// affect delta's performance when entire files are added/removed.
591 pub line_buffer_size: usize,
592
593 #[clap(long = "line-fill-method", value_name = "STRING")]
594 /// Line-fill method in side-by-side mode.
595 ///
596 /// How to extend the background color to the end of the line in side-by-side mode. Can be ansi
597 /// (default) or spaces (default if output is not to a terminal). Has no effect if
598 /// --width=variable is given.
599 pub line_fill_method: Option<String>,
600
601 #[clap(short = 'n', long = "line-numbers")]
602 /// Display line numbers next to the diff.
603 ///
604 /// See LINE NUMBERS section.
605 pub line_numbers: bool,
606
607 #[clap(
608 long = "line-numbers-left-format",
609 default_value = "{nm:^4}⋮",
610 value_name = "FMT"
611 )]
612 /// Format string for the left column of line numbers.
613 ///
614 /// A typical value would be "{nm:^4}⋮" which means to display the line numbers of the minus
615 /// file (old version), center-aligned, padded to a width of 4 characters, followed by a
616 /// dividing character. See the LINE NUMBERS section.
617 pub line_numbers_left_format: String,
618
619 #[clap(
620 long = "line-numbers-left-style",
621 default_value = "auto",
622 value_name = "STYLE"
623 )]
624 /// Style string for the left column of line numbers.
625 ///
626 /// See STYLES and LINE NUMBERS sections.
627 pub line_numbers_left_style: String,
628
629 #[clap(
630 long = "line-numbers-minus-style",
631 default_value = "auto",
632 value_name = "STYLE"
633 )]
634 /// Style string for line numbers in the old (minus) version of the file.
635 ///
636 /// See STYLES and LINE NUMBERS sections.
637 pub line_numbers_minus_style: String,
638
639 #[clap(
640 long = "line-numbers-plus-style",
641 default_value = "auto",
642 value_name = "STYLE"
643 )]
644 /// Style string for line numbers in the new (plus) version of the file.
645 ///
646 /// See STYLES and LINE NUMBERS sections.
647 pub line_numbers_plus_style: String,
648
649 #[clap(
650 long = "line-numbers-right-format",
651 default_value = "{np:^4}│",
652 value_name = "FMT"
653 )]
654 /// Format string for the right column of line numbers.
655 ///
656 /// A typical value would be "{np:^4}│ " which means to display the line numbers of the plus
657 /// file (new version), center-aligned, padded to a width of 4 characters, followed by a
658 /// dividing character, and a space. See the LINE NUMBERS section.
659 pub line_numbers_right_format: String,
660
661 #[clap(
662 long = "line-numbers-right-style",
663 default_value = "auto",
664 value_name = "STYLE"
665 )]
666 /// Style string for the right column of line numbers.
667 ///
668 /// See STYLES and LINE NUMBERS sections.
669 pub line_numbers_right_style: String,
670
671 #[clap(
672 long = "line-numbers-zero-style",
673 default_value = "auto",
674 value_name = "STYLE"
675 )]
676 /// Style string for line numbers in unchanged (zero) lines.
677 ///
678 /// See STYLES and LINE NUMBERS sections.
679 pub line_numbers_zero_style: String,
680
681 #[clap(long = "list-languages")]
682 /// List supported languages and associated file extensions.
683 pub list_languages: bool,
684
685 #[clap(long = "list-syntax-themes")]
686 /// List available syntax-highlighting color themes.
687 pub list_syntax_themes: bool,
688
689 #[clap(long = "map-styles", value_name = "STYLES_MAP")]
690 /// Map styles encountered in raw input to desired output styles.
691 ///
692 /// An example is --map-styles='bold purple => red "#eeeeee", bold cyan => syntax "#eeeeee"'
693 pub map_styles: Option<String>,
694
695 #[clap(long = "max-line-distance", default_value = "0.6", value_name = "DIST")]
696 /// Maximum line pair distance parameter in within-line diff algorithm.
697 ///
698 /// This parameter is the maximum distance (0.0 - 1.0) between two lines for them to be inferred
699 /// to be homologous. Homologous line pairs are highlighted according to the deletion and
700 /// insertion operations transforming one into the other.
701 pub max_line_distance: f64,
702
703 #[clap(long = "max-line-length", default_value = "512", value_name = "N")]
704 /// Truncate lines longer than this.
705 ///
706 /// To prevent any truncation, set to zero. Note that delta will be slow on very long lines
707 /// (e.g. minified .js) if truncation is disabled. When wrapping lines it is automatically set
708 /// to fit at least all visible characters.
709 pub max_line_length: usize,
710
711 #[clap(
712 long = "merge-conflict-begin-symbol",
713 default_value = "▼",
714 value_name = "STRING"
715 )]
716 /// String marking the beginning of a merge conflict region.
717 ///
718 /// The string will be repeated until it reaches the required length.
719 pub merge_conflict_begin_symbol: String,
720
721 #[clap(
722 long = "merge-conflict-end-symbol",
723 default_value = "▲",
724 value_name = "STRING"
725 )]
726 /// String marking the end of a merge conflict region.
727 ///
728 /// The string will be repeated until it reaches the required length.
729 pub merge_conflict_end_symbol: String,
730
731 #[clap(
732 long = "merge-conflict-ours-diff-header-decoration-style",
733 default_value = "box",
734 value_name = "STYLE"
735 )]
736 /// Style string for the decoration of the header above the 'ours' merge conflict diff.
737 ///
738 /// This styles the decoration of the header above the diff between the ancestral commit and the
739 /// 'ours' branch. See STYLES section. The style string should contain one of the special
740 /// attributes 'box', 'ul' (underline), 'ol' (overline), or the combination 'ul ol'.
741 pub merge_conflict_ours_diff_header_decoration_style: String,
742
743 #[clap(
744 long = "merge-conflict-ours-diff-header-style",
745 default_value = "normal",
746 value_name = "STYLE"
747 )]
748 /// Style string for the header above the 'ours' branch merge conflict diff.
749 ///
750 /// See STYLES section.
751 pub merge_conflict_ours_diff_header_style: String,
752
753 #[clap(
754 long = "merge-conflict-theirs-diff-header-decoration-style",
755 default_value = "box",
756 value_name = "STYLE"
757 )]
758 /// Style string for the decoration of the header above the 'theirs' merge conflict diff.
759 ///
760 /// This styles the decoration of the header above the diff between the ancestral commit and
761 /// 'their' branch. See STYLES section. The style string should contain one of the special
762 /// attributes 'box', 'ul' (underline), 'ol' (overline), or the combination 'ul ol'.
763 pub merge_conflict_theirs_diff_header_decoration_style: String,
764
765 #[clap(
766 long = "merge-conflict-theirs-diff-header-style",
767 default_value = "normal",
768 value_name = "STYLE"
769 )]
770 /// Style string for the header above the 'theirs' branch merge conflict diff.
771 ///
772 /// This styles the header above the diff between the ancestral commit and 'their' branch. See
773 /// STYLES section.
774 pub merge_conflict_theirs_diff_header_style: String,
775
776 #[clap(
777 long = "--minus-empty-line-marker-style",
778 default_value = "normal auto",
779 value_name = "STYLE"
780 )]
781 /// Style string for removed empty line marker.
782 ///
783 /// Used only if --minus-style has no background color.
784 pub minus_empty_line_marker_style: String,
785
786 #[clap(
787 long = "minus-emph-style",
788 default_value = "normal auto",
789 value_name = "STYLE"
790 )]
791 /// Style string for emphasized sections of removed lines.
792 ///
793 /// See STYLES section.
794 pub minus_emph_style: String,
795
796 #[clap(
797 long = "minus-non-emph-style",
798 default_value = "minus-style",
799 value_name = "STYLE"
800 )]
801 /// Style string for non-emphasized sections of removed lines that have an emphasized section.
802 ///
803 /// See STYLES section.
804 pub minus_non_emph_style: String,
805
806 #[clap(
807 long = "minus-style",
808 default_value = "normal auto",
809 value_name = "STYLE"
810 )]
811 /// Style string for removed lines.
812 ///
813 /// See STYLES section.
814 pub minus_style: String,
815
816 #[clap(long = "navigate")]
817 /// Activate diff navigation.
818 ///
819 /// Use n to jump forwards and N to jump backwards. To change the file labels used see
820 /// --file-modified-label, --file-removed-label, --file-added-label, --file-renamed-label.
821 pub navigate: bool,
822
823 #[clap(long = "navigate-regex", value_name = "REGEX")]
824 /// Regular expression defining navigation stop points.
825 pub navigate_regex: Option<String>,
826
827 #[clap(long = "no-gitconfig")]
828 /// Do not read any settings from git config.
829 ///
830 /// See GIT CONFIG section.
831 pub no_gitconfig: bool,
832
833 #[clap(long = "pager", value_name = "CMD")]
834 /// Which pager to use.
835 ///
836 /// The default pager is `less`. You can also change pager by setting the environment variables
837 /// DELTA_PAGER, BAT_PAGER, or PAGER (and that is their order of priority). This option
838 /// overrides all environment variables above.
839 pub pager: Option<String>,
840
841 #[clap(
842 long = "paging",
843 default_value = "auto",
844 value_name = "auto|always|never"
845 )]
846 /// Whether to use a pager when displaying output.
847 ///
848 /// Options are: auto, always, and never.
849 pub paging_mode: String,
850
851 #[clap(long = "parse-ansi")]
852 /// Display ANSI color escape sequences in human-readable form.
853 ///
854 /// Example usage: git show --color=always | delta --parse-ansi
855 /// This can be used to help identify input style strings to use with map-styles.
856 pub parse_ansi: bool,
857
858 #[clap(
859 long = "plus-emph-style",
860 default_value = "syntax auto",
861 value_name = "STYLE"
862 )]
863 /// Style string for emphasized sections of added lines.
864 ///
865 /// See STYLES section.
866 pub plus_emph_style: String,
867
868 #[clap(
869 long = "plus-empty-line-marker-style",
870 default_value = "normal auto",
871 value_name = "STYLE"
872 )]
873 /// Style string for added empty line marker.
874 ///
875 /// Used only if --plus-style has no background color.
876 pub plus_empty_line_marker_style: String,
877
878 #[clap(
879 long = "plus-non-emph-style",
880 default_value = "plus-style",
881 value_name = "STYLE"
882 )]
883 /// Style string for non-emphasized sections of added lines that have an emphasized section.
884 ///
885 /// See STYLES section.
886 pub plus_non_emph_style: String,
887
888 #[clap(
889 long = "plus-style",
890 default_value = "syntax auto",
891 value_name = "STYLE"
892 )]
893 /// Style string for added lines.
894 ///
895 /// See STYLES section.
896 pub plus_style: String,
897
898 #[clap(long = "raw")]
899 /// Do not alter the input in any way.
900 ///
901 /// This is mainly intended for testing delta.
902 pub raw: bool,
903
904 #[clap(long = "relative-paths")]
905 /// Output all file paths relative to the current directory.
906 ///
907 /// This means that they will resolve correctly when clicked on or used in shell commands.
908 pub relative_paths: bool,
909
910 #[clap(long = "right-arrow", default_value = "⟶ ", value_name = "STRING")]
911 /// Text to display with a changed file path.
912 ///
913 /// For example, a unified diff heading, a rename, or a chmod.
914 pub right_arrow: String,
915
916 #[clap(long = "show-colors")]
917 /// Show available named colors.
918 ///
919 /// In addition to named colors, arbitrary colors can be specified using RGB hex codes. See
920 /// COLORS section.
921 pub show_colors: bool,
922
923 #[clap(long = "show-config")]
924 /// Display the active values for all Delta options.
925 ///
926 /// Style string options are displayed with foreground and background colors. This can be used to
927 /// experiment with colors by combining this option with other options such as --minus-style,
928 /// --zero-style, --plus-style, --light, --dark, etc.
929 pub show_config: bool,
930
931 #[clap(long = "show-syntax-themes")]
932 /// Show example diff for available syntax-highlighting themes.
933 ///
934 /// If diff output is supplied on standard input then this will be used for the demo. For
935 /// example: `git show | delta --show-syntax-themes`.
936 pub show_syntax_themes: bool,
937
938 #[clap(long = "show-themes")]
939 /// Show example diff for available delta themes.
940 ///
941 /// A delta theme is a delta named feature (see --features) that sets either `light` or `dark`.
942 /// See https://github.com/dandavison/delta#custom-color-themes. If diff output is supplied on
943 /// standard input then this will be used for the demo. For example: `git show | delta
944 /// --show-themes`. By default shows dark or light themes only, according to whether delta is in
945 /// dark or light mode (as set by the user or inferred from BAT_THEME). To control the themes
946 /// shown, use --dark or --light, or both, on the command line together with this option.
947 pub show_themes: bool,
948
949 #[clap(short = 's', long = "side-by-side")]
950 /// Display diffs in side-by-side layout.
951 pub side_by_side: bool,
952
953 #[clap(long = "syntax-theme", value_name = "SYNTAX_THEME")]
954 /// The syntax-highlighting theme to use.
955 ///
956 /// Use --show-syntax-themes to demo available themes. Defaults to the value of the BAT_THEME
957 /// environment variable, if that contains a valid theme name. --syntax-theme=none disables all
958 /// syntax highlighting.
959 pub syntax_theme: Option<String>,
960
961 #[clap(long = "tabs", default_value = "4", value_name = "N")]
962 /// The number of spaces to replace tab characters with.
963 ///
964 /// Use --tabs=0 to pass tab characters through directly, but note that in that case delta will
965 /// calculate line widths assuming tabs occupy one character's width on the screen: if your
966 /// terminal renders tabs as more than than one character wide then delta's output will look
967 /// incorrect.
968 pub tab_width: usize,
969
970 #[clap(
971 long = "true-color",
972 default_value = "auto",
973 value_name = "auto|always|never"
974 )]
975 /// Whether to emit 24-bit ("true color") RGB color codes.
976 ///
977 /// Options are auto, always, and never. "auto" means that delta will emit 24-bit color codes if
978 /// the environment variable COLORTERM has the value "truecolor" or "24bit". If your terminal
979 /// application (the application you use to enter commands at a shell prompt) supports 24 bit
980 /// colors, then it probably already sets this environment variable, in which case you don't
981 /// need to do anything.
982 pub true_color: String,
983
984 #[clap(
985 long = "whitespace-error-style",
986 default_value = "auto auto",
987 value_name = "STYLE"
988 )]
989 /// Style string for whitespace errors.
990 ///
991 /// Defaults to color.diff.whitespace if that is set in git config, or else 'magenta reverse'.
992 pub whitespace_error_style: String,
993
994 #[clap(short = 'w', long = "width", value_name = "N")]
995 /// The width of underline/overline decorations.
996 ///
997 /// Examples: "72" (exactly 72 characters), "-2" (auto-detected terminal width minus 2). An
998 /// expression such as "74-2" is also valid (equivalent to 72 but may be useful if the caller
999 /// has a variable holding the value "74"). Use --width=variable to extend decorations and
1000 /// background colors to the end of the text only. Otherwise background colors extend to the
1001 /// full terminal width.
1002 pub width: Option<String>,
1003
1004 #[clap(long = "word-diff-regex", default_value = r"\w+", value_name = "REGEX")]
1005 /// Regular expression defining a 'word' in within-line diff algorithm.
1006 ///
1007 /// The regular expression used to decide what a word is for the within-line highlight
1008 /// algorithm. For less fine-grained matching than the default try --word-diff-regex="\S+"
1009 /// --max-line-distance=1.0 (this is more similar to `git --word-diff`).
1010 pub tokenization_regex: String,
1011
1012 #[clap(long = "wrap-left-symbol", default_value = "↵", value_name = "STRING")]
1013 /// End-of-line wrapped content symbol (left-aligned).
1014 ///
1015 /// Symbol added to the end of a line indicating that the content has been wrapped onto the next
1016 /// line and continues left-aligned.
1017 pub wrap_left_symbol: String,
1018
1019 #[clap(long = "wrap-max-lines", default_value = "2", value_name = "N")]
1020 /// How often a line should be wrapped if it does not fit.
1021 ///
1022 /// Zero means to never wrap. Any content which does not fit after wrapping will be truncated. A
1023 /// value of "unlimited" means a line will be wrapped as many times as required.
1024 pub wrap_max_lines: String,
1025
1026 #[clap(
1027 long = "wrap-right-percent",
1028 default_value = "37.0",
1029 value_name = "PERCENT"
1030 )]
1031 /// Threshold for right-aligning wrapped content.
1032 ///
1033 /// If the length of the remaining wrapped content, as a percentage of width, is less than this
1034 /// quantity it will be right-aligned. Otherwise it will be left-aligned.
1035 pub wrap_right_percent: String,
1036
1037 #[clap(
1038 long = "wrap-right-prefix-symbol",
1039 default_value = "…",
1040 value_name = "STRING"
1041 )]
1042 /// Pre-wrapped content symbol (right-aligned).
1043 ///
1044 /// Symbol displayed before right-aligned wrapped content.
1045 pub wrap_right_prefix_symbol: String,
1046
1047 #[clap(long = "wrap-right-symbol", default_value = "↴", value_name = "STRING")]
1048 /// End-of-line wrapped content symbol (right-aligned).
1049 ///
1050 /// Symbol added to the end of a line indicating that the content has been wrapped onto the next
1051 /// line and continues right-aligned.
1052 pub wrap_right_symbol: String,
1053
1054 #[clap(
1055 long = "zero-style",
1056 default_value = "syntax normal",
1057 value_name = "STYLE"
1058 )]
1059 /// Style string for unchanged lines.
1060 ///
1061 /// See STYLES section.
1062 pub zero_style: String,
1063
1064 #[clap(long = "24-bit-color", value_name = "auto|always|never")]
1065 /// Deprecated: use --true-color.
1066 pub _24_bit_color: Option<String>,
1067
1068 #[clap(parse(from_os_str))]
1069 /// First file to be compared when delta is being used in diff mode
1070 ///
1071 /// `delta file_1 file_2` is equivalent to `diff -u file_1 file_2 | delta`.
1072 pub minus_file: Option<PathBuf>,
1073
1074 #[clap(parse(from_os_str))]
1075 /// Second file to be compared when delta is being used in diff mode.
1076 pub plus_file: Option<PathBuf>,
1077
1078 #[clap(skip)]
1079 pub computed: ComputedValues,
1080
1081 #[clap(skip)]
1082 pub git_config: Option<GitConfig>,
1083
1084 #[clap(skip)]
1085 pub git_config_entries: HashMap<String, GitConfigEntry>,
1086
1087 #[clap(skip)]
1088 pub env: DeltaEnv,
1089}
1090
1091#[derive(Default, Clone, Debug)]
1092pub struct ComputedValues {
1093 pub available_terminal_width: usize,
1094 pub stdout_is_term: bool,
1095 pub background_color_extends_to_terminal_width: bool,
1096 pub decorations_width: Width,
1097 pub inspect_raw_lines: InspectRawLines,
1098 pub is_light_mode: bool,
1099 pub paging_mode: PagingMode,
1100 pub syntax_set: SyntaxSet,
1101 pub syntax_theme: Option<SyntaxTheme>,
1102 pub true_color: bool,
1103}
1104
1105#[derive(Clone, Debug, PartialEq, Eq)]
1106pub enum Width {
1107 Fixed(usize),
1108 Variable,
1109}
1110
1111impl Default for Width {
1112 fn default() -> Self {
1113 Width::Variable
1114 }
1115}
1116
1117#[derive(Clone, Debug, PartialEq, Eq)]
1118pub enum InspectRawLines {
1119 True,
1120 False,
1121}
1122
1123impl Default for InspectRawLines {
1124 fn default() -> Self {
1125 InspectRawLines::False
1126 }
1127}
1128
1129impl Default for PagingMode {
1130 fn default() -> Self {
1131 PagingMode::Never
1132 }
1133}
1134
1135impl Opt {
1136 pub fn from_git_config(
1137 env: DeltaEnv,
1138 git_config: Option<GitConfig>,
1139 assets: HighlightingAssets,
1140 ) -> Self {
1141 let empty_arg: Vec<String> = vec![];
1142 Self::from_clap_and_git_config(
1143 env,
1144 Self::into_app().get_matches_from(empty_arg),
1145 git_config,
1146 assets,
1147 )
1148 }
1149
1150 pub fn from_args_and_git_config(
1151 env: DeltaEnv,
1152 git_config: Option<GitConfig>,
1153 assets: HighlightingAssets,
1154 ) -> Self {
1155 Self::from_clap_and_git_config(env, Self::into_app().get_matches(), git_config, assets)
1156 }
1157
1158 pub fn from_iter_and_git_config<I>(
1159 env: DeltaEnv,
1160 iter: I,
1161 git_config: Option<GitConfig>,
1162 ) -> Self
1163 where
1164 I: IntoIterator,
1165 I::Item: Into<OsString> + Clone,
1166 {
1167 let assets = utils::bat::assets::load_highlighting_assets();
1168 Self::from_clap_and_git_config(
1169 env,
1170 Self::into_app().get_matches_from(iter),
1171 git_config,
1172 assets,
1173 )
1174 }
1175
1176 fn from_clap_and_git_config(
1177 env: DeltaEnv,
1178 arg_matches: clap::ArgMatches,
1179 mut git_config: Option<GitConfig>,
1180 assets: HighlightingAssets,
1181 ) -> Self {
1182 let mut opt = Opt::from_arg_matches(&arg_matches)
1183 .unwrap_or_else(|_| delta_unreachable("Opt::from_arg_matches failed"));
1184 opt.env = env;
1185 options::set::set_options(&mut opt, &mut git_config, &arg_matches, assets);
1186 opt.git_config = git_config;
1187 opt
1188 }
1189
1190 pub fn get_argument_and_option_names<'a>() -> HashMap<&'a str, &'a str> {
1191 itertools::chain(
1192 Self::into_app().get_opts(),
1193 Self::into_app().get_arguments(),
1194 )
1195 .filter_map(|arg| match (arg.get_name(), arg.get_long()) {
1196 (name, Some(long)) => {
1197 if IGNORED_OPTION_NAMES.contains(name) {
1198 None
1199 } else {
1200 Some((name, long))
1201 }
1202 }
1203 _ => None,
1204 })
1205 .collect()
1206 }
1207}
1208
1209// Option names to exclude when listing options to process for various purposes. These are all
1210// pseudo-flag commands such as --list-languages
1211lazy_static! {
1212 static ref IGNORED_OPTION_NAMES: HashSet<&'static str> = vec![
1213 "list-languages",
1214 "list-syntax-themes",
1215 "show-config",
1216 "show-syntax-themes",
1217 ]
1218 .into_iter()
1219 .collect();
1220}