Skip to content

Commit

Permalink
Add new flag -i that allows to create new windows inside current se…
Browse files Browse the repository at this point in the history
…ssion
  • Loading branch information
ivaaaan authored Feb 1, 2022
1 parent 137fe5f commit 5045599
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 26 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
coverage.out
dist/
smug
test_configs/
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
```
Expand Down
6 changes: 4 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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() {
Expand Down
46 changes: 25 additions & 21 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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
}
9 changes: 6 additions & 3 deletions smug.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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)
}

Expand Down Expand Up @@ -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)
Expand Down
22 changes: 22 additions & 0 deletions smug_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5045599

Please sign in to comment.