on master help sign in
Replace for_line and for_wrap with generate
commitd0ca5669794902a78eebb14b2ca61b01a67c9ee6 authoreth-p <32112321+eth-p@users.noreply.github.com> dateTue 15 May 2018, 13:55 (8 years ago) parenta5a7dc1
2 files changed+46−38
src/decorations.rs+44−36 src/printer.rs+2−2
src/decorations.rs+44−36
@@ -10,8 +10,8 @@ pub struct DecorationText {
1010}
1111
1212pub trait Decoration {
13− fn for_line(&self, line_number: usize, printer: &Printer) -> DecorationText;
14− fn for_wrap(&self, line_number: usize, printer: &Printer) -> DecorationText;
13+ fn generate(&self, line_number: usize, continuation: bool, printer: &Printer)
14+ -> DecorationText;
1515 fn width(&self) -> usize;
1616}
1717
@@ -36,24 +36,29 @@ impl LineNumberDecoration {
3636}
3737
3838impl Decoration for LineNumberDecoration {
39− fn for_line(&self, line_number: usize, _printer: &Printer) -> DecorationText {
40− let plain: String = format!("{:4}", line_number);
41− DecorationText {
42− width: plain.len(),
43− text: self.color.paint(plain).to_string(),
44− }
45− }
39+ fn generate(
40+ &self,
41+ line_number: usize,
42+ continuation: bool,
43+ _printer: &Printer,
44+ ) -> DecorationText {
45+ if continuation {
46+ if line_number > self.cached_wrap_invalid_at {
47+ let new_width = self.cached_wrap.width + 1;
48+ return DecorationText {
49+ text: self.color.paint(" ".repeat(new_width)).to_string(),
50+ width: new_width,
51+ };
52+ }
4653
47− fn for_wrap(&self, line_number: usize, _printer: &Printer) -> DecorationText {
48− if line_number > self.cached_wrap_invalid_at {
49− let new_width = self.cached_wrap.width + 1;
50− return DecorationText {
51− text: self.color.paint(" ".repeat(new_width)).to_string(),
52− width: new_width,
53− };
54+ self.cached_wrap.clone()
55+ } else {
56+ let plain: String = format!("{:4}", line_number);
57+ DecorationText {
58+ width: plain.len(),
59+ text: self.color.paint(plain).to_string(),
60+ }
5461 }
55−
56− self.cached_wrap.clone()
5762 }
5863
5964 fn width(&self) -> usize {
@@ -91,22 +96,24 @@ impl LineChangesDecoration {
9196}
9297
9398impl Decoration for LineChangesDecoration {
94− fn for_line(&self, line_number: usize, printer: &Printer) -> DecorationText {
95− if let Some(ref changes) = printer.line_changes {
96− match changes.get(&(line_number as u32)) {
97− Some(&LineChange::Added) => self.cached_added.clone(),
98− Some(&LineChange::RemovedAbove) => self.cached_removed_above.clone(),
99− Some(&LineChange::RemovedBelow) => self.cached_removed_below.clone(),
100− Some(&LineChange::Modified) => self.cached_modified.clone(),
101− _ => self.cached_none.clone(),
99+ fn generate(
100+ &self,
101+ line_number: usize,
102+ continuation: bool,
103+ printer: &Printer,
104+ ) -> DecorationText {
105+ if !continuation {
106+ if let Some(ref changes) = printer.line_changes {
107+ return match changes.get(&(line_number as u32)) {
108+ Some(&LineChange::Added) => self.cached_added.clone(),
109+ Some(&LineChange::RemovedAbove) => self.cached_removed_above.clone(),
110+ Some(&LineChange::RemovedBelow) => self.cached_removed_below.clone(),
111+ Some(&LineChange::Modified) => self.cached_modified.clone(),
112+ _ => self.cached_none.clone(),
113+ };
102114 }
103− } else {
104− self.cached_none.clone()
105115 }
106− // let status = printer.line_changes.and_then(|ref changes| changes.get(&(line_number as u32)));
107− }
108116
109− fn for_wrap(&self, _line_number: usize, _printer: &Printer) -> DecorationText {
110117 self.cached_none.clone()
111118 }
112119
@@ -132,11 +139,12 @@ impl GridBorderDecoration {
132139}
133140
134141impl Decoration for GridBorderDecoration {
135− fn for_line(&self, _line_number: usize, _printer: &Printer) -> DecorationText {
136− self.cached.clone()
137− }
138−
139− fn for_wrap(&self, _line_number: usize, _printer: &Printer) -> DecorationText {
142+ fn generate(
143+ &self,
144+ _line_number: usize,
145+ _continuation: bool,
146+ _printer: &Printer,
147+ ) -> DecorationText {
140148 self.cached.clone()
141149 }
142150
src/printer.rs+2−2
@@ -112,7 +112,7 @@ impl<'a> Printer<'a> {
112112 if self.panel_width > 0 {
113113 let decorations = self.decorations
114114 .iter()
115− .map(|ref d| d.for_line(line_number, self))
115+ .map(|ref d| d.generate(line_number, false, self))
116116 .collect::<Vec<_>>();
117117
118118 for deco in decorations {
@@ -174,7 +174,7 @@ impl<'a> Printer<'a> {
174174 "{} ",
175175 self.decorations
176176 .iter()
177− .map(|ref d| d.for_wrap(line_number, self).text)
177+ .map(|ref d| d.generate(line_number, true, self).text)
178178 .collect::<Vec<String>>()
179179 .join(" ")
180180 ))