From 7c5bdb400016cbd98daa83deedac23814db44eb8 Mon Sep 17 00:00:00 2001 From: roger Date: Fri, 19 Apr 2019 11:59:39 +0200 Subject: [PATCH 1/2] swap pane instead of moving to a new window This keeps all the windows in different panes in the same place, so in multiple pane windows looks like we are interacting with the active pane that, in reality we move the pane to the new window until we finish interacting with tmux-hints --- src/main.rs | 11 +++++++---- src/utils.rs | 21 +++++++++++++++++---- 2 files changed, 24 insertions(+), 8 deletions(-) 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/utils.rs b/src/utils.rs index d4dfd37..4c797b9 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 (width, height) = 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", ""]); } @@ -100,12 +104,21 @@ 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_width},#{pane_height}"]); + 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())); + } + }; + + None } -pub fn select_last() { - tmux_run(&["last-window"]); +pub fn swap_pane() { + tmux_run(&["swap-pane", "-t", INNER_PANE]); } pub fn get_terminal_size() -> (usize, usize) { From 2f209f33911db1a52cdc63e40c0d19c1cf2ed71a Mon Sep 17 00:00:00 2001 From: roger Date: Fri, 19 Apr 2019 13:47:42 +0200 Subject: [PATCH 2/2] replace get_terminal_size with pane_size this also removes the stty dependency --- README.md | 2 +- src/screen.rs | 3 ++- src/utils.rs | 27 +++------------------------ 3 files changed, 6 insertions(+), 26 deletions(-) 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/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 4c797b9..c101910 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -67,7 +67,7 @@ pub fn open_url(url: &str) { } pub fn open_inner_window(_title: &str, command: &str) { - let (width, height) = pane_size().unwrap(); + 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 @@ -100,13 +100,9 @@ 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 pane_size() -> Option<(String, String)> { - let output = tmux_output(&["list-panes", "-F", "#{pane_active},#{pane_width},#{pane_height}"]); - let panes: Vec<&str> = output.trim().split("\n").collect(); + 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" { @@ -121,23 +117,6 @@ pub fn swap_pane() { tmux_run(&["swap-pane", "-t", INNER_PANE]); } -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 cursor_at(x: usize, y: usize) -> String { format!("\x1B[{};{}H", y + 1, x + 1) }