Skip to content

Commit 3a7d035

Browse files
authored
feat: add icons and json flags to list (#74)
* chore: remove choose command * In favor of the user customizing fzf or any other tool on their own * feat: restructure list formatting and add icon support * chore: reduce lines on tmux session struct * feat: add json support to sesh list
1 parent be2badd commit 3a7d035

File tree

11 files changed

+145
-178
lines changed

11 files changed

+145
-178
lines changed

cmds/choose.go

Lines changed: 0 additions & 81 deletions
This file was deleted.

cmds/list.go

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import (
66

77
cli "github.com/urfave/cli/v2"
88

9+
"github.com/joshmedeski/sesh/icons"
10+
"github.com/joshmedeski/sesh/json"
911
"github.com/joshmedeski/sesh/session"
1012
)
1113

@@ -16,6 +18,11 @@ func List() *cli.Command {
1618
Usage: "List sessions",
1719
UseShortOptionHandling: true,
1820
Flags: []cli.Flag{
21+
&cli.BoolFlag{
22+
Name: "json",
23+
Aliases: []string{"j"},
24+
Usage: "output as json",
25+
},
1926
&cli.BoolFlag{
2027
Name: "tmux",
2128
Aliases: []string{"t"},
@@ -31,6 +38,11 @@ func List() *cli.Command {
3138
Aliases: []string{"H"},
3239
Usage: "don't show currently attached sessions",
3340
},
41+
&cli.BoolFlag{
42+
Name: "icons",
43+
Aliases: []string{"i"},
44+
Usage: "show Nerd Font icons",
45+
},
3446
},
3547
Action: func(cCtx *cli.Context) error {
3648
o := session.Options{
@@ -40,7 +52,23 @@ func List() *cli.Command {
4052
Tmux: cCtx.Bool("tmux"),
4153
Zoxide: cCtx.Bool("zoxide"),
4254
})
43-
fmt.Println(strings.Join(sessions, "\n"))
55+
56+
useIcons := cCtx.Bool("icons")
57+
result := make([]string, len(sessions))
58+
for i, session := range sessions {
59+
if useIcons {
60+
result[i] = icons.PrintWithIcon(session)
61+
} else {
62+
result[i] = session.Name
63+
}
64+
}
65+
66+
useJson := cCtx.Bool("json")
67+
if useJson {
68+
fmt.Println(json.List(sessions))
69+
} else {
70+
fmt.Println(strings.Join(result, "\n"))
71+
}
4472
return nil
4573
},
4674
}

connect/connect.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,10 @@ package connect
22

33
import (
44
"fmt"
5+
"strings"
56

67
"github.com/joshmedeski/sesh/config"
8+
"github.com/joshmedeski/sesh/icons"
79
"github.com/joshmedeski/sesh/session"
810
"github.com/joshmedeski/sesh/tmux"
911
"github.com/joshmedeski/sesh/zoxide"
@@ -15,10 +17,15 @@ func Connect(
1517
command string,
1618
config *config.Config,
1719
) error {
20+
if strings.HasPrefix(choice, icons.TmuxIcon) || strings.HasPrefix(choice, icons.ZoxideIcon) {
21+
choice = choice[4:]
22+
}
23+
1824
session, err := session.Determine(choice, config)
1925
if err != nil {
2026
return fmt.Errorf("unable to connect to %q: %w", choice, err)
2127
}
28+
2229
if err = zoxide.Add(session.Path); err != nil {
2330
return fmt.Errorf("unable to connect to %q: %w", choice, err)
2431
}

icons/icons.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package icons
2+
3+
import (
4+
"fmt"
5+
6+
"github.com/joshmedeski/sesh/session"
7+
)
8+
9+
// TODO: add to config to allow for custom icons
10+
var (
11+
ZoxideIcon string = ""
12+
TmuxIcon string = ""
13+
)
14+
15+
func ansiString(code int, s string) string {
16+
return fmt.Sprintf("\033[%dm%s\033[39m", code, s)
17+
}
18+
19+
func PrintWithIcon(s session.Session) string {
20+
icon := ZoxideIcon
21+
colorCode := 36 // cyan
22+
if s.Src == "tmux" {
23+
icon = TmuxIcon
24+
colorCode = 34 // blue
25+
}
26+
return fmt.Sprintf("%s %s", ansiString(colorCode, icon), s.Name)
27+
}

json/json.go

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package json
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
7+
"github.com/joshmedeski/sesh/session"
8+
)
9+
10+
func List(sessions []session.Session) string {
11+
jsonSessions, err := json.Marshal(sessions)
12+
if err != nil {
13+
fmt.Printf(
14+
"Couldn't list sessions as json: %s\n",
15+
err,
16+
)
17+
}
18+
return string(jsonSessions)
19+
}

session/name.go renamed to name/name.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,10 @@
1-
package session
1+
package name
22

33
import (
44
"path"
55
"path/filepath"
66
"strings"
77

8-
"github.com/joshmedeski/sesh/config"
98
"github.com/joshmedeski/sesh/git"
109
)
1110

@@ -48,7 +47,7 @@ func nameFromGit(result string) string {
4847
return nameFromGit
4948
}
5049

51-
func DetermineName(result string, config *config.Config) string {
50+
func DetermineName(result string) string {
5251
name := result
5352
// TODO: parent directory config option detection
5453
pathName := nameFromPath(result)

seshcli/seshcli.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,6 @@ func App(version string) cli.App {
1313
Usage: "Smart session manager for the terminal",
1414
Commands: []*cli.Command{
1515
cmds.List(),
16-
cmds.Choose(),
1716
cmds.Connect(),
1817
cmds.Clone(),
1918
},

session/determine.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"log"
66

77
"github.com/joshmedeski/sesh/config"
8+
"github.com/joshmedeski/sesh/name"
89
)
910

1011
func Determine(choice string, config *config.Config) (s Session, err error) {
@@ -18,15 +19,15 @@ func Determine(choice string, config *config.Config) (s Session, err error) {
1819
}
1920
s.Path = path
2021

21-
name := DetermineName(path, config)
22-
if name == "" {
22+
sessionName := name.DetermineName(path)
23+
if sessionName == "" {
2324
log.Fatal("Couldn't determine the session name", err)
2425
return s, fmt.Errorf(
2526
"couldn't determine the session name for %q",
2627
choice,
2728
)
2829
}
29-
s.Name = name
30+
s.Name = sessionName
3031

3132
return s, nil
3233
}

session/list.go

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,30 @@ package session
33
import (
44
"fmt"
55
"os"
6+
"reflect"
67

78
"github.com/joshmedeski/sesh/tmux"
89
"github.com/joshmedeski/sesh/zoxide"
910
)
1011

1112
type Options struct {
1213
HideAttached bool
14+
Json bool
1315
}
1416

15-
func List(o Options, srcs Srcs) []string {
16-
var sessions []string
17+
func checkAnyTrue(s interface{}) bool {
18+
val := reflect.ValueOf(s)
19+
for i := 0; i < val.NumField(); i++ {
20+
field := val.Field(i)
21+
if field.Kind() == reflect.Bool && field.Bool() {
22+
return true
23+
}
24+
}
25+
return false
26+
}
27+
28+
func List(o Options, srcs Srcs) []Session {
29+
var sessions []Session
1730
anySrcs := checkAnyTrue(srcs)
1831

1932
tmuxSessions := make([]*tmux.TmuxSession, 0)
@@ -26,11 +39,17 @@ func List(o Options, srcs Srcs) []string {
2639
fmt.Println("Error:", err)
2740
os.Exit(1)
2841
}
29-
tmuxSessionNames := make([]string, len(tmuxList))
42+
tmuxSessionNames := make([]Session, len(tmuxList))
3043
for i, session := range tmuxSessions {
3144
// TODO: allow support for connect as well (PrettyName?)
3245
// tmuxSessionNames[i] = session.Name + " (" + convert.PathToPretty(session.Path) + ")"
33-
tmuxSessionNames[i] = session.Name
46+
tmuxSessionNames[i] = Session{
47+
Src: "tmux",
48+
Name: session.Name,
49+
Path: session.Path,
50+
Attached: session.Attached,
51+
Windows: session.Windows,
52+
}
3453
}
3554
sessions = append(sessions, tmuxSessionNames...)
3655
}
@@ -41,9 +60,14 @@ func List(o Options, srcs Srcs) []string {
4160
fmt.Println("Error:", err)
4261
os.Exit(1)
4362
}
44-
zoxideResultNames := make([]string, len(results))
63+
zoxideResultNames := make([]Session, len(results))
4564
for i, result := range results {
46-
zoxideResultNames[i] = result.Name
65+
zoxideResultNames[i] = Session{
66+
Src: "zoxide",
67+
Name: result.Name,
68+
Path: result.Path,
69+
Score: result.Score,
70+
}
4771
}
4872
sessions = append(sessions, zoxideResultNames...)
4973
}

session/session.go

Lines changed: 6 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,30 +1,15 @@
11
package session
22

3-
import (
4-
"reflect"
5-
)
6-
73
type Session struct {
8-
// tmux or zoxide
9-
Src string
10-
// The display name
11-
Name string
12-
// The absolute directory path
13-
Path string
4+
Src string // tmux or zoxide
5+
Name string // The display name
6+
Path string // The absolute directory path
7+
Score float64 // The score of the session (from Zoxide)
8+
Attached int // Whether the session is currently attached
9+
Windows int // The number of windows in the session
1410
}
1511

1612
type Srcs struct {
1713
Tmux bool
1814
Zoxide bool
1915
}
20-
21-
func checkAnyTrue(s interface{}) bool {
22-
val := reflect.ValueOf(s)
23-
for i := 0; i < val.NumField(); i++ {
24-
field := val.Field(i)
25-
if field.Kind() == reflect.Bool && field.Bool() {
26-
return true
27-
}
28-
}
29-
return false
30-
}

0 commit comments

Comments
 (0)