Skip to content

Commit

Permalink
feat: add icon support
Browse files Browse the repository at this point in the history
  • Loading branch information
joshmedeski committed Aug 6, 2024
1 parent ee34f64 commit ae2d6c5
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 8 deletions.
58 changes: 58 additions & 0 deletions icon/icon.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package icon

import (
"fmt"
"strings"

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

type Icon interface {
AddIcon(session model.SeshSession) string
RemoveIcon(name string) string
}

type RealIcon struct {
config model.Config
}

func NewIcon(config model.Config) Icon {
return &RealIcon{config}
}

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

func ansiString(code int, s string) string {
return fmt.Sprintf("\033[%dm%s\033[39m", code, s)
}

func (i *RealIcon) AddIcon(s model.SeshSession) string {
var icon string
var colorCode int
switch s.Src {
case "tmux":
icon = tmuxIcon
colorCode = 34 // blue
case "zoxide":
icon = zoxideIcon
colorCode = 36 // cyan
case "config":
icon = configIcon
colorCode = 90 // gray
}
if icon != "" {
return fmt.Sprintf("%s %s", ansiString(colorCode, icon), s.Name)
}
return s.Name
}

func (i *RealIcon) RemoveIcon(name string) string {
if strings.HasPrefix(name, tmuxIcon) || strings.HasPrefix(name, zoxideIcon) || strings.HasPrefix(name, configIcon) {
return name[4:]
}
return name
}
7 changes: 5 additions & 2 deletions seshcli/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,12 @@ import (
"fmt"

"github.com/joshmedeski/sesh/connector"
"github.com/joshmedeski/sesh/icon"
"github.com/joshmedeski/sesh/model"
cli "github.com/urfave/cli/v2"
)

func Connect(c connector.Connector) *cli.Command {
func Connect(c connector.Connector, i icon.Icon) *cli.Command {
return &cli.Command{
Name: "connect",
Aliases: []string{"cn"},
Expand All @@ -36,7 +37,9 @@ func Connect(c connector.Connector) *cli.Command {
return nil
}
opts := model.ConnectOpts{Switch: cCtx.Bool("switch"), Command: cCtx.String("command")}
if connection, err := c.Connect(name, opts); err != nil {
trimmedName := i.RemoveIcon(name)
fmt.Println(trimmedName)
if connection, err := c.Connect(trimmedName, opts); err != nil {
// TODO: print to logs?
return err
} else {
Expand Down
13 changes: 9 additions & 4 deletions seshcli/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,12 @@ package seshcli
import (
"fmt"

"github.com/joshmedeski/sesh/icon"
"github.com/joshmedeski/sesh/lister"
cli "github.com/urfave/cli/v2"
)

func List(s lister.Lister) *cli.Command {
func List(icon icon.Icon, list lister.Lister) *cli.Command {
return &cli.Command{
Name: "list",
Aliases: []string{"l"},
Expand Down Expand Up @@ -42,11 +43,11 @@ func List(s lister.Lister) *cli.Command {
&cli.BoolFlag{
Name: "icons",
Aliases: []string{"i"},
Usage: "show Nerd Font icons",
Usage: "show icons",
},
},
Action: func(cCtx *cli.Context) error {
sessions, err := s.List(lister.ListOptions{
sessions, err := list.List(lister.ListOptions{
Config: cCtx.Bool("config"),
HideAttached: cCtx.Bool("hide-attached"),
Icons: cCtx.Bool("icons"),
Expand All @@ -59,7 +60,11 @@ func List(s lister.Lister) *cli.Command {
}

for _, i := range sessions.OrderedIndex {
fmt.Println(sessions.Directory[i].Name)
name := sessions.Directory[i].Name
if cCtx.Bool("icons") {
name = icon.AddIcon(sessions.Directory[i])
}
fmt.Println(name)
}

return nil
Expand Down
6 changes: 4 additions & 2 deletions seshcli/seshcli.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/joshmedeski/sesh/execwrap"
"github.com/joshmedeski/sesh/git"
"github.com/joshmedeski/sesh/home"
"github.com/joshmedeski/sesh/icon"
"github.com/joshmedeski/sesh/lister"
"github.com/joshmedeski/sesh/namer"
"github.com/joshmedeski/sesh/oswrap"
Expand Down Expand Up @@ -48,14 +49,15 @@ func App(version string) cli.App {
startup := startup.NewStartup(config, lister, tmux)
namer := namer.NewNamer(path, git)
connector := connector.NewConnector(config, dir, home, lister, namer, startup, tmux, zoxide)
icon := icon.NewIcon(config)

return cli.App{
Name: "sesh",
Version: version,
Usage: "Smart session manager for the terminal",
Commands: []*cli.Command{
List(lister),
Connect(connector),
List(icon, lister),
Connect(connector, icon),
Clone(),
},
}
Expand Down

0 comments on commit ae2d6c5

Please sign in to comment.