Compare commits
No commits in common. "f8c3ac20acd8f54448cbd370d40405dd06669d2b" and "5d60b35d0740cfb1ffc84be293e7e11111cd0e12" have entirely different histories.
f8c3ac20ac
...
5d60b35d07
6 changed files with 19 additions and 75 deletions
|
@ -1,5 +1,6 @@
|
||||||
//! globally available tmux commands.
|
//! globally available tmux commands.
|
||||||
use std::{
|
use std::{
|
||||||
|
env::var,
|
||||||
ffi::OsString,
|
ffi::OsString,
|
||||||
process::exit
|
process::exit
|
||||||
};
|
};
|
||||||
|
@ -11,12 +12,7 @@ use tmux_interface::{
|
||||||
commands
|
commands
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::{ error, flag, util };
|
||||||
env::{ self, env_var },
|
|
||||||
error,
|
|
||||||
flag,
|
|
||||||
util
|
|
||||||
};
|
|
||||||
|
|
||||||
pub fn attach(pargs: &mut Arguments) {
|
pub fn attach(pargs: &mut Arguments) {
|
||||||
// must be run from terminal
|
// must be run from terminal
|
||||||
|
@ -26,7 +22,7 @@ pub fn attach(pargs: &mut Arguments) {
|
||||||
|
|
||||||
// consume optional flags
|
// consume optional flags
|
||||||
let read_only = pargs.contains(flag::READ_ONLY);
|
let read_only = pargs.contains(flag::READ_ONLY);
|
||||||
let detach_other = pargs.contains(flag::DETACH);
|
let detach_other = pargs.contains(flag::DETACHED);
|
||||||
|
|
||||||
let args = pargs.clone().finish();
|
let args = pargs.clone().finish();
|
||||||
let target: String;
|
let target: String;
|
||||||
|
@ -126,7 +122,7 @@ pub fn list() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// get attached session symbol
|
// get attached session symbol
|
||||||
let attach_symbol = env_var(env::ATTACH_SYMBOL);
|
let attach_symbol = var("REMUX_ATTACH_SYMBOL").unwrap_or("*".to_string());
|
||||||
|
|
||||||
// pretty print session list
|
// pretty print session list
|
||||||
println!("sessions:");
|
println!("sessions:");
|
||||||
|
@ -153,13 +149,9 @@ pub fn new(pargs: &mut Arguments) {
|
||||||
// don't allow unflagged nesting
|
// don't allow unflagged nesting
|
||||||
util::prevent_nest();
|
util::prevent_nest();
|
||||||
|
|
||||||
// get optional flags
|
// get optional flag
|
||||||
let detached = pargs.contains(flag::DETACH);
|
|
||||||
let target_dir: Result<String, Error> = pargs.value_from_str(flag::TARGET);
|
let target_dir: Result<String, Error> = pargs.value_from_str(flag::TARGET);
|
||||||
|
|
||||||
// get environment variables
|
|
||||||
let window_name = env_var(env::NEW_WINDOW_NAME);
|
|
||||||
|
|
||||||
// get target or fallback
|
// get target or fallback
|
||||||
let args = pargs.clone().finish();
|
let args = pargs.clone().finish();
|
||||||
let title: String;
|
let title: String;
|
||||||
|
@ -176,18 +168,10 @@ pub fn new(pargs: &mut Arguments) {
|
||||||
let mut new = commands::NewSession::new();
|
let mut new = commands::NewSession::new();
|
||||||
new = new.group_name(title);
|
new = new.group_name(title);
|
||||||
if let Some(command) = command { new.shell_command = Some(command.to_string_lossy()); }
|
if let Some(command) = command { new.shell_command = Some(command.to_string_lossy()); }
|
||||||
if detached { new.detached = true; }
|
|
||||||
if let Ok(target_dir) = target_dir { new = new.start_directory(target_dir); }
|
if let Ok(target_dir) = target_dir { new = new.start_directory(target_dir); }
|
||||||
|
|
||||||
let mut tmux = Tmux::new().add_command(new);
|
Tmux::new()
|
||||||
|
.add_command(new)
|
||||||
// rename window if var not empty
|
.output().ok();
|
||||||
if !window_name.is_empty() {
|
|
||||||
let auto_name = commands::RenameWindow::new()
|
|
||||||
.new_name(window_name);
|
|
||||||
tmux = tmux.add_command(auto_name);
|
|
||||||
}
|
|
||||||
|
|
||||||
tmux.output().ok();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
13
src/env.rs
13
src/env.rs
|
@ -1,13 +0,0 @@
|
||||||
use std::env::var;
|
|
||||||
|
|
||||||
pub type EnvVar = (&'static str, &'static str);
|
|
||||||
|
|
||||||
pub static ATTACH_SYMBOL: EnvVar = ("REMUX_ATTACH_SYMBOL", "*");
|
|
||||||
pub static NEW_WINDOW_NAME: EnvVar = ("REMUX_NEW_WINDOW", "");
|
|
||||||
|
|
||||||
pub fn env_var(envvar: EnvVar) -> String {
|
|
||||||
var(envvar.0).unwrap_or(envvar.1.to_string())
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn tmux() -> bool { !var("TMUX").unwrap_or("".to_string()).is_empty() }
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
|
|
||||||
type Flag = [&'static str;2];
|
type Flag = [&'static str;2];
|
||||||
|
|
||||||
pub static DETACH: Flag = ["-d", "--detach"];
|
pub static DETACHED: Flag = ["-d", "--detached"];
|
||||||
pub static HELP: Flag = ["-h", "--help"];
|
pub static HELP: Flag = ["-h", "--help"];
|
||||||
pub static NEST: Flag = ["-n", "--nest"];
|
pub static NEST: Flag = ["-n", "--nest"];
|
||||||
pub static QUIET: Flag = ["-q", "--quiet"];
|
pub static QUIET: Flag = ["-q", "--quiet"];
|
||||||
|
|
33
src/help.rs
33
src/help.rs
|
@ -16,25 +16,16 @@ A command wrapper for tmux written in Rust.
|
||||||
usage: remux <command> [<args>]
|
usage: remux <command> [<args>]
|
||||||
|
|
||||||
commands:
|
commands:
|
||||||
help Show help text for remux, a command, or a help topic.
|
help Show help text for remux or a specific command
|
||||||
attach Attach to an existing tmux session
|
attach Attach to an existing tmux session
|
||||||
detach Detach clients from a tmux session
|
detach Detach clients from a tmux session
|
||||||
has Check if a tmux session exists
|
has Check if a tmux session exists
|
||||||
list Pretty-print all tmux sessions
|
list Pretty-print all tmux sessions
|
||||||
new Create a new tmux session
|
new Create a new tmux session
|
||||||
|
|
||||||
root print session path (session)
|
Use 'remux help <command>' to see detailed help text for each command."),
|
||||||
switch switch to another session (session)
|
|
||||||
|
|
||||||
Use 'remux help <command>' to see detailed help text for each command.
|
Some("a" | "attach")
|
||||||
|
|
||||||
help topics:
|
|
||||||
env Environment variables"),
|
|
||||||
|
|
||||||
|
|
||||||
// COMMAND HELP
|
|
||||||
|
|
||||||
Some("a" | "attach")
|
|
||||||
=>
|
=>
|
||||||
println!("remux attach
|
println!("remux attach
|
||||||
Attach to an existing session.
|
Attach to an existing session.
|
||||||
|
@ -123,23 +114,7 @@ args:
|
||||||
flags:
|
flags:
|
||||||
-r, --read-only Attach the target session as read-only."),
|
-r, --read-only Attach the target session as read-only."),
|
||||||
|
|
||||||
// TOPIC HELP
|
// not found
|
||||||
|
|
||||||
Some("env" | "vars")
|
|
||||||
=>
|
|
||||||
println!("remux environment variables
|
|
||||||
|
|
||||||
REMUX_ATTACH_SYMBOL
|
|
||||||
Changes the symbol displayed for attached sessions displayed
|
|
||||||
by the 'list' command.
|
|
||||||
Default: '*'
|
|
||||||
|
|
||||||
REMUX_NEW_WINDOW
|
|
||||||
Provides a default window name when creating a session with
|
|
||||||
the 'new' command, if not empty.
|
|
||||||
Default: ''"),
|
|
||||||
|
|
||||||
// not found
|
|
||||||
_ => error::no_help(topic.unwrap())
|
_ => error::no_help(topic.unwrap())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -3,7 +3,6 @@ use std::env::{ set_var, var };
|
||||||
use pico_args::Arguments;
|
use pico_args::Arguments;
|
||||||
|
|
||||||
mod command;
|
mod command;
|
||||||
mod env;
|
|
||||||
mod error;
|
mod error;
|
||||||
mod flag;
|
mod flag;
|
||||||
mod help;
|
mod help;
|
||||||
|
|
13
src/util.rs
13
src/util.rs
|
@ -1,5 +1,5 @@
|
||||||
use std::{
|
use std::{
|
||||||
env::current_dir,
|
env::{ current_dir, var },
|
||||||
io::{ stdout, IsTerminal },
|
io::{ stdout, IsTerminal },
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
process::exit
|
process::exit
|
||||||
|
@ -12,10 +12,7 @@ use tmux_interface::{
|
||||||
variables::session::SessionsCtl
|
variables::session::SessionsCtl
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{
|
use crate::error;
|
||||||
env,
|
|
||||||
error
|
|
||||||
};
|
|
||||||
|
|
||||||
/// return a Vec of all sessions or None
|
/// return a Vec of all sessions or None
|
||||||
pub fn get_sessions() -> Option<Vec<Session>> {
|
pub fn get_sessions() -> Option<Vec<Session>> {
|
||||||
|
@ -27,9 +24,10 @@ pub fn get_sessions() -> Option<Vec<Session>> {
|
||||||
|
|
||||||
/// show the tmux nest text if env var is not unset
|
/// show the tmux nest text if env var is not unset
|
||||||
pub fn prevent_nest() {
|
pub fn prevent_nest() {
|
||||||
if env::tmux() {
|
let tmux = var("TMUX").ok();
|
||||||
|
if tmux.is_some() && tmux.unwrap() != "" {
|
||||||
println!("Sessions should be nested with care; unset TMUX or use the '-n' flag to allow.");
|
println!("Sessions should be nested with care; unset TMUX or use the '-n' flag to allow.");
|
||||||
exit(6);
|
exit(1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -46,6 +44,7 @@ pub fn session_exists<S: Into<String>>(target: S) -> bool {
|
||||||
let has_session = commands::HasSession::new()
|
let has_session = commands::HasSession::new()
|
||||||
.target_session(target.into());
|
.target_session(target.into());
|
||||||
Tmux::new().add_command(has_session)
|
Tmux::new().add_command(has_session)
|
||||||
|
.disable_echo()
|
||||||
.status()
|
.status()
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.success()
|
.success()
|
||||||
|
|
Loading…
Reference in a new issue