-
-
Notifications
You must be signed in to change notification settings - Fork 609
[wip] check validity of branch name #759
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
24bce3b
b18af7a
5733d51
f5854a8
937123b
d46c614
9ac600c
a7d462c
d87138d
8878c69
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,12 +12,18 @@ use crate::{ | |
use anyhow::Result; | ||
use asyncgit::{sync, CWD}; | ||
use crossterm::event::Event; | ||
use tui::{backend::Backend, layout::Rect, Frame}; | ||
use easy_cast::Cast; | ||
use tui::{ | ||
backend::Backend, layout::Rect, widgets::Paragraph, Frame, | ||
}; | ||
|
||
pub struct CreateBranchComponent { | ||
input: TextInputComponent, | ||
queue: Queue, | ||
key_config: SharedKeyConfig, | ||
theme: SharedTheme, | ||
invalid_name: bool, | ||
already_exists: bool, | ||
} | ||
|
||
impl DrawableComponent for CreateBranchComponent { | ||
|
@@ -27,6 +33,7 @@ impl DrawableComponent for CreateBranchComponent { | |
rect: Rect, | ||
) -> Result<()> { | ||
self.input.draw(f, rect)?; | ||
self.draw_warnings(f); | ||
|
||
Ok(()) | ||
} | ||
|
@@ -60,8 +67,9 @@ impl Component for CreateBranchComponent { | |
} | ||
|
||
if let Event::Key(e) = ev { | ||
self.validate_input()?; | ||
if e == self.key_config.enter { | ||
self.create_branch(); | ||
self.create_branch() | ||
} | ||
|
||
return Ok(EventState::Consumed); | ||
|
@@ -95,13 +103,16 @@ impl CreateBranchComponent { | |
Self { | ||
queue, | ||
input: TextInputComponent::new( | ||
theme, | ||
theme.clone(), | ||
key_config.clone(), | ||
&strings::create_branch_popup_title(&key_config), | ||
&strings::create_branch_popup_msg(&key_config), | ||
true, | ||
), | ||
key_config, | ||
theme, | ||
invalid_name: false, | ||
already_exists: false, | ||
} | ||
} | ||
|
||
|
@@ -137,4 +148,52 @@ impl CreateBranchComponent { | |
} | ||
} | ||
} | ||
|
||
fn validate_input(&mut self) -> Result<()> { | ||
let branch_name = self.input.get_text().as_str(); | ||
|
||
self.already_exists = false; | ||
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. since we only show either 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. ah, makes sense! will do. thanks! |
||
self.invalid_name = false; | ||
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 assignment is useless |
||
|
||
let branches = sync::get_branches_info(CWD, true)?; | ||
for branch in &branches { | ||
if branch.name == self.input.get_text().as_str() { | ||
self.already_exists = true; | ||
} | ||
} | ||
self.invalid_name = sync::branch_name_is_valid(branch_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. this cannot work, this is inverted |
||
|
||
Ok(()) | ||
} | ||
|
||
fn draw_warnings<B: Backend>(&self, f: &mut Frame<B>) { | ||
let branch_name = self.input.get_text().as_str(); | ||
|
||
let msg; | ||
if branch_name.is_empty() { | ||
return; | ||
} else if self.invalid_name { | ||
msg = strings::branch_invalid_name_warning(); | ||
} else if self.already_exists { | ||
msg = strings::branch_already_exists(); | ||
} else { | ||
return; | ||
} | ||
|
||
let msg_length: u16 = msg.len().cast(); | ||
let w = Paragraph::new(msg).style(self.theme.text_danger()); | ||
|
||
let rect = { | ||
let mut rect = self.input.get_area(); | ||
rect.y += rect.height.saturating_sub(1); | ||
rect.height = 1; | ||
let offset = rect.width.saturating_sub(msg_length + 1); | ||
rect.width = rect.width.saturating_sub(offset + 1); | ||
rect.x += offset; | ||
|
||
rect | ||
}; | ||
|
||
f.render_widget(w, rect); | ||
} | ||
} |
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.
does this even work? IMO every key consumed by
self.input
will not even go here