Skip to content

Commit

Permalink
feat: add preview command (#203)
Browse files Browse the repository at this point in the history
- Add a new `sesh preview` command
- Add configuration support for the preview command
  • Loading branch information
danitrap authored Jan 7, 2025
1 parent 023e772 commit 0c3480c
Show file tree
Hide file tree
Showing 41 changed files with 984 additions and 38 deletions.
14 changes: 12 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ In order to integrate with tmux, you can add a binding to your tmux config (`tmu

```sh
bind-key "T" run-shell "sesh connect \"$(
sesh list --icons | fzf-tmux -p 55%,60% \
sesh list --icons | fzf-tmux -p 80%,70% \
--no-sort --ansi --border-label ' sesh ' --prompt '' \
--header ' ^a all ^t tmux ^g configs ^x zoxide ^d tmux kill ^f find' \
--bind 'tab:down,btab:up' \
Expand All @@ -121,6 +121,8 @@ bind-key "T" run-shell "sesh connect \"$(
--bind 'ctrl-x:change-prompt(📁 )+reload(sesh list -z --icons)' \
--bind 'ctrl-f:change-prompt(🔎 )+reload(fd -H -d 2 -t d -E .Trash . ~)' \
--bind 'ctrl-d:execute(tmux kill-session -t {2..})+change-prompt(⚡ )+reload(sesh list --icons)' \
--preview-window 'right:55%' \
--preview 'sesh preview {}'
)\""
```

Expand Down Expand Up @@ -222,9 +224,14 @@ mkdir -p ~/.config/sesh && touch ~/.config/sesh/sesh.toml

The default session can be configured to run a command when connecting to a session. This is useful for running a dev server or starting a tmux plugin.

Additionally, you can define a preview command that runs when previewing the session's directory. This can be handy for displaying files with tools like [eza](https://github.com/eza-community/eza) or [lsd](https://github.com/lsd-rs/lsd).

Note: The `{}` will be automatically replaced with the session's path.

```toml
[default_session]
startup_command = "nvim -c ':Telescope find_files'"
preview_command = "eza --all --git --icons --color=always {}"
```

If you want to disable the default start command on a specific session, you can set `disable_startup_command = true`.
Expand All @@ -235,7 +242,9 @@ A startup command is a command that is run when a session is created. It is usef

**Note:** If you use the `--command/-c` flag, then the startup script will not be run.

I like to use a command that opens nvim on session startup:
I like to use a command that opens nvim on session startup.

You can also define a preview command to display the contents of a specific file using [bat](https://github.com/sharkdp/bat) or any another file previewer of your choice.

```toml
[[session]]
Expand All @@ -247,6 +256,7 @@ startup_command = "ls"
name = "tmux config"
path = "~/c/dotfiles/.config/tmux"
startup_command = "nvim tmux.conf"
preview_command = "bat --color=always ~/c/dotfiles/.config/tmux/tmux.conf"
```

### Listing Configurations
Expand Down
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.

4 changes: 2 additions & 2 deletions configurator/mock_Configurator.go

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

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.

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.

6 changes: 3 additions & 3 deletions 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.

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.

1 change: 1 addition & 0 deletions lister/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ func listConfig(l *RealLister) (model.SeshSessions, error) {
Name: session.Name,
Path: path,
StartupCommand: session.StartupCommand,
PreviewCommand: session.PreviewCommand,
DisableStartupCommand: session.DisableStartCommand,
Tmuxinator: session.Tmuxinator,
}
Expand Down
6 changes: 3 additions & 3 deletions lister/mock_Lister.go

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

2 changes: 1 addition & 1 deletion lister/mock_srcStrategy.go

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

37 changes: 37 additions & 0 deletions ls/ls.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package ls

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

type Ls interface {
ListDirectory(name string) (string, error)
}

type RealLs struct {
config model.Config
shell shell.Shell
}

func NewLs(config model.Config, shell shell.Shell) Ls {
return &RealLs{config, shell}
}

func (g *RealLs) ListDirectory(path string) (string, error) {
command := g.config.DefaultSessionConfig.PreviewCommand
if command == "" {
command = "ls {}"
}

cmdParts, err := g.shell.PrepareCmd(command, map[string]string{"{}": path})
if err != nil {
return "", err
}

cmdOutput, err := g.shell.Cmd(cmdParts[0], cmdParts[1:]...)
if err != nil {
return "", err
}
return cmdOutput, nil
}
88 changes: 88 additions & 0 deletions ls/mock_Ls.go

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

1 change: 1 addition & 0 deletions model/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ type (
StartupCommand string `toml:"startup_command"`
Tmuxp string `toml:"tmuxp"`
Tmuxinator string `toml:"tmuxinator"`
PreviewCommand string `toml:"preview_command"`
}

SessionConfig struct {
Expand Down
1 change: 1 addition & 0 deletions model/sesh_session.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ type (
Path string // The absolute directory path

StartupCommand string // The command to run when the session is started
PreviewCommand string // The command to run when the session is previewed
DisableStartupCommand bool // Ignore the default startup command if present
Tmuxinator string // Name of the tmuxinator config
Attached int // Whether the session is currently attached
Expand Down
2 changes: 1 addition & 1 deletion namer/mock_Namer.go

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

6 changes: 3 additions & 3 deletions oswrap/mock_Os.go

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

2 changes: 1 addition & 1 deletion pathwrap/mock_Path.go

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

41 changes: 41 additions & 0 deletions previewer/config.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package previewer

import (
"github.com/joshmedeski/sesh/lister"
"github.com/joshmedeski/sesh/shell"
)

type ConfigPreviewStrategy struct {
lister lister.Lister
shell shell.Shell
}

func NewConfigStrategy(lister lister.Lister, shell shell.Shell) *ConfigPreviewStrategy {
return &ConfigPreviewStrategy{lister: lister, shell: shell}
}

func (s *ConfigPreviewStrategy) Execute(name string) (string, error) {
session, configExists := s.lister.FindConfigSession(name)
if !configExists {
return "", nil
}

if session.PreviewCommand == "" {
return "", nil
}

replacements := map[string]string{
"{}": session.Path,
}
cmdParts, err := s.shell.PrepareCmd(session.PreviewCommand, replacements)
if err != nil {
return "", err
}

cmdOutput, err := s.shell.Cmd(cmdParts[0], cmdParts[1:]...)
if err != nil {
return "", err
}

return cmdOutput, nil
}
Loading

0 comments on commit 0c3480c

Please sign in to comment.