Add new flag `-i` that allows to create new windows inside current session

master
Ivan 2 years ago committed by GitHub
parent 137fe5f038
commit 5045599de6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

1
.gitignore vendored

@ -1,3 +1,4 @@
coverage.out
dist/
smug
test_configs/

@ -82,6 +82,7 @@ smug <command> [<project>] [-f, --file <file>] [-w, --windows <window>]... [-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
```

@ -16,13 +16,15 @@ var usage = fmt.Sprintf(`Smug - tmux session manager. Version %s
Usage:
smug <command> [<project>] [-f, --file <file>] [-w, --windows <window>]... [-a, --attach] [-d, --debug] [<key>=<value>]...
smug <command> [<project>] [-f, --file <file>] [-w, --windows <window>]... [-a, --attach] [-d, --debug] [--detach] [-i, --inside-current-session] [<key>=<value>]...
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() {

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

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

@ -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 {

Loading…
Cancel
Save