Expose a proper `--display` CLI option

This helps with discovery of the different display options available,
such as inline (see #34).
pull/230/head
Wilfred Hughes 2022-04-01 22:11:54 +07:00
parent e9992a60f4
commit 97b9a13d15
4 changed files with 78 additions and 29 deletions

@ -1,5 +1,11 @@
## 0.26 (unreleased)
### Command Line Interface
Added the `--display` option to switch between `side-by-side`,
`side-by-side-show-both`, and `inline` display modes. This replaces
the `INLINE` and `DFT_SHOW_BOTH` environment variables.
## 0.25 (released 31st March 2022)
### Display

@ -50,7 +50,7 @@ use mimalloc::MiMalloc;
#[global_allocator]
static GLOBAL: MiMalloc = MiMalloc;
use options::{should_use_color, Mode};
use options::{should_use_color, DisplayMode, Mode};
use sliders::fix_all_sliders;
use std::{env, path::Path};
use style::BackgroundColor;
@ -126,6 +126,7 @@ fn main() {
byte_limit,
print_unchanged,
missing_as_empty,
display_mode,
background_color,
color_output,
display_width,
@ -157,6 +158,7 @@ fn main() {
print_diff_result(
display_width,
use_color,
display_mode,
background_color,
print_unchanged,
&diff_result,
@ -174,6 +176,7 @@ fn main() {
print_diff_result(
display_width,
use_color,
display_mode,
background_color,
print_unchanged,
&diff_result,
@ -370,6 +373,7 @@ fn diff_directories<'a>(
fn print_diff_result(
display_width: usize,
use_color: bool,
display_mode: DisplayMode,
background: BackgroundColor,
print_unchanged: bool,
summary: &DiffResult,
@ -406,31 +410,35 @@ fn print_diff_result(
return;
}
if env::var("INLINE").is_ok() {
inline::print(
lhs_src,
rhs_src,
&summary.lhs_positions,
&summary.rhs_positions,
&hunks,
&summary.path,
&lang_name,
use_color,
background,
);
} else {
side_by_side::print(
&hunks,
display_width,
use_color,
background,
&summary.path,
&lang_name,
lhs_src,
rhs_src,
&summary.lhs_positions,
&summary.rhs_positions,
);
match display_mode {
DisplayMode::Inline => {
inline::print(
lhs_src,
rhs_src,
&summary.lhs_positions,
&summary.rhs_positions,
&hunks,
&summary.path,
&lang_name,
use_color,
background,
);
}
DisplayMode::SideBySide | DisplayMode::SideBySideShowBoth => {
side_by_side::print(
&hunks,
display_width,
use_color,
display_mode,
background,
&summary.path,
&lang_name,
lhs_src,
rhs_src,
&summary.lhs_positions,
&summary.rhs_positions,
);
}
}
}
(FileContent::Binary(lhs_bytes), FileContent::Binary(rhs_bytes)) => {

@ -1,4 +1,4 @@
use std::env;
use std::{borrow::Borrow, env};
use atty::Stream;
use clap::{crate_authors, crate_description, crate_version, App, AppSettings, Arg};
@ -63,6 +63,12 @@ fn app() -> clap::App<'static> {
.validator(|s| s.parse::<usize>())
.required(false),
)
.arg(
Arg::new("display").long("display")
.possible_values(["side-by-side", "side-by-side-show-both", "inline", ])
.value_name("MODE")
.help("Display mode for showing results. Overrides $DFT_DISPLAY if present.")
)
.arg(
Arg::new("color").long("color")
.possible_values(["always", "auto", "never"])
@ -111,12 +117,20 @@ fn app() -> clap::App<'static> {
.setting(AppSettings::ArgRequiredElseHelp)
}
#[derive(Debug, Copy, Clone)]
pub enum DisplayMode {
Inline,
SideBySide,
SideBySideShowBoth,
}
pub enum Mode {
Diff {
node_limit: u32,
byte_limit: usize,
print_unchanged: bool,
missing_as_empty: bool,
display_mode: DisplayMode,
background_color: BackgroundColor,
color_output: ColorOutput,
display_width: usize,
@ -205,6 +219,22 @@ pub fn parse_args() -> Mode {
env_width.unwrap_or_else(detect_display_width)
};
let display_mode_str = if let Some(display_mode_str) = matches.value_of("display") {
display_mode_str.to_owned()
} else {
env::var("DFT_DISPLAY").unwrap_or_else(|_| "".to_owned())
};
let display_mode = match display_mode_str.borrow() {
"side-by-side" => DisplayMode::SideBySide,
"side-by-side-show-both" => DisplayMode::SideBySideShowBoth,
"inline" => DisplayMode::Inline,
_ => {
// The CLI validates values, but environment variables can
// be any string.
DisplayMode::SideBySide
}
};
let color_output = if let Some(color_when) = matches.value_of("color") {
if color_when == "always" {
ColorOutput::Always
@ -274,6 +304,7 @@ pub fn parse_args() -> Mode {
byte_limit,
print_unchanged,
missing_as_empty,
display_mode,
background_color,
color_output,
display_width,

@ -10,6 +10,7 @@ use crate::{
context::all_matched_lines_filled,
hunks::{matched_lines_for_hunk, Hunk},
lines::{codepoint_len, format_line_num, LineNumber},
options::DisplayMode,
positions::SingleLineSpan,
style::{self, apply_colors, color_positions, novel_style, split_and_apply, BackgroundColor},
syntax::{zip_pad_shorter, MatchedPos},
@ -302,6 +303,7 @@ pub fn print(
hunks: &[Hunk],
display_width: usize,
use_color: bool,
display_mode: DisplayMode,
background: BackgroundColor,
display_path: &str,
lang_name: &str,
@ -413,7 +415,8 @@ pub fn print(
prev_rhs_line_num,
);
if no_lhs_changes && !std::env::var("DFT_SHOW_BOTH").is_ok() {
let show_both = matches!(display_mode, DisplayMode::SideBySideShowBoth);
if no_lhs_changes && !show_both {
match rhs_line_num {
Some(rhs_line_num) => {
let rhs_line = &rhs_colored_lines[rhs_line_num.0];
@ -433,7 +436,7 @@ pub fn print(
println!("{}{}", display_rhs_line_num, display_rhs_line_num);
}
}
} else if no_rhs_changes && !std::env::var("DFT_SHOW_BOTH").is_ok() {
} else if no_rhs_changes && !show_both {
match lhs_line_num {
Some(lhs_line_num) => {
let lhs_line = &lhs_colored_lines[lhs_line_num.0];
@ -686,6 +689,7 @@ mod tests {
&hunks,
80,
true,
DisplayMode::SideBySide,
BackgroundColor::Dark,
"foo.el",
"Emacs Lisp",