initial implementation of root command and made some commands allow being thrown through pipes

This commit is contained in:
Valerie Wolfe 2024-06-10 12:01:15 -04:00
parent 6b7c84b673
commit e017d57a53
5 changed files with 40 additions and 6 deletions

View file

@ -1,4 +1,5 @@
//! commands accessible from within a session //! commands accessible from within a session
use std::fs::read_to_string;
use pico_args::Arguments; use pico_args::Arguments;
use tmux_interface::{ use tmux_interface::{
@ -8,7 +9,10 @@ use tmux_interface::{
use crate::{ error, flag, util }; use crate::{ error, flag, util };
const TMP_ROOT: &str = "/tmp/remux_root";
pub fn switch(pargs: &mut Arguments) { pub fn switch(pargs: &mut Arguments) {
util::terminal_enforce();
// refuse to run outside a session // refuse to run outside a session
util::session_enforce("switch"); util::session_enforce("switch");
@ -32,3 +36,17 @@ pub fn switch(pargs: &mut Arguments) {
.output().ok(); .output().ok();
} }
pub fn root() {
util::session_enforce("root");
let exec = commands::Run::new().shell_command("printf '#{session_path}' > ".to_string() + TMP_ROOT);
Tmux::new()
.add_command(exec)
.output().ok();
if let Ok(text) = read_to_string(TMP_ROOT) {
println!("{text}");
std::fs::remove_file(TMP_ROOT).ok();
}
}

View file

@ -15,6 +15,8 @@ use tmux_interface::{
use crate::{ error, flag, util }; use crate::{ error, flag, util };
pub fn attach(pargs: &mut Arguments) { pub fn attach(pargs: &mut Arguments) {
// must be run from terminal
util::terminal_enforce();
// don't allow unflagged nests // don't allow unflagged nests
util::prevent_nest(); util::prevent_nest();
@ -59,6 +61,7 @@ pub fn attach(pargs: &mut Arguments) {
} }
pub fn detach(pargs: &mut Arguments) { pub fn detach(pargs: &mut Arguments) {
util::terminal_enforce();
// get target or fallback // get target or fallback
let args = pargs.clone().finish(); let args = pargs.clone().finish();
let target: String; let target: String;
@ -142,6 +145,7 @@ pub fn list() {
} }
pub fn new(pargs: &mut Arguments) { pub fn new(pargs: &mut Arguments) {
util::terminal_enforce();
// don't allow unflagged nesting // don't allow unflagged nesting
util::prevent_nest(); util::prevent_nest();

View file

@ -91,6 +91,14 @@ flags:
-n, --nest Create the session inside another session. -n, --nest Create the session inside another session.
-t, --target <dir> Sets the target directory for the new session."), -t, --target <dir> Sets the target directory for the new session."),
Some("root")
=>
println!("remux root
Print the session path (#{{session_path}}) to standard output.
Must be run from inside a session.
usage: remux root"),
Some("s" | "switch") Some("s" | "switch")
=> =>
println!("remux switch println!("remux switch

View file

@ -1,7 +1,4 @@
use std::{ use std::env::{ set_var, var };
env::{ set_var, var },
io::{ stdout, IsTerminal }
};
use pico_args::Arguments; use pico_args::Arguments;
@ -39,8 +36,6 @@ fn main() {
set_var("TMUX", ""); set_var("TMUX", "");
} }
if !stdout().is_terminal() { error::not_terminal(); }
let subcommand = args.subcommand().unwrap(); let subcommand = args.subcommand().unwrap();
// invoke subcommand function // invoke subcommand function
@ -64,6 +59,9 @@ fn main() {
Some("n" | "new") Some("n" | "new")
=> command::share::new(&mut args), => command::share::new(&mut args),
Some("root")
=> command::session::root(),
Some("s" | "switch") Some("s" | "switch")
=> command::session::switch(&mut args), => command::session::switch(&mut args),

View file

@ -1,5 +1,6 @@
use std::{ use std::{
env::{ current_dir, var }, env::{ current_dir, var },
io::{ stdout, IsTerminal },
path::PathBuf, path::PathBuf,
process::exit process::exit
}; };
@ -49,6 +50,11 @@ pub fn session_exists<S: Into<String>>(target: S) -> bool {
.success() .success()
} }
/// enforce a command is being run in a terminal
pub fn terminal_enforce() {
if !stdout().is_terminal() { error::not_terminal(); }
}
/// attempt to return the repo name or exit /// attempt to return the repo name or exit
pub fn repo_fallback() -> String { pub fn repo_fallback() -> String {
let repo = repo_root(current_dir().unwrap()); let repo = repo_root(current_dir().unwrap());