From 5045599de66cf235587a2822d2d23107da65cfef Mon Sep 17 00:00:00 2001 From: Ivan Date: Tue, 1 Feb 2022 13:49:36 +0200 Subject: [PATCH] Add new flag `-i` that allows to create new windows inside current session --- .gitignore | 1 + README.md | 1 + main.go | 6 ++++-- options.go | 46 +++++++++++++++++++++++++--------------------- smug.go | 9 ++++++--- smug_test.go | 22 ++++++++++++++++++++++ 6 files changed, 59 insertions(+), 26 deletions(-) diff --git a/.gitignore b/.gitignore index 7c94bee..0746fca 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ coverage.out dist/ smug +test_configs/ diff --git a/README.md b/README.md index d7f5d00..4e51d63 100644 --- a/README.md +++ b/README.md @@ -82,6 +82,7 @@ smug [] [-f, --file ] [-w, --windows ]... [-a, -f, --file A custom path to a config file -w, --windows List of windows to start. If session exists, those windows will be attached to current session. -a, --attach Force switch client for a session +-i, --inside-current-session Create all windows inside current session -d, --debug Print all commands to ~/.config/smug/smug.log --detach Detach session. The same as `-d` flag in the tmux ``` diff --git a/main.go b/main.go index ebf4cfe..e557ebf 100644 --- a/main.go +++ b/main.go @@ -16,13 +16,15 @@ var usage = fmt.Sprintf(`Smug - tmux session manager. Version %s Usage: - smug [] [-f, --file ] [-w, --windows ]... [-a, --attach] [-d, --debug] [=]... + smug [] [-f, --file ] [-w, --windows ]... [-a, --attach] [-d, --debug] [--detach] [-i, --inside-current-session] [=]... Options: -f, --file %s -w, --windows %s -a, --attach %s + -i, --inside-current-session %s -d, --debug %s + --detach %s Commands: list list available project configurations @@ -43,7 +45,7 @@ Examples: $ smug stop blog $ smug start blog --attach $ smug print > ~/.config/smug/blog.yml -`, version, FileUsage, WindowsUsage, AttachUsage, DebugUsage) +`, version, FileUsage, WindowsUsage, AttachUsage, InsideCurrentSessionUsage, DebugUsage, DetachUsage) func main() { options, err := ParseOptions(os.Args[1:], func() { diff --git a/options.go b/options.go index 1e4f949..fbfd7de 100644 --- a/options.go +++ b/options.go @@ -19,24 +19,26 @@ const ( var validCommands = []string{CommandStart, CommandStop, CommandNew, CommandEdit, CommandList, CommandPrint} type Options struct { - Command string - Project string - Config string - Windows []string - Settings map[string]string - Attach bool - Detach bool - Debug bool + Command string + Project string + Config string + Windows []string + Settings map[string]string + Attach bool + Detach bool + Debug bool + InsideCurrentSession bool } var ErrHelp = errors.New("help requested") const ( - WindowsUsage = "List of windows to start. If session exists, those windows will be attached to current session." - AttachUsage = "Force switch client for a session" - DetachUsage = "Detach tmux session. The same as -d flag in the tmux" - DebugUsage = "Print all commands to ~/.config/smug/smug.log" - FileUsage = "A custom path to a config file" + WindowsUsage = "List of windows to start. If session exists, those windows will be attached to current session" + AttachUsage = "Force switch client for a session" + DetachUsage = "Detach tmux session. The same as -d flag in the tmux" + DebugUsage = "Print all commands to ~/.config/smug/smug.log" + FileUsage = "A custom path to a config file" + InsideCurrentSessionUsage = "Create all windows inside current session" ) // Creates a new FlagSet. @@ -70,6 +72,7 @@ func ParseOptions(argv []string, helpRequested func()) (Options, error) { attach := flags.BoolP("attach", "a", false, AttachUsage) detach := flags.Bool("detach", false, DetachUsage) debug := flags.BoolP("debug", "d", false, DebugUsage) + insideCurrentSession := flags.BoolP("inside-current-session", "i", false, InsideCurrentSessionUsage) err := flags.Parse(argv) @@ -106,13 +109,14 @@ func ParseOptions(argv []string, helpRequested func()) (Options, error) { } return Options{ - Project: project, - Config: *config, - Command: cmd, - Settings: settings, - Windows: *windows, - Attach: *attach, - Detach: *detach, - Debug: *debug, + Project: project, + Config: *config, + Command: cmd, + Settings: settings, + Windows: *windows, + Attach: *attach, + Detach: *detach, + Debug: *debug, + InsideCurrentSession: *insideCurrentSession, }, nil } diff --git a/smug.go b/smug.go index afa7ab8..8e347dc 100644 --- a/smug.go +++ b/smug.go @@ -72,6 +72,7 @@ func (smug Smug) switchOrAttach(target string, attach bool, insideTmuxSession bo } else if !insideTmuxSession { return smug.tmux.Attach(target, os.Stdin, os.Stdout, os.Stderr) } + return nil } @@ -126,7 +127,7 @@ func (smug Smug) Start(config Config, options Options, context Context) error { if err != nil { return err } - } else if len(windows) == 0 { + } else if len(windows) == 0 && !options.InsideCurrentSession { return smug.switchOrAttach(sessionName, attach, context.InsideTmuxSession) } @@ -191,8 +192,10 @@ func (smug Smug) Start(config Config, options Options, context Context) error { } } - smug.tmux.KillWindow(sessionName + defaultWindowName) - smug.tmux.RenumberWindows(sessionName) + if !options.InsideCurrentSession { + smug.tmux.KillWindow(sessionName + defaultWindowName) + smug.tmux.RenumberWindows(sessionName) + } if len(windows) == 0 && len(config.Windows) > 0 && options.Detach == false { return smug.switchOrAttach(sessionName+config.Windows[0].Name, attach, context.InsideTmuxSession) diff --git a/smug_test.go b/smug_test.go index 5d51275..f2fbb4d 100644 --- a/smug_test.go +++ b/smug_test.go @@ -212,6 +212,28 @@ var testTable = map[string]struct { }, []string{""}, }, + "test create new windows in current session": { + Config{ + Session: "ses", + Root: "root", + Windows: []Window{ + {Name: "win1"}, + }, + }, + Options{ + InsideCurrentSession: true, + }, + Context{InsideTmuxSession: true}, + []string{ + "tmux has-session -t ses:", + "tmux neww -Pd -t ses: -c root -F #{window_id} -n win1", + "tmux select-layout -t even-horizontal", + }, + []string{ + "tmux kill-session -t ses", + }, + []string{""}, + }, } type MockCommander struct {