2022-04-20 12:40:46 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2022-06-22 19:42:00 +02:00
|
|
|
"flag"
|
2022-04-20 12:40:46 +02:00
|
|
|
"fmt"
|
2022-07-12 15:05:31 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"log"
|
2022-06-22 19:42:00 +02:00
|
|
|
"os"
|
2022-07-12 15:05:31 +02:00
|
|
|
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
2022-08-13 21:10:22 +02:00
|
|
|
viper "github.com/spf13/viper"
|
2022-04-20 12:40:46 +02:00
|
|
|
)
|
|
|
|
|
2022-06-22 19:42:00 +02:00
|
|
|
const help = `go-sh-manymanuals: ???
|
|
|
|
???
|
|
|
|
|
|
|
|
Options:
|
|
|
|
-h output help
|
|
|
|
`
|
|
|
|
|
|
|
|
var portFlag int
|
|
|
|
var helpFlag bool
|
|
|
|
|
2022-08-13 21:10:22 +02:00
|
|
|
type Configuration struct {
|
|
|
|
ManualDir string
|
|
|
|
}
|
|
|
|
|
2022-06-22 19:42:00 +02:00
|
|
|
func handleCliFlags() {
|
|
|
|
flag.BoolVar(&helpFlag, "h", false, "output help")
|
|
|
|
flag.Parse()
|
|
|
|
}
|
|
|
|
|
2022-07-12 15:05:31 +02:00
|
|
|
type model struct {
|
2022-07-13 23:42:39 +02:00
|
|
|
manuals []string
|
|
|
|
cursor int
|
2022-07-12 15:05:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
func initialModel() model {
|
2022-08-14 10:54:31 +02:00
|
|
|
folder := viper.Get("ShareManualDir").(string)
|
2022-08-13 21:10:22 +02:00
|
|
|
manuals, err := gatherManuals(folder)
|
2022-08-14 10:19:02 +02:00
|
|
|
|
2022-07-12 15:05:31 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Fatalf("unable to read manuals: %s", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
|
|
|
|
|
|
|
return model{
|
2022-07-13 23:42:39 +02:00
|
|
|
manuals: manuals,
|
2022-07-12 15:05:31 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-14 10:19:02 +02:00
|
|
|
func gatherManuals(directory string) ([]string, error) {
|
|
|
|
fileInfos, err := ioutil.ReadDir(fmt.Sprintf("./%s", directory))
|
2022-07-12 15:05:31 +02:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
var filenames []string
|
|
|
|
for _, fileInfo := range fileInfos {
|
2022-08-14 10:19:02 +02:00
|
|
|
filename := fileInfo.Name()
|
|
|
|
if fileInfo.IsDir() {
|
|
|
|
filename = fmt.Sprintf("📁 %s", fileInfo.Name())
|
|
|
|
}
|
|
|
|
filenames = append(filenames, filename)
|
2022-07-12 15:05:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return filenames, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) Init() tea.Cmd {
|
|
|
|
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 "up", "k":
|
|
|
|
if m.cursor > 0 {
|
|
|
|
m.cursor--
|
|
|
|
}
|
|
|
|
case "down", "j":
|
|
|
|
if m.cursor < len(m.manuals)-1 {
|
|
|
|
m.cursor++
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return m, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m model) View() string {
|
|
|
|
ui := "File listing for current working directory\n\n"
|
|
|
|
|
|
|
|
for i, choice := range m.manuals {
|
|
|
|
cursor := " "
|
|
|
|
if m.cursor == i {
|
|
|
|
cursor = ">"
|
|
|
|
}
|
|
|
|
|
|
|
|
ui += fmt.Sprintf("%s %s\n", cursor, choice)
|
|
|
|
}
|
|
|
|
|
|
|
|
ui += "\nPress q to quit.\n"
|
2022-08-14 10:54:31 +02:00
|
|
|
ui += "\nPress forward or backward to navigate in and out of directories.\n"
|
2022-07-12 15:05:31 +02:00
|
|
|
return ui
|
|
|
|
}
|
|
|
|
|
2022-04-20 12:40:46 +02:00
|
|
|
func main() {
|
2022-06-22 19:42:00 +02:00
|
|
|
handleCliFlags()
|
|
|
|
|
|
|
|
if helpFlag {
|
|
|
|
fmt.Printf(help)
|
|
|
|
os.Exit(0)
|
|
|
|
}
|
|
|
|
|
2022-08-14 10:19:02 +02:00
|
|
|
viper.SetConfigFile("config.json")
|
|
|
|
viper.ReadInConfig()
|
|
|
|
|
2022-07-13 23:40:52 +02:00
|
|
|
p := tea.NewProgram(initialModel(), tea.WithAltScreen())
|
2022-07-12 15:05:31 +02:00
|
|
|
if err := p.Start(); err != nil {
|
|
|
|
fmt.Printf("Alas, there's been an error: %v", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2022-04-20 12:40:46 +02:00
|
|
|
}
|