-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
91 lines (78 loc) · 2.46 KB
/
main.go
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
package main
// A simple example demonstrating the use of multiple text input components
// from the Bubbles component library.
import (
"fmt"
"os"
"strings"
"github.com/roryq/til-prompt/pkg/editor"
"github.com/roryq/til-prompt/pkg/viewer"
"github.com/roryq/til-prompt/internal/manager"
"github.com/alecthomas/kong"
tea "github.com/charmbracelet/bubbletea"
gap "github.com/muesli/go-app-paths"
"github.com/roryq/til-prompt/internal/core"
)
var (
scope = gap.NewScope(gap.User, "til")
)
var cli struct {
New struct{} `cmd default:"1" help:"Create a new TIL entry. (default command)"`
Config struct {
List struct{} `cmd default:"1" help:"List the current configuration. (default sub-command)"`
Edit struct{} `cmd help:"Open the config in your configured $EDITOR"`
} `cmd help:"Manage the current configuration."`
Edit struct {
Keyword string `arg optional help:"optional keyword to search for."`
} `cmd help:"Open today's TIL in your configured editor. You can optionally pass a keyword to search for."`
View struct {
Keyword string `arg optional help:"optional keyword to search for."`
} `cmd help:"Display a styled view of today's TIL. You can optionally pass a keyword to search for."`
}
func main() {
config, err := core.LoadConfig(scope)
if err != nil {
fmt.Printf("could not load config: %s\n", err)
os.Exit(1)
}
ktx := kong.Parse(&cli,
kong.Name("til"),
kong.Description("An interactive prompt for managing TIL entries."))
switch c := ktx.Command(); c {
case "config list":
fmt.Println(config.Formatted())
case "config edit":
err := core.EditConfig(scope)
if err != nil {
fmt.Printf("could not start program: %s\n", err)
os.Exit(1)
}
case "edit", "edit <keyword>", "view", "view <keyword>":
selection := make(chan manager.Selection, 1)
program := new(tea.Program)
if strings.HasPrefix(c, "edit") {
program = tea.NewProgram(manager.NewUI(config, manager.Edit, selection, cli.Edit.Keyword))
} else {
program = tea.NewProgram(manager.NewUI(config, manager.View, selection, cli.View.Keyword))
}
if err := program.Start(); err != nil {
fmt.Printf("could not start editor: %s\n", err)
os.Exit(1)
}
select {
case c := <-selection:
if c.Mode == manager.Edit {
editor.Launch(config, c.Path)
} else {
viewer.Launch(c.Path)
}
default:
os.Exit(0)
}
default:
if err := tea.NewProgram(core.NewUI(config)).Start(); err != nil {
fmt.Printf("could not start program: %s\n", err)
os.Exit(1)
}
}
}