refactor: remove process.rs

This commit is contained in:
graelo 2021-03-17 23:32:38 +01:00
parent d2d3e812fc
commit ec03a71bfd
4 changed files with 7 additions and 34 deletions

View file

@ -8,7 +8,6 @@ pub mod colors;
pub mod error;
pub mod model;
pub mod output_destination;
pub mod process;
pub mod regexes;
pub mod selection;
pub mod tmux;

View file

@ -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())
}

View file

@ -5,7 +5,6 @@ use std::fmt;
use std::str::FromStr;
use crate::error::ParseError;
use crate::process;
#[derive(Debug, PartialEq)]
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}",
];
let output = process::execute("tmux", &args)?;
let output = duct::cmd("tmux", &args).read()?;
// Each call to `Pane::parse` returns a `Result<Pane, _>`. All results
// are collected into a Result<Vec<Pane>, _>, thanks to `collect()`.
@ -165,9 +164,7 @@ pub fn list_panes() -> Result<Vec<Pane>, ParseError> {
/// # Example
/// ```get_options("@copyrat-")```
pub fn get_options(prefix: &str) -> Result<HashMap<String, String>, ParseError> {
let args = vec!["show", "-g"];
let output = process::execute("tmux", &args)?;
let output = duct::cmd!("tmux", "show", "-g").read()?;
let lines: Vec<&str> = output.split('\n').collect();
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 output = process::execute("tmux", &args)?;
let output = duct::cmd("tmux", &args).read()?;
Ok(output)
}
/// Ask tmux to swap the current Pane with the target_pane (uses Tmux format).
pub fn swap_pane_with(target_pane: &str) -> Result<(), ParseError> {
// -Z: keep the window zoomed if it was zoomed.
let args = vec!["swap-pane", "-Z", "-s", target_pane];
process::execute("tmux", &args)?;
duct::cmd!("tmux", "swap-pane", "-Z", "-s", target_pane).run()?;
Ok(())
}

View file

@ -8,7 +8,7 @@ use sequence_trie::SequenceTrie;
use termion::{self, color, cursor, event, style};
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> {
model: &'a mut model::Model<'a>,
@ -493,8 +493,8 @@ impl<'a> Ui<'a> {
event::Key::Char(_ch @ ' ') => {
output_destination.toggle();
let message = format!("output destination: `{}`", output_destination);
let args = vec!["display-message", &message];
process::execute("tmux", &args)
duct::cmd!("tmux", "display-message", &message)
.run()
.expect("could not make tmux display the message.");
continue;
}