ditty/main.go

130 lines
2.1 KiB
Go

package main
import (
"flag"
"fmt"
"log"
"net/http"
_ "net/http/pprof"
"os"
"os/signal"
"path/filepath"
"strings"
"syscall"
"time"
)
const (
volumeBase = 2
version = "0.0.0"
versionInfo = `ditty - Audio player - v` + version + `
https://git.sr.ht/~tslocum/ditty
The MIT License (MIT)
Copyright (c) 2020 Trevor Slocum <trevor@rocketnine.space>
`
)
var (
printVersionInfo bool
debugAddress string
done = make(chan bool)
)
func main() {
log.SetFlags(0)
flag.BoolVar(&printVersionInfo, "version", false, "print version information and exit")
flag.StringVar(&debugAddress, "debug-address", "", "address to serve debug info")
flag.Parse()
if printVersionInfo {
fmt.Print(versionInfo)
return
}
if debugAddress != "" {
go func() {
log.Fatal(http.ListenAndServe(debugAddress, nil))
}()
}
err := initTUI()
if err != nil {
log.Fatalf("failed to initialize terminal user interface: %s", err)
}
sigc := make(chan os.Signal, 1)
signal.Notify(sigc,
syscall.SIGINT,
syscall.SIGTERM)
go func() {
<-sigc
done <- true
}()
go func() {
err := app.Run()
if err != nil {
log.Fatal(err)
}
done <- true
}()
startPath := strings.Join(flag.Args(), " ")
if startPath == "" {
wd, err := os.Getwd()
if err != nil || wd == "" {
homeDir, err := os.UserHomeDir()
if err == nil && homeDir != "" {
startPath = homeDir
}
} else {
startPath = wd
}
}
if startPath == "" {
log.Fatal("supply a folder to browse initially")
}
fileInfo, err := os.Stat(startPath)
if err != nil {
log.Fatal(err)
}
if fileInfo.IsDir() {
browseFolder(startPath)
} else {
browseFolder(filepath.Dir(startPath))
audioFile, err := openFile(strings.Join(flag.Args(), " "))
if err != nil {
statusText = err.Error()
app.QueueUpdateDraw(updateMain)
} else {
play(audioFile)
}
}
defer func() {
if playingStreamer != nil {
playingStreamer.Close()
}
if app != nil {
app.Stop()
}
}()
t := time.NewTicker(time.Second)
for {
select {
case <-done:
return
case <-t.C:
app.QueueUpdateDraw(updateStatus)
}
}
}