feat(tui): add rename functionality
This commit is contained in:
1 parent
9e65fb43d8
commit
df3d6b24c2
4 files changed
+98
-4
No files matched your search
@@ -271,6 +271,37 @@ impl InstanceManager {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn rename(&self, old_name: &str, new_name: &str) -> Result<(), InstanceError> {
|
||||
let new_name = new_name.trim();
|
||||
if new_name.is_empty() {
|
||||
return Err(InstanceError::InvalidName("Name cannot be empty".to_string()));
|
||||
}
|
||||
if old_name == new_name {
|
||||
return Ok(());
|
||||
}
|
||||
let old_dir = self.instances_dir.join(old_name);
|
||||
let new_dir = self.instances_dir.join(new_name);
|
||||
if !old_dir.exists() {
|
||||
return Err(InstanceError::NotFound(old_name.to_string()));
|
||||
}
|
||||
if new_dir.exists() {
|
||||
return Err(InstanceError::AlreadyExists(new_name.to_string()));
|
||||
}
|
||||
std::fs::rename(&old_dir, &new_dir)?;
|
||||
|
||||
let config_path = new_dir.join("instance.json");
|
||||
if let Ok(data) = std::fs::read_to_string(&config_path) {
|
||||
if let Ok(mut config) = serde_json::from_str::<InstanceConfig>(&data) {
|
||||
config.name = new_name.to_string();
|
||||
if let Ok(json) = serde_json::to_string_pretty(&config) {
|
||||
let _ = std::fs::write(&config_path, json);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn load_all(&self) -> Vec<InstanceConfig> {
|
||||
let mut instances = vec![];
|
||||
let read_dir = match std::fs::read_dir(&self.instances_dir) {
|
||||
|
||||
@@ -392,6 +392,42 @@ impl App {
|
||||
new_instance::handle_key(&key_event, &mut self.profiles_state);
|
||||
}
|
||||
_ => {
|
||||
if self.focused == FocusedArea::Profiles && self.profiles_state.renaming.is_some() {
|
||||
match key_event.code {
|
||||
KeyCode::Enter => {
|
||||
let new_name = self.profiles_state.renaming.take().unwrap_or_default();
|
||||
if let Some(inst) = self.profiles_state.selected_instance() {
|
||||
let old_name = inst.name.clone();
|
||||
match self.instance_manager.rename(&old_name, &new_name) {
|
||||
Ok(()) => {
|
||||
if let Some(inst) = self.profiles_state.instances.iter_mut().find(|i| i.name == old_name) {
|
||||
inst.name = new_name.trim().to_string();
|
||||
}
|
||||
}
|
||||
Err(e) => {
|
||||
tracing::error!("Rename failed: {}", e);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
KeyCode::Esc => {
|
||||
self.profiles_state.renaming = None;
|
||||
}
|
||||
KeyCode::Backspace => {
|
||||
if let Some(ref mut name) = self.profiles_state.renaming {
|
||||
name.pop();
|
||||
}
|
||||
}
|
||||
KeyCode::Char(c) => {
|
||||
if let Some(ref mut name) = self.profiles_state.renaming {
|
||||
name.push(c);
|
||||
}
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
if self.focused == FocusedArea::Profiles && self.profiles_state.search.active {
|
||||
self.profiles_state.handle_key(&key_event);
|
||||
return Ok(());
|
||||
@@ -468,6 +504,14 @@ impl App {
|
||||
}
|
||||
}
|
||||
}
|
||||
KeyCode::Char('r')
|
||||
if self.focused == FocusedArea::Profiles
|
||||
&& !self.profiles_state.search.active =>
|
||||
{
|
||||
if let Some(inst) = self.profiles_state.selected_instance() {
|
||||
self.profiles_state.renaming = Some(inst.name.clone());
|
||||
}
|
||||
}
|
||||
KeyCode::Esc
|
||||
if self.focused == FocusedArea::Profiles
|
||||
&& !self.profiles_state.search.active =>
|
||||
|
||||
@@ -186,6 +186,7 @@ pub fn render(
|
||||
("Esc", " kill"),
|
||||
("a", " add"),
|
||||
("d", " delete"),
|
||||
("r", " rename"),
|
||||
("/", " search"),
|
||||
])
|
||||
} else {
|
||||
|
||||
@@ -51,6 +51,7 @@ pub struct State {
|
||||
pub scrollbar_state: ScrollbarState,
|
||||
pub show_popup: bool,
|
||||
pub search: SearchState,
|
||||
pub renaming: Option<String>,
|
||||
}
|
||||
|
||||
impl State {
|
||||
@@ -62,6 +63,7 @@ impl State {
|
||||
scrollbar_state: ScrollbarState::default(),
|
||||
show_popup: false,
|
||||
search: SearchState::default(),
|
||||
renaming: None,
|
||||
};
|
||||
if count > 0 {
|
||||
s.list_state.selected = Some(0);
|
||||
@@ -249,10 +251,26 @@ pub fn render(frame: &mut Frame, area: Rect, focused: FocusedArea, state: &mut S
|
||||
"\u{258c} ",
|
||||
Style::default().fg(loader_color(instance.loader)),
|
||||
);
|
||||
let name_line = Line::from(vec![
|
||||
stripe.clone(),
|
||||
Span::styled(instance.name.as_str(), name_style),
|
||||
]);
|
||||
|
||||
let is_renaming = context.is_selected && state.renaming.is_some();
|
||||
let name_line = if is_renaming {
|
||||
let rename_val = state.renaming.as_deref().unwrap_or("");
|
||||
Line::from(vec![
|
||||
stripe.clone(),
|
||||
Span::styled(rename_val, Style::default().fg(THEME.colors.foreground)),
|
||||
Span::styled(
|
||||
"\u{2588}",
|
||||
Style::default()
|
||||
.fg(THEME.colors.border_focused)
|
||||
.add_modifier(Modifier::SLOW_BLINK),
|
||||
),
|
||||
])
|
||||
} else {
|
||||
Line::from(vec![
|
||||
stripe.clone(),
|
||||
Span::styled(instance.name.as_str(), name_style),
|
||||
])
|
||||
};
|
||||
|
||||
let (meta_text, meta_text_style) = match get_run_state(&instance.name) {
|
||||
Some(RunState::Authenticating) => (
|
||||
|
||||
Reference in new issue
Block a user