Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 5 additions & 35 deletions internal/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@ package app

import (
"fmt"
"os"
"os/exec"
"syscall"

"github.com/samber/mo"

"dev/internal/filesystem"
"dev/internal/hooks"
"dev/internal/projects"
"dev/internal/terminal"
"dev/internal/tui"
)

type Config struct {
Icons tui.Icons
Hooks []hooks.Hook
Icons tui.Icons
Terminal terminal.Terminal
}

func Run(args []string, cfg Config, fs filesystem.FileSystem) mo.Result[string] {
Expand All @@ -40,34 +37,7 @@ func Run(args []string, cfg Config, fs filesystem.FileSystem) mo.Result[string]
return mo.Err[string](err)
}

hooks.RunAll(cfg.Hooks, tuiResult.Name)
cfg.Terminal.RenameTab(tuiResult.Name)

return openEditor(tuiResult.Path)
}

func openEditor(path string) mo.Result[string] {
editor, err := getEditorFromEnv().Get()
if err != nil {
return mo.Err[string](err)
}

editorPath, err := exec.LookPath(editor)
if err != nil {
return mo.Err[string](err)
}
if err := syscall.Exec(editorPath, []string{editor, path}, os.Environ()); err != nil {
return mo.Err[string](err)
}

return mo.Ok(editorPath)
}

func getEditorFromEnv() mo.Result[string] {
if editor := os.Getenv("VISUAL"); editor != "" {
return mo.Ok(editor)
}
if editor := os.Getenv("EDITOR"); editor != "" {
return mo.Ok(editor)
}
return mo.Err[string](fmt.Errorf("$VISUAL or $EDITOR is not set"))
return cfg.Terminal.OpenEditor(tuiResult.Path)
}
33 changes: 0 additions & 33 deletions internal/hooks/hooks.go

This file was deleted.

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

import (
"fmt"
"os"
"os/exec"

"github.com/samber/mo"
)

type Terminal interface {
OpenEditor(path string) mo.Result[string]
RenameTab(name string) error
}

func Detect() Terminal {
if os.Getenv("ZELLIJ") != "" {
return &Zellij{}
}
if os.Getenv("TMUX") != "" {
return &Tmux{}
}
return &Default{}
}

type Zellij struct{}

func (z *Zellij) OpenEditor(path string) mo.Result[string] {
return run("zellij", "", "edit", "--cwd", path, "-i", ".")
}

func (z *Zellij) RenameTab(name string) error {
return exec.Command("zellij", "action", "rename-tab", name).Run()
}

type Tmux struct{}

func (t *Tmux) OpenEditor(path string) mo.Result[string] {
editor, err := getEditorFromEnv().Get()
if err != nil {
return mo.Err[string](err)
}
return run("tmux", "", "respawn-pane", "-k", "-c", path, editor, ".")
}

func (t *Tmux) RenameTab(name string) error {
return exec.Command("tmux", "rename-window", name).Run()
}

type Default struct{}

func (d *Default) OpenEditor(path string) mo.Result[string] {
editor, err := getEditorFromEnv().Get()
if err != nil {
return mo.Err[string](err)
}
return run(editor, path, ".")
}

func (d *Default) RenameTab(name string) error {
return nil
}

func run(name string, dir string, args ...string) mo.Result[string] {
path, err := exec.LookPath(name)
if err != nil {
return mo.Err[string](err)
}

cmd := exec.Command(path, args...)
cmd.Dir = dir
cmd.Stdin = os.Stdin
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

if err := cmd.Run(); err != nil {
return mo.Err[string](err)
}

return mo.Ok(path)
}

func getEditorFromEnv() mo.Result[string] {
if editor := os.Getenv("VISUAL"); editor != "" {
return mo.Ok(editor)
}
if editor := os.Getenv("EDITOR"); editor != "" {
return mo.Ok(editor)
}
return mo.Err[string](fmt.Errorf("$VISUAL or $EDITOR is not set"))
}
4 changes: 2 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import (

"dev/internal/app"
"dev/internal/filesystem"
"dev/internal/hooks"
"dev/internal/terminal"
"dev/internal/tui"
)

Expand Down Expand Up @@ -37,7 +37,7 @@ func main() {
Icons: tui.Icons{
Dir: "",
},
Hooks: hooks.Default,
Terminal: terminal.Detect(),
}
fs := &filesystem.RealFileSystem{}

Expand Down
Loading