Skip to content

Commit ae2d6c5

Browse files
committed
feat: add icon support
1 parent ee34f64 commit ae2d6c5

File tree

4 files changed

+76
-8
lines changed

4 files changed

+76
-8
lines changed

icon/icon.go

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package icon
2+
3+
import (
4+
"fmt"
5+
"strings"
6+
7+
"github.com/joshmedeski/sesh/model"
8+
)
9+
10+
type Icon interface {
11+
AddIcon(session model.SeshSession) string
12+
RemoveIcon(name string) string
13+
}
14+
15+
type RealIcon struct {
16+
config model.Config
17+
}
18+
19+
func NewIcon(config model.Config) Icon {
20+
return &RealIcon{config}
21+
}
22+
23+
var (
24+
zoxideIcon string = ""
25+
tmuxIcon string = ""
26+
configIcon string = ""
27+
)
28+
29+
func ansiString(code int, s string) string {
30+
return fmt.Sprintf("\033[%dm%s\033[39m", code, s)
31+
}
32+
33+
func (i *RealIcon) AddIcon(s model.SeshSession) string {
34+
var icon string
35+
var colorCode int
36+
switch s.Src {
37+
case "tmux":
38+
icon = tmuxIcon
39+
colorCode = 34 // blue
40+
case "zoxide":
41+
icon = zoxideIcon
42+
colorCode = 36 // cyan
43+
case "config":
44+
icon = configIcon
45+
colorCode = 90 // gray
46+
}
47+
if icon != "" {
48+
return fmt.Sprintf("%s %s", ansiString(colorCode, icon), s.Name)
49+
}
50+
return s.Name
51+
}
52+
53+
func (i *RealIcon) RemoveIcon(name string) string {
54+
if strings.HasPrefix(name, tmuxIcon) || strings.HasPrefix(name, zoxideIcon) || strings.HasPrefix(name, configIcon) {
55+
return name[4:]
56+
}
57+
return name
58+
}

seshcli/connect.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@ import (
55
"fmt"
66

77
"github.com/joshmedeski/sesh/connector"
8+
"github.com/joshmedeski/sesh/icon"
89
"github.com/joshmedeski/sesh/model"
910
cli "github.com/urfave/cli/v2"
1011
)
1112

12-
func Connect(c connector.Connector) *cli.Command {
13+
func Connect(c connector.Connector, i icon.Icon) *cli.Command {
1314
return &cli.Command{
1415
Name: "connect",
1516
Aliases: []string{"cn"},
@@ -36,7 +37,9 @@ func Connect(c connector.Connector) *cli.Command {
3637
return nil
3738
}
3839
opts := model.ConnectOpts{Switch: cCtx.Bool("switch"), Command: cCtx.String("command")}
39-
if connection, err := c.Connect(name, opts); err != nil {
40+
trimmedName := i.RemoveIcon(name)
41+
fmt.Println(trimmedName)
42+
if connection, err := c.Connect(trimmedName, opts); err != nil {
4043
// TODO: print to logs?
4144
return err
4245
} else {

seshcli/list.go

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,12 @@ package seshcli
33
import (
44
"fmt"
55

6+
"github.com/joshmedeski/sesh/icon"
67
"github.com/joshmedeski/sesh/lister"
78
cli "github.com/urfave/cli/v2"
89
)
910

10-
func List(s lister.Lister) *cli.Command {
11+
func List(icon icon.Icon, list lister.Lister) *cli.Command {
1112
return &cli.Command{
1213
Name: "list",
1314
Aliases: []string{"l"},
@@ -42,11 +43,11 @@ func List(s lister.Lister) *cli.Command {
4243
&cli.BoolFlag{
4344
Name: "icons",
4445
Aliases: []string{"i"},
45-
Usage: "show Nerd Font icons",
46+
Usage: "show icons",
4647
},
4748
},
4849
Action: func(cCtx *cli.Context) error {
49-
sessions, err := s.List(lister.ListOptions{
50+
sessions, err := list.List(lister.ListOptions{
5051
Config: cCtx.Bool("config"),
5152
HideAttached: cCtx.Bool("hide-attached"),
5253
Icons: cCtx.Bool("icons"),
@@ -59,7 +60,11 @@ func List(s lister.Lister) *cli.Command {
5960
}
6061

6162
for _, i := range sessions.OrderedIndex {
62-
fmt.Println(sessions.Directory[i].Name)
63+
name := sessions.Directory[i].Name
64+
if cCtx.Bool("icons") {
65+
name = icon.AddIcon(sessions.Directory[i])
66+
}
67+
fmt.Println(name)
6368
}
6469

6570
return nil

seshcli/seshcli.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/joshmedeski/sesh/execwrap"
88
"github.com/joshmedeski/sesh/git"
99
"github.com/joshmedeski/sesh/home"
10+
"github.com/joshmedeski/sesh/icon"
1011
"github.com/joshmedeski/sesh/lister"
1112
"github.com/joshmedeski/sesh/namer"
1213
"github.com/joshmedeski/sesh/oswrap"
@@ -48,14 +49,15 @@ func App(version string) cli.App {
4849
startup := startup.NewStartup(config, lister, tmux)
4950
namer := namer.NewNamer(path, git)
5051
connector := connector.NewConnector(config, dir, home, lister, namer, startup, tmux, zoxide)
52+
icon := icon.NewIcon(config)
5153

5254
return cli.App{
5355
Name: "sesh",
5456
Version: version,
5557
Usage: "Smart session manager for the terminal",
5658
Commands: []*cli.Command{
57-
List(lister),
58-
Connect(connector),
59+
List(icon, lister),
60+
Connect(connector, icon),
5961
Clone(),
6062
},
6163
}

0 commit comments

Comments
 (0)