diff --git a/README.md b/README.md index 83c5938..6a051df 100644 --- a/README.md +++ b/README.md @@ -18,7 +18,7 @@ Inspired by another tool in perl [tmux-url-select][2]. ## Requirements -Depends on `rust`, `tmux` and `stty`. +Depends on `rust`, `tmux`. ## Installation diff --git a/src/main.rs b/src/main.rs index 4028daa..2f723be 100644 --- a/src/main.rs +++ b/src/main.rs @@ -44,18 +44,21 @@ fn read_loop(screen: &mut Screen) { utils::open_url(selected); utils::display(&format!("Opening: {}", selected)); if key != 'O' { - utils::select_last(); + utils::swap_pane(); return; } } // paste in console 'p' => { - utils::select_last(); + utils::swap_pane(); utils::tmux_run(&["send", screen.selected()]); return; } // exit - 'q' => return, + 'q' => { + utils::swap_pane(); + return; + }, _ => utils::display(&format!("Unknown key: {}", key)), } @@ -76,7 +79,7 @@ fn inner() { io::stdout().flush().unwrap(); // Avoid flickering by moving here - utils::select_inner_window(); + utils::swap_pane(); read_loop(&mut screen); } diff --git a/src/screen.rs b/src/screen.rs index 262d294..5518be3 100644 --- a/src/screen.rs +++ b/src/screen.rs @@ -29,12 +29,13 @@ impl Screen { pub fn new() -> Screen { utils::capture_pane(); + let size = utils::pane_size().unwrap(); let buffer = utils::get_buffer(); utils::clear_buffer(); Screen { buffer, selected: 0, - size: utils::get_terminal_size(), + size: (size.0.parse().unwrap(), size.1.parse().unwrap()), hints: vec![], } } diff --git a/src/utils.rs b/src/utils.rs index d4dfd37..c101910 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -7,6 +7,7 @@ use std::process::{Command, Stdio}; use crate::settings::Settings; const INNER_WINDOW: &str = "999999"; +const INNER_PANE: &str = "999999.0"; pub fn tmux(args: I) -> Command where @@ -66,10 +67,13 @@ pub fn open_url(url: &str) { } pub fn open_inner_window(_title: &str, command: &str) { + let (height, width) = pane_size().unwrap(); let command = format!("{} inner", command); tmux_run(&["new-window", "-dn", "", "-t", INNER_WINDOW, &command]); // Remove status format in new window tmux_run(&["setw", "-qt", INNER_WINDOW, "window-status-format", ""]); + tmux_run(&["setw", "-qt", INNER_WINDOW, "force-height", &height.to_string()]); + tmux_run(&["setw", "-qt", INNER_WINDOW, "force-width", &width.to_string()]); tmux_run(&["setw", "-qt", INNER_WINDOW, "window-status-current-format", ""]); } @@ -96,33 +100,21 @@ pub fn clean_string(buffer: &str) -> String { rectrl.replace_all(&buffer, "").to_string() } -pub fn select_window(title: &str) { - tmux_run(&["select-window", "-t", title]); -} - -pub fn select_inner_window() { - select_window(INNER_WINDOW); -} +pub fn pane_size() -> Option<(String, String)> { + let output = tmux_output(&["list-panes", "-F", "#{pane_active},#{pane_height},#{pane_width}"]); + let panes: Vec<&str> = output.trim().split('\n').collect(); + for pane_str in &panes { + let pane: Vec<&str> = pane_str.split(',').collect(); + if pane[0] == "1" { + return Some((pane[1].to_owned(), pane[2].to_owned())); + } + }; -pub fn select_last() { - tmux_run(&["last-window"]); + None } -pub fn get_terminal_size() -> (usize, usize) { - let out = Command::new("stty") - .arg("-F") - .arg("/dev/tty") - .arg("size") - .output() - .expect("Can't run stty"); - - let result = String::from_utf8_lossy(&out.stdout).to_string(); - let result: Vec<&str> = result.trim().split(' ').collect(); - - match result.as_slice() { - [a, b] => (a.parse().unwrap(), b.parse().unwrap()), - _ => panic!("stty invalid output"), - } +pub fn swap_pane() { + tmux_run(&["swap-pane", "-t", INNER_PANE]); } pub fn cursor_at(x: usize, y: usize) -> String {