Skip to content

Commit

Permalink
feat: add tmuxinator support (#171)
Browse files Browse the repository at this point in the history
* Added support for listing configs and connecting via tmuxinator with -T
flag

* feat: improve tmuxinator features

- Simplify module names
- Change connection strategy to use mapping based of connect Src
- Add icon support to tmuxinator entries
- Changed the priority to list and connect to tmuxinator before zoxide

* chore: update mockery
* fix: tests and remove log

---------

Co-authored-by: kadriandev <[email protected]>
  • Loading branch information
joshmedeski and kadriandev authored Sep 20, 2024
1 parent 18eda33 commit 7c05d27
Show file tree
Hide file tree
Showing 48 changed files with 526 additions and 63 deletions.
2 changes: 1 addition & 1 deletion cloner/mock_Cloner.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion configurator/mock_Configurator.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions connector/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/joshmedeski/sesh/namer"
"github.com/joshmedeski/sesh/startup"
"github.com/joshmedeski/sesh/tmux"
"github.com/joshmedeski/sesh/tmuxinator"
"github.com/joshmedeski/sesh/zoxide"
"github.com/stretchr/testify/assert"
mock "github.com/stretchr/testify/mock"
Expand All @@ -23,6 +24,7 @@ func TestConfigStrategy(t *testing.T) {
mockStartup := new(startup.MockStartup)
mockTmux := new(tmux.MockTmux)
mockZoxide := new(zoxide.MockZoxide)
mockTmuxinator := new(tmuxinator.MockTmuxinator)

c := &RealConnector{
model.Config{},
Expand All @@ -33,6 +35,7 @@ func TestConfigStrategy(t *testing.T) {
mockStartup,
mockTmux,
mockZoxide,
mockTmuxinator,
}
mockTmux.On("AttachSession", mock.Anything).Return("attaching", nil)
mockZoxide.On("Add", mock.Anything).Return(nil)
Expand Down
16 changes: 9 additions & 7 deletions connector/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,19 @@ func (c *RealConnector) Connect(name string, opts model.ConnectOpts) (string, er
// sesh connect --config (sesh list --config | fzf)
strategies := []func(*RealConnector, string) (model.Connection, error){
tmuxStrategy,
tmuxinatorStrategy,
configStrategy,
dirStrategy,
zoxideStrategy,
}

connectStrategy := map[string]func(c *RealConnector, connection model.Connection, opts model.ConnectOpts) (string, error){
"tmux": connectToTmux,
"tmuxinator": connectToTmuxinator,
"config": connectToTmux,
"zoxide": connectToTmux,
}

for _, strategy := range strategies {
if connection, err := strategy(c, name); err != nil {
return "", fmt.Errorf("failed to establish connection: %w", err)
Expand All @@ -28,13 +36,7 @@ func (c *RealConnector) Connect(name string, opts model.ConnectOpts) (string, er
if connection.AddToZoxide {
c.zoxide.Add(connection.Session.Path)
}
if connection.New {
c.tmux.NewSession(connection.Session.Name, connection.Session.Path)
c.startup.Exec(connection.Session)
}
// TODO: configure the ability to create a session in a detached way (like update)
// TODO: configure the ability to create a popup instead of switching (with no tmux bar?)
return c.tmux.SwitchOrAttach(connection.Session.Name, opts)
return connectStrategy[connection.Session.Src](c, connection, opts)
}
}

Expand Down
20 changes: 12 additions & 8 deletions connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"github.com/joshmedeski/sesh/namer"
"github.com/joshmedeski/sesh/startup"
"github.com/joshmedeski/sesh/tmux"
"github.com/joshmedeski/sesh/tmuxinator"
"github.com/joshmedeski/sesh/zoxide"
)

Expand All @@ -16,14 +17,15 @@ type Connector interface {
}

type RealConnector struct {
config model.Config
dir dir.Dir
home home.Home
lister lister.Lister
namer namer.Namer
startup startup.Startup
tmux tmux.Tmux
zoxide zoxide.Zoxide
config model.Config
dir dir.Dir
home home.Home
lister lister.Lister
namer namer.Namer
startup startup.Startup
tmux tmux.Tmux
zoxide zoxide.Zoxide
tmuxinator tmuxinator.Tmuxinator
}

func NewConnector(
Expand All @@ -35,6 +37,7 @@ func NewConnector(
startup startup.Startup,
tmux tmux.Tmux,
zoxide zoxide.Zoxide,
tmuxinator tmuxinator.Tmuxinator,
) Connector {
return &RealConnector{
config,
Expand All @@ -45,5 +48,6 @@ func NewConnector(
startup,
tmux,
zoxide,
tmuxinator,
}
}
2 changes: 1 addition & 1 deletion connector/mock_Connector.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 9 additions & 0 deletions connector/tmux.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,12 @@ func tmuxStrategy(c *RealConnector, name string) (model.Connection, error) {
// Switch: true
}, nil
}

func connectToTmux(c *RealConnector, connection model.Connection, opts model.ConnectOpts) (string, error) {
if connection.New {
return c.tmux.SwitchOrAttach(connection.Session.Name, opts)
}
c.tmux.NewSession(connection.Session.Name, connection.Session.Path)
c.startup.Exec(connection.Session)
return c.tmux.SwitchOrAttach(connection.Session.Name, opts)
}
3 changes: 3 additions & 0 deletions connector/tmux_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/joshmedeski/sesh/namer"
"github.com/joshmedeski/sesh/startup"
"github.com/joshmedeski/sesh/tmux"
"github.com/joshmedeski/sesh/tmuxinator"
"github.com/joshmedeski/sesh/zoxide"
"github.com/stretchr/testify/assert"
mock "github.com/stretchr/testify/mock"
Expand All @@ -23,6 +24,7 @@ func TestEstablishTmuxConnection(t *testing.T) {
mockStartup := new(startup.MockStartup)
mockTmux := new(tmux.MockTmux)
mockZoxide := new(zoxide.MockZoxide)
mockTmuxinator := new(tmuxinator.MockTmuxinator)

c := &RealConnector{
model.Config{},
Expand All @@ -33,6 +35,7 @@ func TestEstablishTmuxConnection(t *testing.T) {
mockStartup,
mockTmux,
mockZoxide,
mockTmuxinator,
}
mockTmux.On("AttachSession", mock.Anything).Return("attaching", nil)
mockZoxide.On("Add", mock.Anything).Return(nil)
Expand Down
23 changes: 23 additions & 0 deletions connector/tmuxinator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package connector

import (
"github.com/joshmedeski/sesh/model"
)

func tmuxinatorStrategy(c *RealConnector, name string) (model.Connection, error) {
session, exists := c.lister.FindTmuxinatorConfig(name)
if !exists {
return model.Connection{Found: false}, nil
}

return model.Connection{
Found: true,
Session: session,
New: true,
AddToZoxide: false,
}, nil
}

func connectToTmuxinator(c *RealConnector, connection model.Connection, opts model.ConnectOpts) (string, error) {
return c.tmuxinator.Start(connection.Session.Name)
}
2 changes: 1 addition & 1 deletion dir/mock_Dir.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion execwrap/mock_Exec.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion execwrap/mock_ExecCmd.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion git/mock_Git.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion home/mock_Home.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 8 additions & 4 deletions icon/icon.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ func NewIcon(config model.Config) Icon {
}

var (
zoxideIcon string = ""
tmuxIcon string = ""
configIcon string = ""
zoxideIcon string = ""
tmuxIcon string = ""
configIcon string = ""
tmuxinatorIcon string = ""
)

func ansiString(code int, s string) string {
Expand All @@ -37,6 +38,9 @@ func (i *RealIcon) AddIcon(s model.SeshSession) string {
case "tmux":
icon = tmuxIcon
colorCode = 34 // blue
case "tmuxinator":
icon = tmuxinatorIcon
colorCode = 33 // yellow
case "zoxide":
icon = zoxideIcon
colorCode = 36 // cyan
Expand All @@ -51,7 +55,7 @@ func (i *RealIcon) AddIcon(s model.SeshSession) string {
}

func (i *RealIcon) RemoveIcon(name string) string {
if strings.HasPrefix(name, tmuxIcon) || strings.HasPrefix(name, zoxideIcon) || strings.HasPrefix(name, configIcon) {
if strings.HasPrefix(name, tmuxIcon) || strings.HasPrefix(name, zoxideIcon) || strings.HasPrefix(name, configIcon) || strings.HasPrefix(name, tmuxinatorIcon) {
return name[4:]
}
return name
Expand Down
2 changes: 1 addition & 1 deletion icon/mock_Icon.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion json/mock_Json.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion lister/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func listConfig(l *RealLister) (model.SeshSessions, error) {
Name: session.Name,
Path: path,
StartupCommand: session.StartupCommand,
Tmuxinator: session.Tmuxinator,
}
}
}
Expand All @@ -36,8 +37,8 @@ func listConfig(l *RealLister) (model.SeshSessions, error) {
}

func (l *RealLister) FindConfigSession(name string) (model.SeshSession, bool) {
sessions, _ := listConfig(l)
key := configKey(name)
sessions, _ := listConfig(l)
if session, exists := sessions.Directory[key]; exists {
return session, exists
} else {
Expand Down
4 changes: 3 additions & 1 deletion lister/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/joshmedeski/sesh/home"
"github.com/joshmedeski/sesh/model"
"github.com/joshmedeski/sesh/tmux"
"github.com/joshmedeski/sesh/tmuxinator"
"github.com/joshmedeski/sesh/zoxide"
"github.com/stretchr/testify/assert"
)
Expand All @@ -16,6 +17,7 @@ func TestListConfigSessions(t *testing.T) {
mockHome.On("ExpandHome", "/Users/joshmedeski/.config/sesh").Return("/Users/joshmedeski/.config/sesh", nil)
mockZoxide := new(zoxide.MockZoxide)
mockTmux := new(tmux.MockTmux)
mockTmuxinator := new(tmuxinator.MockTmuxinator)
config := model.Config{
SessionConfigs: []model.SessionConfig{
{
Expand All @@ -24,7 +26,7 @@ func TestListConfigSessions(t *testing.T) {
},
},
}
lister := NewLister(config, mockHome, mockTmux, mockZoxide)
lister := NewLister(config, mockHome, mockTmux, mockZoxide, mockTmuxinator)

realLister, ok := lister.(*RealLister)
if !ok {
Expand Down
8 changes: 5 additions & 3 deletions lister/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,16 @@ type (
Json bool
Tmux bool
Zoxide bool
Tmuxinator bool
}
srcStrategy func(*RealLister) (model.SeshSessions, error)
)

var srcStrategies = map[string]srcStrategy{
"tmux": listTmux,
"config": listConfig,
"zoxide": listZoxide,
"tmux": listTmux,
"config": listConfig,
"tmuxinator": listTmuxinator,
"zoxide": listZoxide,
}

func (l *RealLister) List(opts ListOptions) (model.SeshSessions, error) {
Expand Down
15 changes: 9 additions & 6 deletions lister/lister.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"github.com/joshmedeski/sesh/home"
"github.com/joshmedeski/sesh/model"
"github.com/joshmedeski/sesh/tmux"
"github.com/joshmedeski/sesh/tmuxinator"
"github.com/joshmedeski/sesh/zoxide"
)

Expand All @@ -13,15 +14,17 @@ type Lister interface {
GetLastTmuxSession() (model.SeshSession, bool)
FindConfigSession(name string) (model.SeshSession, bool)
FindZoxideSession(name string) (model.SeshSession, bool)
FindTmuxinatorConfig(name string) (model.SeshSession, bool)
}

type RealLister struct {
home home.Home
tmux tmux.Tmux
zoxide zoxide.Zoxide
config model.Config
config model.Config
home home.Home
tmux tmux.Tmux
zoxide zoxide.Zoxide
tmuxinator tmuxinator.Tmuxinator
}

func NewLister(config model.Config, home home.Home, tmux tmux.Tmux, zoxide zoxide.Zoxide) Lister {
return &RealLister{home, tmux, zoxide, config}
func NewLister(config model.Config, home home.Home, tmux tmux.Tmux, zoxide zoxide.Zoxide, tmuxinator tmuxinator.Tmuxinator) Lister {
return &RealLister{config, home, tmux, zoxide, tmuxinator}
}
Loading

0 comments on commit 7c05d27

Please sign in to comment.