use crate::app::CustomState; pub fn build_commands(state: &CustomState) -> Vec { let command = state.command.trim(); if command.is_empty() { return Vec::new(); } let selected = state.selected_lines(); if selected.is_empty() { return Vec::new(); } selected .iter() .map(|line| { let line = line.trim(); if command.contains("{}") { command.replace("{}", line) } else { format!("{} {}", line, command) } }) .collect() } #[cfg(test)] mod tests { use super::*; use crate::app::CustomState; fn make_state(lines: Vec, selected: Vec, command: &str) -> CustomState { CustomState { lines, selected, cursor: 0, command: command.to_string(), } } #[test] fn test_build_commands_append() { let state = make_state( vec![ "ssh root@ip1".to_string(), "ssh root@ip2".to_string(), "ssh root@ip3".to_string(), ], vec![false, true, true], "lsblk", ); assert_eq!( build_commands(&state), vec!["ssh root@ip2 lsblk", "ssh root@ip3 lsblk"] ); } #[test] fn test_build_commands_placeholder() { let state = make_state( vec![ "root@ip1".to_string(), "root@ip2".to_string(), ], vec![true, true], "ssh {} lsblk", ); assert_eq!( build_commands(&state), vec!["ssh root@ip1 lsblk", "ssh root@ip2 lsblk"] ); } #[test] fn test_build_commands_empty_selection() { let state = make_state( vec!["ssh root@ip1".to_string()], vec![false], "lsblk", ); assert!(build_commands(&state).is_empty()); } }