mirror of
https://github.com/TECHNOFAB11/tmux-copyrat.git
synced 2025-12-12 16:10:07 +01:00
refactor: remove process.rs
This commit is contained in:
parent
d2d3e812fc
commit
ec03a71bfd
4 changed files with 7 additions and 34 deletions
|
|
@ -8,7 +8,6 @@ pub mod colors;
|
||||||
pub mod error;
|
pub mod error;
|
||||||
pub mod model;
|
pub mod model;
|
||||||
pub mod output_destination;
|
pub mod output_destination;
|
||||||
pub mod process;
|
|
||||||
pub mod regexes;
|
pub mod regexes;
|
||||||
pub mod selection;
|
pub mod selection;
|
||||||
pub mod tmux;
|
pub mod tmux;
|
||||||
|
|
|
||||||
|
|
@ -1,21 +0,0 @@
|
||||||
use std::process::Command;
|
|
||||||
|
|
||||||
use crate::error::ParseError;
|
|
||||||
|
|
||||||
/// Execute an arbitrary Unix command and return the stdout as a `String` if
|
|
||||||
/// successful.
|
|
||||||
pub fn execute(command: &str, args: &[&str]) -> Result<String, ParseError> {
|
|
||||||
let output = Command::new(command).args(args).output()?;
|
|
||||||
|
|
||||||
if !output.status.success() {
|
|
||||||
let msg = String::from_utf8_lossy(&output.stderr);
|
|
||||||
return Err(ParseError::ProcessFailure(format!(
|
|
||||||
"Process failure: {} {}, error {}",
|
|
||||||
command,
|
|
||||||
args.join(" "),
|
|
||||||
msg
|
|
||||||
)));
|
|
||||||
}
|
|
||||||
|
|
||||||
Ok(String::from_utf8_lossy(&output.stdout).to_string())
|
|
||||||
}
|
|
||||||
13
src/tmux.rs
13
src/tmux.rs
|
|
@ -5,7 +5,6 @@ use std::fmt;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
|
||||||
use crate::error::ParseError;
|
use crate::error::ParseError;
|
||||||
use crate::process;
|
|
||||||
|
|
||||||
#[derive(Debug, PartialEq)]
|
#[derive(Debug, PartialEq)]
|
||||||
pub struct Pane {
|
pub struct Pane {
|
||||||
|
|
@ -145,7 +144,7 @@ pub fn list_panes() -> Result<Vec<Pane>, ParseError> {
|
||||||
"#{pane_id}:#{?pane_in_mode,true,false}:#{pane_height}:#{scroll_position}:#{?pane_active,true,false}",
|
"#{pane_id}:#{?pane_in_mode,true,false}:#{pane_height}:#{scroll_position}:#{?pane_active,true,false}",
|
||||||
];
|
];
|
||||||
|
|
||||||
let output = process::execute("tmux", &args)?;
|
let output = duct::cmd("tmux", &args).read()?;
|
||||||
|
|
||||||
// Each call to `Pane::parse` returns a `Result<Pane, _>`. All results
|
// Each call to `Pane::parse` returns a `Result<Pane, _>`. All results
|
||||||
// are collected into a Result<Vec<Pane>, _>, thanks to `collect()`.
|
// are collected into a Result<Vec<Pane>, _>, thanks to `collect()`.
|
||||||
|
|
@ -165,9 +164,7 @@ pub fn list_panes() -> Result<Vec<Pane>, ParseError> {
|
||||||
/// # Example
|
/// # Example
|
||||||
/// ```get_options("@copyrat-")```
|
/// ```get_options("@copyrat-")```
|
||||||
pub fn get_options(prefix: &str) -> Result<HashMap<String, String>, ParseError> {
|
pub fn get_options(prefix: &str) -> Result<HashMap<String, String>, ParseError> {
|
||||||
let args = vec!["show", "-g"];
|
let output = duct::cmd!("tmux", "show", "-g").read()?;
|
||||||
|
|
||||||
let output = process::execute("tmux", &args)?;
|
|
||||||
let lines: Vec<&str> = output.split('\n').collect();
|
let lines: Vec<&str> = output.split('\n').collect();
|
||||||
|
|
||||||
let pattern = format!(r#"{prefix}([\w\-0-9]+) "?(\w+)"?"#, prefix = prefix);
|
let pattern = format!(r#"{prefix}([\w\-0-9]+) "?(\w+)"?"#, prefix = prefix);
|
||||||
|
|
@ -223,16 +220,14 @@ pub fn capture_pane(pane: &Pane, region: &CaptureRegion) -> Result<String, Parse
|
||||||
|
|
||||||
let args: Vec<&str> = args.split(' ').collect();
|
let args: Vec<&str> = args.split(' ').collect();
|
||||||
|
|
||||||
let output = process::execute("tmux", &args)?;
|
let output = duct::cmd("tmux", &args).read()?;
|
||||||
Ok(output)
|
Ok(output)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Ask tmux to swap the current Pane with the target_pane (uses Tmux format).
|
/// Ask tmux to swap the current Pane with the target_pane (uses Tmux format).
|
||||||
pub fn swap_pane_with(target_pane: &str) -> Result<(), ParseError> {
|
pub fn swap_pane_with(target_pane: &str) -> Result<(), ParseError> {
|
||||||
// -Z: keep the window zoomed if it was zoomed.
|
// -Z: keep the window zoomed if it was zoomed.
|
||||||
let args = vec!["swap-pane", "-Z", "-s", target_pane];
|
duct::cmd!("tmux", "swap-pane", "-Z", "-s", target_pane).run()?;
|
||||||
|
|
||||||
process::execute("tmux", &args)?;
|
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ use sequence_trie::SequenceTrie;
|
||||||
use termion::{self, color, cursor, event, style};
|
use termion::{self, color, cursor, event, style};
|
||||||
|
|
||||||
use crate::error::ParseError;
|
use crate::error::ParseError;
|
||||||
use crate::{colors, model, output_destination::OutputDestination, process, selection::Selection};
|
use crate::{colors, model, output_destination::OutputDestination, selection::Selection};
|
||||||
|
|
||||||
pub struct Ui<'a> {
|
pub struct Ui<'a> {
|
||||||
model: &'a mut model::Model<'a>,
|
model: &'a mut model::Model<'a>,
|
||||||
|
|
@ -493,8 +493,8 @@ impl<'a> Ui<'a> {
|
||||||
event::Key::Char(_ch @ ' ') => {
|
event::Key::Char(_ch @ ' ') => {
|
||||||
output_destination.toggle();
|
output_destination.toggle();
|
||||||
let message = format!("output destination: `{}`", output_destination);
|
let message = format!("output destination: `{}`", output_destination);
|
||||||
let args = vec!["display-message", &message];
|
duct::cmd!("tmux", "display-message", &message)
|
||||||
process::execute("tmux", &args)
|
.run()
|
||||||
.expect("could not make tmux display the message.");
|
.expect("could not make tmux display the message.");
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue