-
-
Notifications
You must be signed in to change notification settings - Fork 607
Greater 50 chars commit highlighting #197
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,6 +8,7 @@ use crate::{ | |
}; | ||
use anyhow::Result; | ||
use crossterm::event::{Event, KeyCode, KeyModifiers}; | ||
use tui::style::Color::Red; | ||
use tui::{ | ||
backend::Backend, | ||
layout::Rect, | ||
|
@@ -16,7 +17,7 @@ use tui::{ | |
Frame, | ||
}; | ||
|
||
/// primarily a subcomponet for user input of text (used in `CommitComponent`) | ||
/// Primarily a sub-component for user input of text (used in `CommitComponent`). | ||
pub struct TextInputComponent { | ||
title: String, | ||
default_msg: String, | ||
|
@@ -104,13 +105,13 @@ impl TextInputComponent { | |
self.title = t; | ||
} | ||
|
||
fn get_draw_text(&self) -> Vec<Text> { | ||
fn apply_cursor(&self) -> Vec<Text> { | ||
let style = self.theme.text(true, false); | ||
|
||
let mut txt = Vec::new(); | ||
|
||
// the portion of the text before the cursor is added | ||
// if the cursor is not at the first character | ||
// The portion of the text before the cursor is added | ||
// if the cursor is not at the first character. | ||
if self.cursor_position > 0 { | ||
txt.push(Text::styled( | ||
&self.msg[..self.cursor_position], | ||
|
@@ -141,8 +142,8 @@ impl TextInputComponent { | |
style.modifier(Modifier::UNDERLINED), | ||
)); | ||
|
||
// the final portion of the text is added if there is | ||
// still remaining characters | ||
// The final portion of the text is added if there is | ||
// still remaining characters. | ||
if let Some(pos) = self.next_char_position() { | ||
if pos < self.msg.len() { | ||
txt.push(Text::styled(&self.msg[pos..], style)); | ||
|
@@ -151,6 +152,25 @@ impl TextInputComponent { | |
|
||
txt | ||
} | ||
|
||
fn apply_highlighting(&self) -> Vec<Text> { | ||
let style = self.theme.text(true, false); | ||
let mut txt = Vec::new(); | ||
let char_limit_warning = 50; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. for constants lets put them up on top outside the type |
||
if self.msg.len() > char_limit_warning { | ||
txt.push(Text::styled( | ||
&self.msg[..char_limit_warning], | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will likely fail if the limit cuts in the middle of a utf8 boundary. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Didn't even consider utf8 - embarrassing considering I have one (á) in my name. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. no worries, happens to me all the time 🙈 |
||
style, | ||
)); | ||
txt.push(Text::styled( | ||
&self.msg[char_limit_warning..], | ||
style.fg(Red), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. colors should be looked up using the theme so people can customise it. in the style we have |
||
)); | ||
} else { | ||
txt.push(Text::styled(&self.msg, style)); | ||
} | ||
txt | ||
} | ||
} | ||
|
||
impl DrawableComponent for TextInputComponent { | ||
|
@@ -166,7 +186,8 @@ impl DrawableComponent for TextInputComponent { | |
self.theme.text(false, false), | ||
)] | ||
} else { | ||
self.get_draw_text() | ||
self.apply_cursor(); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. how come we don't need to use the result of |
||
self.apply_highlighting() | ||
}; | ||
|
||
let area = ui::centered_rect(60, 20, f.size()); | ||
|
@@ -303,7 +324,7 @@ mod tests { | |
|
||
comp.incr_cursor(); | ||
|
||
let txt = comp.get_draw_text(); | ||
let txt = comp.apply_cursor(); | ||
|
||
assert_eq!(txt.len(), 4); | ||
assert_eq!(get_text(&txt[0]), Some("a")); | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think
get_draw_text
is more appropriate for what we want the method to do, if we wanna split the logic it does because it would be too much for one method lets subdivide it. other components already use a similar notation of moving this logic into a sort ofget_text
method.