Skip to content

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

Closed
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 29 additions & 8 deletions src/components/textinput.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -104,13 +105,13 @@ impl TextInputComponent {
self.title = t;
}

fn get_draw_text(&self) -> Vec<Text> {
Copy link
Collaborator

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 of get_text method.

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],
Expand Down Expand Up @@ -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));
Expand All @@ -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;
Copy link
Collaborator

Choose a reason for hiding this comment

The 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],
Copy link
Collaborator

Choose a reason for hiding this comment

The 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.

Copy link
Author

Choose a reason for hiding this comment

The 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.

Copy link
Collaborator

Choose a reason for hiding this comment

The 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),
Copy link
Collaborator

Choose a reason for hiding this comment

The 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 text_danger already which likely is what u are looking for

));
} else {
txt.push(Text::styled(&self.msg, style));
}
txt
}
}

impl DrawableComponent for TextInputComponent {
Expand All @@ -166,7 +186,8 @@ impl DrawableComponent for TextInputComponent {
self.theme.text(false, false),
)]
} else {
self.get_draw_text()
self.apply_cursor();
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

how come we don't need to use the result of apply_cursor?

self.apply_highlighting()
};

let area = ui::centered_rect(60, 20, f.size());
Expand Down Expand Up @@ -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"));
Expand Down