summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorSainath Singineedi <44405294+sainad2222@users.noreply.github.com>2024-02-21 04:39:53 +0530
committerGitHub <noreply@github.com>2024-02-21 00:09:53 +0100
commitfa051eff5aadb333b28fa0f80e0d79b3e43e2815 (patch)
tree42aaa5914ebc010a0e767886ef009874c2c88647 /src
parenta50a478c1a306a070c03898e5a2ec4d7d5567290 (diff)
feat: add branch name validation on renaming (#2081)
Closes #2062 --------- Co-authored-by: extrawurst <776816+extrawurst@users.noreply.github.com>
Diffstat (limited to 'src')
-rw-r--r--src/popups/rename_branch.rs43
1 files changed, 40 insertions, 3 deletions
diff --git a/src/popups/rename_branch.rs b/src/popups/rename_branch.rs
index d8243417..bf8e7974 100644
--- a/src/popups/rename_branch.rs
+++ b/src/popups/rename_branch.rs
@@ -2,6 +2,7 @@ use crate::components::{
visibility_blocking, CommandBlocking, CommandInfo, Component,
DrawableComponent, EventState, InputType, TextInputComponent,
};
+use crate::ui::style::SharedTheme;
use crate::{
app::Environment,
keys::{key_match, SharedKeyConfig},
@@ -11,7 +12,8 @@ use crate::{
use anyhow::Result;
use asyncgit::sync::{self, RepoPathRef};
use crossterm::event::Event;
-use ratatui::{layout::Rect, Frame};
+use easy_cast::Cast;
+use ratatui::{layout::Rect, widgets::Paragraph, Frame};
pub struct RenameBranchPopup {
repo: RepoPathRef,
@@ -19,12 +21,15 @@ pub struct RenameBranchPopup {
branch_ref: Option<String>,
queue: Queue,
key_config: SharedKeyConfig,
+ theme: SharedTheme,
}
impl DrawableComponent for RenameBranchPopup {
fn draw(&self, f: &mut Frame, rect: Rect) -> Result<()> {
- self.input.draw(f, rect)?;
-
+ if self.is_visible() {
+ self.input.draw(f, rect)?;
+ self.draw_warnings(f);
+ }
Ok(())
}
}
@@ -97,6 +102,7 @@ impl RenameBranchPopup {
.with_input_type(InputType::Singleline),
branch_ref: None,
key_config: env.key_config.clone(),
+ theme: env.theme.clone(),
}
}
@@ -148,4 +154,35 @@ impl RenameBranchPopup {
self.input.clear();
}
+
+ fn draw_warnings(&self, f: &mut Frame) {
+ let current_text = self.input.get_text();
+
+ if !current_text.is_empty() {
+ let valid = sync::validate_branch_name(current_text)
+ .unwrap_or_default();
+
+ if !valid {
+ let msg = strings::branch_name_invalid();
+ 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);
+ }
+ }
+ }
}