-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.go
More file actions
120 lines (96 loc) · 2.78 KB
/
main.go
File metadata and controls
120 lines (96 loc) · 2.78 KB
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
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
package main
import (
"fmt"
"os"
"os/exec"
"strings"
tea "github.com/charmbracelet/bubbletea"
"github.com/charmbracelet/lipgloss"
)
var title_style = lipgloss.NewStyle().
Bold(true).
Foreground(lipgloss.Color("#c6a0f6"))
var banner_style = lipgloss.NewStyle().
BorderStyle(lipgloss.RoundedBorder()).
BorderForeground(lipgloss.Color("#c6a0f6"))
var program_style = lipgloss.NewStyle().
Bold(false).
Foreground(lipgloss.Color("#a6da95"))
var exit_style = lipgloss.NewStyle().
Bold(false).
Foreground(lipgloss.Color("#ed8796"))
var footer_style = lipgloss.NewStyle().
Bold(false).
Foreground(lipgloss.Color("#5b6078"))
type model struct {
run bool
program string
width int
height int
exit_msg string
}
type FinishedMsg struct { err error }
func initialModel() model {
return model{
width: 0,
height: 0,
}
}
func (m model) Init() tea.Cmd {
// Just return `nil`, which means "no I/O right now, please."
return nil
}
func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case "ctrl+c", "q":
return m, tea.Quit
case "enter", " ":
return m, tea.ExecProcess(exec.Command("zsh", "-ci", m.program), func(err error) tea.Msg {
return FinishedMsg{err: err}
})
}
case tea.WindowSizeMsg:
m.width = msg.Width
m.height = msg.Height
case FinishedMsg:
if msg.err != nil {
m.exit_msg = "Last run: " + msg.err.Error()
}
}
return m, nil
}
func (m model) View() string {
// The header
// text = title_style.Render("Ready when you are...")
banner_style = banner_style.Width(m.width-2).Height(m.height-2).PaddingTop(m.height/2-1)
// title_style = title_style.Width(m.width-2)
// program_style = program_style.Width(m.width-2)
// footer_style = footer_style.Width(m.width-2)
center := lipgloss.NewStyle().Align(lipgloss.Center).Width(m.width-2)
text := lipgloss.JoinVertical(lipgloss.Center,
lipgloss.JoinHorizontal(lipgloss.Center,
title_style.Render("Waiting to run: "),
program_style.Render(m.program),
),
exit_style.Render("\n" + m.exit_msg),
footer_style.Render("\nPress [Enter] to continue... [Ctrl+C | q] to cancel"),
)
text = center.Render(text)
return banner_style.Render(text)
}
func main() {
program := strings.Join(os.Args[1:], " ")
m := model{
run: false,
program: program,
width: 0,
height: 0,
}
p := tea.NewProgram(m, tea.WithAltScreen())
if _, err := p.Run(); err != nil {
fmt.Printf("Alas, there's been an error: %v", err)
os.Exit(1)
}
}