Skip to content

Commit

Permalink
Add some flags
Browse files Browse the repository at this point in the history
  • Loading branch information
Dlurak committed Aug 19, 2024
1 parent 7a1bd7f commit e7d2f79
Show file tree
Hide file tree
Showing 9 changed files with 210 additions and 130 deletions.
23 changes: 23 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
name: Rust

on:
push:
branches: [ "master" ]
pull_request:
branches: [ "master" ]

env:
CARGO_TERM_COLOR: always

jobs:
build:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3
- name: Build
run: cargo build --verbose
- name: Run Clippy
run: cargo clippy --verbose
- name: Run tests
run: cargo test --verbose
98 changes: 0 additions & 98 deletions src/cli.rs

This file was deleted.

41 changes: 41 additions & 0 deletions src/cli/directory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
use clap::{Parser, Subcommand};

#[derive(Parser, Debug)]
pub struct DirectoryCli {
#[command(subcommand)]
pub action: DirectoryCommands,
}

#[derive(Subcommand, Debug)]
pub enum DirectoryCommands {
#[command(alias = "ls")]
List(ListDirectoryArgs),
Start(StartDirectoryArgs),
}

#[derive(Parser, Debug)]
pub struct ListDirectoryArgs {
/// Show minimal output for scripts
#[arg(short, long, default_value_t = false)]
pub minimal: bool,
}

#[derive(Parser, Debug)]
pub struct StartDirectoryArgs {
/// The directory to start the session in
pub directory: String,

/// Start the session detached
#[arg(short, long, default_value_t = false)]
pub detached: bool,

/// Specify the name of the tmux session
///
/// Optionally provide a name for the session. If not provided, it will be either the name from the configuration or from the directory
#[arg(short, long)]
pub name: Option<String>,

/// Always start a new session instead of attaching to an existing session
#[arg(long, default_value_t = false)]
pub always_new_session: bool,
}
33 changes: 33 additions & 0 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
pub mod directory;
pub mod template;

use clap::{Parser, Subcommand};
use self::{directory::DirectoryCli, template::TemplateCli};

// TODO: Make a `cli`directory with multiple files

/// A CLI for tmux session management
#[derive(Parser, Debug)]
#[clap(version)]
pub struct Cli {
#[command(subcommand)]
pub cmd: Commands,
}

#[derive(Subcommand, Debug)]
pub enum Commands {
/// Initialize the config
///
/// This command will initialize your config directories.
Init,
/// Manage directories in the context of muxmate and tmux
///
/// This command provides functionalities to interact with tmux sessions based on directories.
#[command(alias = "dir", alias = "dirs", alias = "directories")]
Directory(DirectoryCli),
/// Manage templates in the context of muxmate and tmux
///
/// This command provides functionalities to interact with tmux sessions based on templates
#[command(alias = "temp", alias = "templ")]
Template(TemplateCli),
}
47 changes: 47 additions & 0 deletions src/cli/template.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
use clap::{Parser, Subcommand};
use std::path::PathBuf;
#[derive(Parser, Debug)]
pub struct TemplateCli {
#[command(subcommand)]
pub action: TemplateCommands,
}

#[derive(Subcommand, Debug)]
pub enum TemplateCommands {
#[command(alias = "ls")]
List(ListTemplateArgs),
Start(StartTemplateArgs),
}

#[derive(Parser, Debug)]
pub struct ListTemplateArgs {
/// Show minimal output for scripts
#[arg(short, long, default_value_t = false)]
pub minimal: bool,
/// Show all templates including hidden ones
#[arg(short, long, default_value_t = false)]
pub all: bool,
}

#[derive(Parser, Debug)]
pub struct StartTemplateArgs {
pub template_name: String,

/// Start the session detached
#[arg(short, long, default_value_t = false)]
pub detached: bool,

/// The directory to start it in
#[arg(long, alias = "dir")]
pub directory: Option<PathBuf>,

/// Specify the name of the tmux session
///
/// Optionally provide a name for the session. If not provided, it will be either the name from the configuration or from the directory
#[arg(short, long)]
pub name: Option<String>,

/// Always start a new session instead of attaching to an existing session
#[arg(long, default_value_t = false)]
pub always_new_session: bool,
}
35 changes: 14 additions & 21 deletions src/commands/directory.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,8 @@
use crate::{
cli::{DirectoryCli, DirectoryCommands, ListArgs, StartDirectoryArgs},
directories::{self, Directory},
helpers::{absolute_path, dir_name, Exit},
tmux::{attach, session_exists},
widgets::{heading::Heading, table::fmt_table},
cli::directory::{DirectoryCli, DirectoryCommands, ListDirectoryArgs, StartDirectoryArgs}, conditional_command, directories::{self, Directory}, helpers::{absolute_path, dir_name, Exit}, tmux::{attach, session_exists}, widgets::{heading::Heading, table::fmt_table}
};
use std::{collections::HashMap, path::PathBuf};
use tmux_interface::{NewSession, Tmux};
use tmux_interface::{NewSession, Tmux, TmuxCommand};

pub fn directory_handler(args: DirectoryCli) {
match args.action {
Expand All @@ -15,7 +11,7 @@ pub fn directory_handler(args: DirectoryCli) {
}
}

fn list_handler(args: ListArgs) {
fn list_handler(args: ListDirectoryArgs) {
let config = directories::parse_directory_config();
let categories = config.categories;

Expand Down Expand Up @@ -47,24 +43,21 @@ fn start_handler(args: StartDirectoryArgs) {
let (name, path) = resolve_dir_path(&args);
let exists = session_exists(&name).unwrap_or(false);

if !exists {
let new_session_cmd = NewSession::new()
let new_session_cmd = conditional_command!(
args.always_new_session || !exists,
NewSession::new()
.start_directory(path.to_string_lossy())
.detached()
.session_name(&name)
.window_name(&name);
Tmux::new()
.add_command(new_session_cmd)
.output()
.exit(1, "Could not start Tmux-session");
}
.window_name(&name)
);
let attach_cmd = conditional_command!(!args.detached, attach(&name));

if !args.detached {
Tmux::new()
.add_command(attach(&name))
.output()
.exit(1, "Could not switch to the Tmux session");
}
Tmux::new()
.add_command(new_session_cmd)
.add_command(attach_cmd)
.output()
.exit(1, "Could not switch to the Tmux session");
}

fn resolve_dir_path(cli_args: &StartDirectoryArgs) -> (String, PathBuf) {
Expand Down
Loading

0 comments on commit e7d2f79

Please sign in to comment.