Skip to content

Commit

Permalink
Add cli command new to create a new config file with some simple options
Browse files Browse the repository at this point in the history
  • Loading branch information
alexandrebodin committed Jul 19, 2018
1 parent 16f3ce0 commit 4ac5c81
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 14 deletions.
52 changes: 52 additions & 0 deletions cli/new_config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
package cli

import (
"fmt"
"os"

"github.com/BurntSushi/toml"
"github.com/alecthomas/kingpin"
"github.com/alexandrebodin/tmuxctl/config"
)

func init() {
var newConfig = rootCmd.Command("new", "Create a new configuration")
filePtr := newConfig.Flag("file", "Path of the configuration file").Short('f').Default(".tmuxctlrc").String()
namePtr := newConfig.Flag("name", "Name of the session").Short('n').String()
wCount := newConfig.Flag("windows", "Count of windows").Short('w').Default("1").Int()
pCount := newConfig.Flag("panes", "Count of panes per window").Short('p').Default("1").Int()

newConfig.Action(func(ctx *kingpin.ParseContext) error {
fmt.Printf("Creating new configuration file: \"%s\"\n", *filePtr)

conf := &config.Session{
Name: *namePtr,
WindowScripts: []string{},
}

if *wCount > 0 {
for i := 0; i < *wCount; i++ {
w := config.Window{
Scripts: []string{},
PaneScripts: []string{},
}
if *pCount > 0 {
for j := 0; j < *pCount; j++ {
w.Panes = append(w.Panes, config.Pane{Scripts: []string{}})
}
}

conf.Windows = append(conf.Windows, w)
}
}

f, err := os.Create(*filePtr)
if err != nil {
return err
}

defer f.Close()

return toml.NewEncoder(f).Encode(conf)
})
}
28 changes: 14 additions & 14 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,32 @@ import (

// Pane contains a pane configuration
type Pane struct {
Dir string
Zoom bool
Split string
Scripts []string
Dir string `toml:"dir"`
Zoom bool `toml:"zoom"`
Split string `toml:"split"`
Scripts []string `toml:"scripts"`
}

// Window contains a window configuration
type Window struct {
Name string
Dir string
Layout string
Sync bool
Scripts []string
Panes []Pane
Name string `toml:"name"`
Dir string `toml:"dir"`
Layout string `toml:"layout"`
Sync bool `toml:"sync"`
Scripts []string `toml:"scripts"`
PaneScripts []string `toml:"pane-scripts"`
Panes []Pane `toml:"panes"`
}

// Session contains a tmux session configuration
type Session struct {
Name string
Dir string
ClearPanes bool `toml:"clear-panes"`
Windows []Window
Name string `toml:"name"`
Dir string `toml:"dir"`
ClearPanes bool `toml:"clear-panes"`
SelectWindow string `toml:"select-window"`
SelectPane int `toml:"select-pane"`
WindowScripts []string `toml:"window-scripts"`
Windows []Window `toml:"windows"`
}

var (
Expand Down

0 comments on commit 4ac5c81

Please sign in to comment.