Skip to content

Commit a08a264

Browse files
authored
feat: improve session logic (#78)
- Switch isSession to FindSession and return nil if not found - Determine name logic now returns the session name if it exists - Use "::" separator in tmux list to support spaces in path and name
1 parent fc2671d commit a08a264

File tree

7 files changed

+53
-38
lines changed

7 files changed

+53
-38
lines changed

README.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ sesh connect $(sesh list | fzf)
7676
In order to integrate with tmux, you can add a binding to your tmux config (`tmux.conf`). For example, the following will bind `ctrl-a T` to open a fzf prompt as a tmux popup (using `fzf-tmux`) and using different commands to list sessions (`sesh list -t`), zoxide directories (`sesh list -z`), and find directories (`fd...`).
7777

7878
```sh
79-
bind-key "T" run-shell "sesh connect $(
79+
bind-key "T" run-shell "sesh connect \"$(
8080
sesh list -tz | fzf-tmux -p 55%,60% \
8181
--no-sort --border-label ' sesh ' --prompt '' \
8282
--header ' ^a all ^t tmux ^x zoxide ^f find' \
@@ -85,7 +85,7 @@ In order to integrate with tmux, you can add a binding to your tmux config (`tmu
8585
--bind 'ctrl-t:change-prompt(🪟 )+reload(sesh list -t)' \
8686
--bind 'ctrl-x:change-prompt(📁 )+reload(sesh list -z)' \
8787
--bind 'ctrl-f:change-prompt(🔎 )+reload(fd -H -d 2 -t d -E .Trash . ~)'
88-
)"
88+
)\""
8989
```
9090

9191
You can customize this however you want, see `man fzf` for more info on the different options.

name/name.go

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"strings"
77

88
"github.com/joshmedeski/sesh/git"
9+
"github.com/joshmedeski/sesh/tmux"
910
)
1011

1112
func convertToValidName(name string) string {
@@ -47,12 +48,17 @@ func nameFromGit(result string) string {
4748
return nameFromGit
4849
}
4950

50-
func DetermineName(result string) string {
51-
name := result
51+
func DetermineName(choice string, path string) string {
52+
session, _ := tmux.FindSession(choice)
53+
if session != nil {
54+
return session.Name
55+
}
56+
5257
// TODO: parent directory config option detection
53-
pathName := nameFromPath(result)
58+
pathName := nameFromPath(path)
5459
if pathName != "" {
55-
name = pathName
60+
return convertToValidName(pathName)
5661
}
57-
return convertToValidName(name)
62+
63+
return convertToValidName(choice)
5864
}

session/determine.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ func Determine(choice string, config *config.Config) (s Session, err error) {
1919
}
2020
s.Path = path
2121

22-
sessionName := name.DetermineName(path)
22+
sessionName := name.DetermineName(choice, path)
2323
if sessionName == "" {
2424
log.Fatal("Couldn't determine the session name", err)
2525
return s, fmt.Errorf(

session/path.go

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,16 @@ func DeterminePath(choice string) (string, error) {
3030
return fullPath, nil
3131
}
3232

33-
isSession, sessionPath := tmux.IsSession(fullPath)
34-
if isSession && sessionPath != "" {
35-
return sessionPath, nil
33+
session, err := tmux.FindSession(fullPath)
34+
if err != nil {
35+
return "", fmt.Errorf(
36+
"couldn't determine the path for %q: %w",
37+
choice,
38+
err,
39+
)
40+
}
41+
if session != nil {
42+
return session.Path, nil
3643
}
3744

3845
zoxideResult, err := zoxide.Query(fullPath)

tmux/list.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ type TmuxSession struct {
3232
Marked bool // 1 if this session contains the marked pane
3333
}
3434

35+
var separator = "::"
36+
3537
func format() string {
3638
variables := []string{
3739
"#{session_activity}",
@@ -57,7 +59,7 @@ func format() string {
5759
"#{session_windows}",
5860
}
5961

60-
return strings.Join(variables, " ")
62+
return strings.Join(variables, separator)
6163
}
6264

6365
type Options struct {
@@ -67,7 +69,7 @@ type Options struct {
6769
func processSessions(o Options, sessionList []string) []*TmuxSession {
6870
sessions := make([]*TmuxSession, 0, len(sessionList))
6971
for _, line := range sessionList {
70-
fields := strings.Split(line, " ") // Strings split by single space
72+
fields := strings.Split(line, separator) // Strings split by single space
7173

7274
if len(fields) != 21 {
7375
continue

tmux/list_test.go

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,14 +7,14 @@ import (
77
)
88

99
func TestFormat(t *testing.T) {
10-
want := "#{session_activity} #{session_alerts} #{session_attached}" +
11-
" #{session_attached_list} #{session_created} #{session_format} " +
12-
"#{session_group} #{session_group_attached} " +
13-
"#{session_group_attached_list} #{session_group_list} " +
14-
"#{session_group_many_attached} #{session_group_size} " +
15-
"#{session_grouped} #{session_id} #{session_last_attached} " +
16-
"#{session_many_attached} #{session_marked} #{session_name} " +
17-
"#{session_path} #{session_stack} #{session_windows}"
10+
want := "#{session_activity}::#{session_alerts}::#{session_attached}::" +
11+
"#{session_attached_list}::#{session_created}::#{session_format}::" +
12+
"#{session_group}::#{session_group_attached}::" +
13+
"#{session_group_attached_list}::#{session_group_list}::" +
14+
"#{session_group_many_attached}::#{session_group_size}::" +
15+
"#{session_grouped}::#{session_id}::#{session_last_attached}::" +
16+
"#{session_many_attached}::#{session_marked}::#{session_name}::" +
17+
"#{session_path}::#{session_stack}::#{session_windows}"
1818
got := format()
1919
require.Equal(t, want, got)
2020
}
@@ -33,13 +33,13 @@ func TestProcessSessions(t *testing.T) {
3333
}{
3434
"Single active session": {
3535
Input: []string{
36-
"1705879337 1 /dev/ttys000 1705878987 1 0 $2 1705879328 0 0 session-1 /some/test/path 1 1",
36+
"1705879337::::1::/dev/ttys000::1705878987::1::::::::::::::0::$2::1705879328::0::0::session-1::/some/test/path::1::1",
3737
},
3838
Expected: make([]*TmuxSession, 1),
3939
},
4040
"Hide single active session": {
4141
Input: []string{
42-
"1705879337 1 /dev/ttys000 1705878987 1 0 $2 1705879328 0 0 session-1 /some/test/path 1 1",
42+
"1705879337::::1::/dev/ttys000::1705878987::1::::::::::::::0::$2::1705879328::0::0::session-1::/some/test/path::1::1",
4343
},
4444
Options: Options{
4545
HideAttached: true,
@@ -48,21 +48,21 @@ func TestProcessSessions(t *testing.T) {
4848
},
4949
"Single inactive session": {
5050
Input: []string{
51-
"1705879002 0 1705878987 1 0 $2 1705878987 0 0 session-1 /some/test/path 1 1",
51+
"1705879002::::0::::1705878987::1::::::::::::::0::$2::1705878987::0::0::session-1::/some/test/path::1::1",
5252
},
5353
Expected: make([]*TmuxSession, 1),
5454
},
5555
"Two inactive session": {
5656
Input: []string{
57-
"1705879002 0 1705878987 1 0 $2 1705878987 0 0 session-1 /some/test/path 1 1",
58-
"1705879063 0 1705879002 1 0 $3 1705879002 0 0 session-2 /some/other/test/path 1 1",
57+
"1705879002::::0::::1705878987::1::::::::::::::0::$2::1705878987::0::0::session-1::/some/test/path::1::1",
58+
"1705879063::::0::::1705879002::1::::::::::::::0::$3::1705879002::0::0::session-2::/some/other/test/path::1::1",
5959
},
6060
Expected: make([]*TmuxSession, 2),
6161
},
6262
"Two active session": {
6363
Input: []string{
64-
"1705879337 1 /dev/ttys000 1705878987 1 0 $2 1705879328 0 0 session-1 /some/test/path 1 1",
65-
"1705879337 1 /dev/ttys000 1705878987 1 0 $2 1705879328 0 0 session-1 /some/test/path 1 1",
64+
"1705879337::::1::/dev/ttys000::1705878987::1::::::::::::::0::$2::1705879328::0::0::session-1::/some/test/path::1::1",
65+
"1705879337::::1::/dev/ttys000::1705878987::1::::::::::::::0::$2::1705879328::0::0::session-1::/some/test/path::1::1",
6666
},
6767
Expected: make([]*TmuxSession, 2),
6868
},
@@ -71,8 +71,8 @@ func TestProcessSessions(t *testing.T) {
7171
},
7272
"Invalid LastAttached (Issue 34)": {
7373
Input: []string{
74-
"1705879002 0 1705878987 1 0 $2 1705878987 0 0 session-1 /some/test/path 1 1",
75-
"1705879063 0 1705879002 1 0 $3 0 0 session-2 /some/other/test/path 1 1",
74+
"1705879002::::0::::1705878987::1::::::::::::::0::$2::1705878987::0::0::session-1::/some/test/path::1::1",
75+
"1705879063::::0::::1705879002::1::::::::::::::0::$3::::0::0::session-2::/some/other/test/path::1::1",
7676
},
7777
Expected: make([]*TmuxSession, 2),
7878
},
@@ -89,8 +89,8 @@ func TestProcessSessions(t *testing.T) {
8989
func BenchmarkProcessSessions(b *testing.B) {
9090
for n := 0; n < b.N; n++ {
9191
processSessions(Options{}, []string{
92-
"1705879337 1 /dev/ttys000 1705878987 1 0 $2 1705879328 0 0 session-1 /some/test/path 1 1",
93-
"1705879337 1 /dev/ttys000 1705878987 1 0 $2 1705879328 0 0 session-1 /some/test/path 1 1",
92+
"1705879337::::1::/dev/ttys000::1705878987::1::::::::::::::0::$2::1705879328::0::0::session-1::/some/test/path::1::1",
93+
"1705879337::::1::/dev/ttys000::1705878987::1::::::::::::::0::$2::1705879328::0::0::session-1::/some/test/path::1::1",
9494
})
9595
}
9696
}

tmux/tmux.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -68,18 +68,18 @@ func isAttached() bool {
6868
return len(os.Getenv("TMUX")) > 0
6969
}
7070

71-
func IsSession(session string) (bool, string) {
71+
func FindSession(session string) (*TmuxSession, error) {
7272
sessions, err := List(Options{})
7373
if err != nil {
74-
return false, ""
74+
return nil, err
7575
}
7676

7777
for _, s := range sessions {
7878
if s.Name == session {
79-
return true, s.Path
79+
return s, nil
8080
}
8181
}
82-
return false, ""
82+
return nil, nil
8383
}
8484

8585
func attachSession(session string) error {
@@ -146,8 +146,8 @@ func Connect(
146146
sessionPath string,
147147
config *config.Config,
148148
) error {
149-
isSession, _ := IsSession(s.Name)
150-
if !isSession {
149+
session, _ := FindSession(s.Name)
150+
if session == nil {
151151
_, err := NewSession(s)
152152
if err != nil {
153153
return fmt.Errorf(

0 commit comments

Comments
 (0)