Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

swap pane instead of moving to a new window #17

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
11 changes: 7 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)),
}

Expand All @@ -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);
}
Expand Down
3 changes: 2 additions & 1 deletion src/screen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![],
}
}
Expand Down
40 changes: 16 additions & 24 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<I, S>(args: I) -> Command
where
Expand Down Expand Up @@ -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", ""]);
}

Expand All @@ -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 {
Expand Down