2020-03-30 19:37:29 +02:00
|
|
|
#[cfg(feature = "git")]
|
2019-10-06 03:44:14 +02:00
|
|
|
use crate::diff::LineChange;
|
2019-03-08 11:48:22 +01:00
|
|
|
use crate::printer::{Colors, InteractivePrinter};
|
2023-03-23 10:47:38 +01:00
|
|
|
use nu_ansi_term::Style;
|
2018-05-14 03:44:07 +02:00
|
|
|
|
2019-10-15 03:25:53 +02:00
|
|
|
#[derive(Debug, Clone)]
|
2020-04-22 18:30:06 +02:00
|
|
|
pub(crate) struct DecorationText {
|
2018-05-14 03:44:07 +02:00
|
|
|
pub width: usize,
|
|
|
|
pub text: String,
|
|
|
|
}
|
|
|
|
|
2020-04-22 18:30:06 +02:00
|
|
|
pub(crate) trait Decoration {
|
2018-08-23 19:43:10 +02:00
|
|
|
fn generate(
|
|
|
|
&self,
|
|
|
|
line_number: usize,
|
|
|
|
continuation: bool,
|
|
|
|
printer: &InteractivePrinter,
|
|
|
|
) -> DecorationText;
|
2018-05-14 03:44:07 +02:00
|
|
|
fn width(&self) -> usize;
|
|
|
|
}
|
|
|
|
|
2020-04-22 18:30:06 +02:00
|
|
|
pub(crate) struct LineNumberDecoration {
|
2018-05-14 03:44:07 +02:00
|
|
|
color: Style,
|
|
|
|
cached_wrap: DecorationText,
|
|
|
|
cached_wrap_invalid_at: usize,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LineNumberDecoration {
|
2020-04-22 18:30:06 +02:00
|
|
|
pub(crate) fn new(colors: &Colors) -> Self {
|
2018-05-14 03:44:07 +02:00
|
|
|
LineNumberDecoration {
|
|
|
|
color: colors.line_number,
|
|
|
|
cached_wrap_invalid_at: 10000,
|
|
|
|
cached_wrap: DecorationText {
|
|
|
|
text: colors.line_number.paint(" ".repeat(4)).to_string(),
|
|
|
|
width: 4,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decoration for LineNumberDecoration {
|
2018-05-15 22:55:38 +02:00
|
|
|
fn generate(
|
|
|
|
&self,
|
|
|
|
line_number: usize,
|
|
|
|
continuation: bool,
|
2018-08-23 19:43:10 +02:00
|
|
|
_printer: &InteractivePrinter,
|
2018-05-15 22:55:38 +02:00
|
|
|
) -> DecorationText {
|
|
|
|
if continuation {
|
2024-02-11 22:49:13 +01:00
|
|
|
if line_number >= self.cached_wrap_invalid_at {
|
2018-05-15 22:55:38 +02:00
|
|
|
let new_width = self.cached_wrap.width + 1;
|
|
|
|
return DecorationText {
|
|
|
|
text: self.color.paint(" ".repeat(new_width)).to_string(),
|
|
|
|
width: new_width,
|
|
|
|
};
|
|
|
|
}
|
2018-05-14 03:44:07 +02:00
|
|
|
|
2018-05-15 22:55:38 +02:00
|
|
|
self.cached_wrap.clone()
|
|
|
|
} else {
|
2024-02-24 22:36:14 +01:00
|
|
|
let plain: String = format!("{line_number:4}");
|
2018-05-15 22:55:38 +02:00
|
|
|
DecorationText {
|
|
|
|
width: plain.len(),
|
|
|
|
text: self.color.paint(plain).to_string(),
|
|
|
|
}
|
2018-05-14 03:44:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn width(&self) -> usize {
|
|
|
|
4
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 19:37:29 +02:00
|
|
|
#[cfg(feature = "git")]
|
2020-04-22 18:30:06 +02:00
|
|
|
pub(crate) struct LineChangesDecoration {
|
2018-05-14 03:44:07 +02:00
|
|
|
cached_none: DecorationText,
|
|
|
|
cached_added: DecorationText,
|
|
|
|
cached_removed_above: DecorationText,
|
|
|
|
cached_removed_below: DecorationText,
|
|
|
|
cached_modified: DecorationText,
|
|
|
|
}
|
|
|
|
|
2020-03-30 19:37:29 +02:00
|
|
|
#[cfg(feature = "git")]
|
2018-05-14 03:44:07 +02:00
|
|
|
impl LineChangesDecoration {
|
|
|
|
#[inline]
|
|
|
|
fn generate_cached(style: Style, text: &str) -> DecorationText {
|
|
|
|
DecorationText {
|
|
|
|
text: style.paint(text).to_string(),
|
|
|
|
width: text.chars().count(),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 18:30:06 +02:00
|
|
|
pub(crate) fn new(colors: &Colors) -> Self {
|
2018-05-14 03:44:07 +02:00
|
|
|
LineChangesDecoration {
|
|
|
|
cached_none: Self::generate_cached(Style::default(), " "),
|
|
|
|
cached_added: Self::generate_cached(colors.git_added, "+"),
|
|
|
|
cached_removed_above: Self::generate_cached(colors.git_removed, "‾"),
|
|
|
|
cached_removed_below: Self::generate_cached(colors.git_removed, "_"),
|
|
|
|
cached_modified: Self::generate_cached(colors.git_modified, "~"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-03-30 19:37:29 +02:00
|
|
|
#[cfg(feature = "git")]
|
2018-05-14 03:44:07 +02:00
|
|
|
impl Decoration for LineChangesDecoration {
|
2018-05-15 22:55:38 +02:00
|
|
|
fn generate(
|
|
|
|
&self,
|
|
|
|
line_number: usize,
|
|
|
|
continuation: bool,
|
2018-08-23 19:43:10 +02:00
|
|
|
printer: &InteractivePrinter,
|
2018-05-15 22:55:38 +02:00
|
|
|
) -> DecorationText {
|
|
|
|
if !continuation {
|
|
|
|
if let Some(ref changes) = printer.line_changes {
|
|
|
|
return match changes.get(&(line_number as u32)) {
|
|
|
|
Some(&LineChange::Added) => self.cached_added.clone(),
|
|
|
|
Some(&LineChange::RemovedAbove) => self.cached_removed_above.clone(),
|
|
|
|
Some(&LineChange::RemovedBelow) => self.cached_removed_below.clone(),
|
|
|
|
Some(&LineChange::Modified) => self.cached_modified.clone(),
|
|
|
|
_ => self.cached_none.clone(),
|
|
|
|
};
|
2018-05-14 03:44:07 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
self.cached_none.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn width(&self) -> usize {
|
|
|
|
self.cached_none.width
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-22 18:30:06 +02:00
|
|
|
pub(crate) struct GridBorderDecoration {
|
2018-05-14 03:44:07 +02:00
|
|
|
cached: DecorationText,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl GridBorderDecoration {
|
2020-04-22 18:30:06 +02:00
|
|
|
pub(crate) fn new(colors: &Colors) -> Self {
|
2018-05-14 03:44:07 +02:00
|
|
|
GridBorderDecoration {
|
|
|
|
cached: DecorationText {
|
|
|
|
text: colors.grid.paint("│").to_string(),
|
|
|
|
width: 1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Decoration for GridBorderDecoration {
|
2018-05-15 22:55:38 +02:00
|
|
|
fn generate(
|
|
|
|
&self,
|
|
|
|
_line_number: usize,
|
|
|
|
_continuation: bool,
|
2018-08-23 19:43:10 +02:00
|
|
|
_printer: &InteractivePrinter,
|
2018-05-15 22:55:38 +02:00
|
|
|
) -> DecorationText {
|
2018-05-14 03:44:07 +02:00
|
|
|
self.cached.clone()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn width(&self) -> usize {
|
|
|
|
self.cached.width
|
|
|
|
}
|
|
|
|
}
|