aboutsummaryrefslogtreecommitdiff
path: root/main.go
blob: 9656146baee17d7da60f35e05cfd51f17e5e814d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
package main

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

	tea "github.com/charmbracelet/bubbletea"
)

type item struct {
	title    string
	contents string
}

func (i item) Title() string       { return i.title }
func (i item) Description() string { return i.contents }
func (i item) FilterValue() string { return i.title }

func main() {
	if e := runProgram(); e != nil {
		fmt.Println(e)
		os.Exit(1)
	}
}

func runProgram() error {

	model, err := tea.NewProgram(StartSelecting(), tea.WithMouseCellMotion()).Run()
	if err != nil {
		return fmt.Errorf("Error running program: %s", err)
	}
	if confirm, ok := model.(confirmModel); ok && confirm.doRun {
		sh, err := exec.LookPath("sh")
		if err != nil {
			return fmt.Errorf("could not find sh: %s", err)
		}

		args := []string{"-v", "-c"}
		args = append(args, "set -v; "+confirm.selected.contents)

		err = syscall.Exec(sh, args, os.Environ())
		if err != nil {
			return fmt.Errorf("could not exec: %s", err)
		}
	}

	return nil
}