Replace for_line and for_wrap with generate

pull/102/head
eth-p 6 years ago
parent a5a7dc14e4
commit d0ca566979
No known key found for this signature in database
GPG Key ID: 1F8DF8091CD46FBC

@ -10,8 +10,8 @@ pub struct DecorationText {
} }
pub trait Decoration { pub trait Decoration {
fn for_line(&self, line_number: usize, printer: &Printer) -> DecorationText; fn generate(&self, line_number: usize, continuation: bool, printer: &Printer)
fn for_wrap(&self, line_number: usize, printer: &Printer) -> DecorationText; -> DecorationText;
fn width(&self) -> usize; fn width(&self) -> usize;
} }
@ -36,15 +36,13 @@ impl LineNumberDecoration {
} }
impl Decoration for LineNumberDecoration { impl Decoration for LineNumberDecoration {
fn for_line(&self, line_number: usize, _printer: &Printer) -> DecorationText { fn generate(
let plain: String = format!("{:4}", line_number); &self,
DecorationText { line_number: usize,
width: plain.len(), continuation: bool,
text: self.color.paint(plain).to_string(), _printer: &Printer,
} ) -> DecorationText {
} if continuation {
fn for_wrap(&self, line_number: usize, _printer: &Printer) -> DecorationText {
if line_number > self.cached_wrap_invalid_at { if line_number > self.cached_wrap_invalid_at {
let new_width = self.cached_wrap.width + 1; let new_width = self.cached_wrap.width + 1;
return DecorationText { return DecorationText {
@ -54,6 +52,13 @@ impl Decoration for LineNumberDecoration {
} }
self.cached_wrap.clone() self.cached_wrap.clone()
} else {
let plain: String = format!("{:4}", line_number);
DecorationText {
width: plain.len(),
text: self.color.paint(plain).to_string(),
}
}
} }
fn width(&self) -> usize { fn width(&self) -> usize {
@ -91,22 +96,24 @@ impl LineChangesDecoration {
} }
impl Decoration for LineChangesDecoration { impl Decoration for LineChangesDecoration {
fn for_line(&self, line_number: usize, printer: &Printer) -> DecorationText { fn generate(
&self,
line_number: usize,
continuation: bool,
printer: &Printer,
) -> DecorationText {
if !continuation {
if let Some(ref changes) = printer.line_changes { if let Some(ref changes) = printer.line_changes {
match changes.get(&(line_number as u32)) { return match changes.get(&(line_number as u32)) {
Some(&LineChange::Added) => self.cached_added.clone(), Some(&LineChange::Added) => self.cached_added.clone(),
Some(&LineChange::RemovedAbove) => self.cached_removed_above.clone(), Some(&LineChange::RemovedAbove) => self.cached_removed_above.clone(),
Some(&LineChange::RemovedBelow) => self.cached_removed_below.clone(), Some(&LineChange::RemovedBelow) => self.cached_removed_below.clone(),
Some(&LineChange::Modified) => self.cached_modified.clone(), Some(&LineChange::Modified) => self.cached_modified.clone(),
_ => self.cached_none.clone(), _ => self.cached_none.clone(),
};
} }
} else {
self.cached_none.clone()
}
// let status = printer.line_changes.and_then(|ref changes| changes.get(&(line_number as u32)));
} }
fn for_wrap(&self, _line_number: usize, _printer: &Printer) -> DecorationText {
self.cached_none.clone() self.cached_none.clone()
} }
@ -132,11 +139,12 @@ impl GridBorderDecoration {
} }
impl Decoration for GridBorderDecoration { impl Decoration for GridBorderDecoration {
fn for_line(&self, _line_number: usize, _printer: &Printer) -> DecorationText { fn generate(
self.cached.clone() &self,
} _line_number: usize,
_continuation: bool,
fn for_wrap(&self, _line_number: usize, _printer: &Printer) -> DecorationText { _printer: &Printer,
) -> DecorationText {
self.cached.clone() self.cached.clone()
} }

@ -112,7 +112,7 @@ impl<'a> Printer<'a> {
if self.panel_width > 0 { if self.panel_width > 0 {
let decorations = self.decorations let decorations = self.decorations
.iter() .iter()
.map(|ref d| d.for_line(line_number, self)) .map(|ref d| d.generate(line_number, false, self))
.collect::<Vec<_>>(); .collect::<Vec<_>>();
for deco in decorations { for deco in decorations {
@ -174,7 +174,7 @@ impl<'a> Printer<'a> {
"{} ", "{} ",
self.decorations self.decorations
.iter() .iter()
.map(|ref d| d.for_wrap(line_number, self).text) .map(|ref d| d.generate(line_number, true, self).text)
.collect::<Vec<String>>() .collect::<Vec<String>>()
.join(" ") .join(" ")
)) ))

Loading…
Cancel
Save