Skip to content

Commit

Permalink
Initial config support
Browse files Browse the repository at this point in the history
  • Loading branch information
raitonoberu committed Apr 22, 2022
1 parent e4b54cc commit 1ae3ff6
Show file tree
Hide file tree
Showing 9 changed files with 472 additions and 359 deletions.
26 changes: 0 additions & 26 deletions cmd/clear.go

This file was deleted.

63 changes: 41 additions & 22 deletions cmd/pipe.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"errors"
"fmt"
"os"
"sptlrx/cookie"
"sptlrx/config"
"sptlrx/pool"
"sptlrx/spotify"
"strings"
Expand All @@ -22,37 +22,58 @@ var (

var pipeCmd = &coral.Command{
Use: "pipe",
Short: "Start printing the current lines in stdout",
Short: "Start printing the current lines to stdout",

RunE: func(cmd *coral.Command, args []string) error {
var clientCookie string
var conf *config.Config

conf, err := config.Load()
if err != nil {
return fmt.Errorf("couldn't load config: %w", err)
}

if conf == nil {
conf = config.New()
}

if FlagCookie != "" {
clientCookie = FlagCookie
conf.Cookie = FlagCookie
} else if envCookie := os.Getenv("SPOTIFY_COOKIE"); envCookie != "" {
clientCookie = envCookie
} else {
clientCookie, _ = cookie.Load()
conf.Cookie = envCookie
}

if clientCookie == "" {
if conf.Cookie == "" {
return errors.New("couldn't find cookie")
}

client, err := spotify.NewClient(clientCookie)
client, err := spotify.NewClient(conf.Cookie)
if err != nil {
return fmt.Errorf("couldn't create client: %w", err)
}
if err := cookie.Save(clientCookie); err != nil {
return fmt.Errorf("couldn't save cookie: %w", err)

if cmd.Flags().Changed("length") {
conf.Pipe.Length = FlagLength
}
if cmd.Flags().Changed("overflow") {
conf.Pipe.Overflow = FlagOverflow
}
if cmd.Flags().Changed("ignore-errors") {
conf.Pipe.IgnoreErrors = FlagIgnoreErrors
}

if cmd.Flags().Changed("tinterval") {
conf.TimerInterval = FlagTimerInterval
}
if cmd.Flags().Changed("uinterval") {
conf.UpdateInterval = FlagUpdateInterval
}

ch := make(chan pool.Update)
go pool.Listen(client, ch)
go pool.Listen(client, conf, ch)

for update := range ch {
if update.Err != nil {
if !FlagIgnoreErrors {
if !conf.Pipe.IgnoreErrors {
fmt.Println(err.Error())
}
continue
Expand All @@ -62,24 +83,23 @@ var pipeCmd = &coral.Command{
continue
}
line := update.Lines[update.Index].Words
if FlagLength == 0 {
if conf.Pipe.Length == 0 {
fmt.Println(line)
} else {
// TODO: find out if there is a better way to cut the line
switch FlagOverflow {
switch conf.Pipe.Overflow {
case "word":
s := wordwrap.String(line, FlagLength)
s := wordwrap.String(line, conf.Pipe.Length)
fmt.Println(strings.Split(s, "\n")[0])
case "none":
s := wrap.String(line, FlagLength)
s := wrap.String(line, conf.Pipe.Length)
fmt.Println(strings.Split(s, "\n")[0])
case "ellipsis":
s := wrap.String(line, FlagLength)
s := wrap.String(line, conf.Pipe.Length)
lines := strings.Split(s, "\n")
if len(lines) == 1 {
fmt.Println(lines[0])
} else {
s := wrap.String(lines[0], FlagLength-3)
s := wrap.String(lines[0], conf.Pipe.Length-3)
fmt.Println(strings.Split(s, "\n")[0] + "...")
}
}
Expand All @@ -90,8 +110,7 @@ var pipeCmd = &coral.Command{
}

func init() {
pipeCmd.Flags().StringVar(&FlagCookie, "cookie", "", "your cookie")
pipeCmd.Flags().IntVar(&FlagLength, "length", 0, "max length of line")
pipeCmd.Flags().StringVar(&FlagOverflow, "overflow", "word", "how to wrap an overflowed line (none/word/ellipsis)")
pipeCmd.Flags().BoolVar(&FlagIgnoreErrors, "ignore-errors", false, "don't print errors")
pipeCmd.Flags().BoolVar(&FlagIgnoreErrors, "ignore-errors", true, "don't print errors")
}
123 changes: 57 additions & 66 deletions cmd/root.go
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,12 @@ import (
"fmt"
"log"
"os"
"sptlrx/cookie"
"sptlrx/config"
"sptlrx/spotify"
"sptlrx/ui"
"strconv"
"strings"

tea "github.com/charmbracelet/bubbletea"
gloss "github.com/charmbracelet/lipgloss"
"github.com/muesli/coral"
)

Expand Down Expand Up @@ -40,8 +38,10 @@ var (
FlagStyleBefore string
FlagStyleCurrent string
FlagStyleAfter string
FlagHAlignment string

FlagHAlignment string
FlagTimerInterval int
FlagUpdateInterval int
)

var rootCmd = &coral.Command{
Expand All @@ -52,51 +52,60 @@ var rootCmd = &coral.Command{
SilenceUsage: true,

RunE: func(cmd *coral.Command, args []string) error {
var clientCookie string
var conf *config.Config

conf, err := config.Load()
if err != nil {
return fmt.Errorf("couldn't load config: %w", err)
}
if conf == nil {
conf = config.New()
fmt.Print(banner)
fmt.Printf("Config will be stored in %s\n", config.Directory)
config.Save(conf)
}

if FlagCookie != "" {
clientCookie = FlagCookie
conf.Cookie = FlagCookie
} else if envCookie := os.Getenv("SPOTIFY_COOKIE"); envCookie != "" {
clientCookie = envCookie
} else {
fileCookie, err := cookie.Load()
if err != nil {
return fmt.Errorf("couldn't load cookie: %w", err)
}
clientCookie = fileCookie
conf.Cookie = envCookie
}

if clientCookie == "" {
fmt.Print(banner)
fmt.Printf("Cookie will be stored in %s\n", cookie.Directory)
if conf.Cookie == "" {
fmt.Print(help)
ask("Enter your cookie:", &clientCookie)
fmt.Println("You can always clear cookie by running 'sptlrx clear'.")
ask("Enter your cookie:", &conf.Cookie)
config.Save(conf)
}

client, err := spotify.NewClient(clientCookie)
client, err := spotify.NewClient(conf.Cookie)
if err != nil {
return fmt.Errorf("couldn't create client: %w", err)
}
if err := cookie.Save(clientCookie); err != nil {
return fmt.Errorf("couldn't save cookie: %w", err)

if cmd.Flags().Changed("before") {
conf.Style.Before = parseStyleFlag(FlagStyleBefore)
}
if cmd.Flags().Changed("current") {
conf.Style.Current = parseStyleFlag(FlagStyleCurrent)
}
if cmd.Flags().Changed("after") {
conf.Style.After = parseStyleFlag(FlagStyleAfter)
}
if cmd.Flags().Changed("halign") {
conf.Style.HAlignment = FlagHAlignment
}

hAlignment := 0.5
switch FlagHAlignment {
case "left":
hAlignment = 0
case "right":
hAlignment = 1
if cmd.Flags().Changed("tinterval") {
conf.TimerInterval = FlagTimerInterval
}
if cmd.Flags().Changed("uinterval") {
conf.UpdateInterval = FlagUpdateInterval
}

p := tea.NewProgram(
&ui.Model{
Client: client,
HAlignment: gloss.Position(hAlignment),
StyleBefore: parseStyle(FlagStyleBefore),
StyleCurrent: parseStyle(FlagStyleCurrent),
StyleAfter: parseStyle(FlagStyleAfter),
Client: client,
Config: conf,
},
tea.WithAltScreen(),
)
Expand Down Expand Up @@ -126,63 +135,45 @@ func ask(what string, answer *string) {
}
}

func parseStyle(value string) gloss.Style {
var style gloss.Style

if value == "" {
return style
}
func parseStyleFlag(value string) config.StyleConfig {
var style config.StyleConfig

for _, part := range strings.Split(value, ",") {
switch part {
case "bold":
style = style.Bold(true)
style.Bold = true
case "italic":
style = style.Italic(true)
style.Italic = true
case "underline":
style = style.Underline(true)
style.Undeline = true
case "strikethrough":
style = style.Strikethrough(true)
style.Strikethrough = true
case "blink":
style = style.Blink(true)
style.Blink = true
case "faint":
style = style.Faint(true)
style.Faint = true
default:
if validateColor(part) {
if style.GetForeground() == (gloss.NoColor{}) {
style = style.Foreground(gloss.Color(part))
} else {
style = style.Background(gloss.Color(part))
style.ColorWhitespace(false)
}
} else {
fmt.Println("Invalid style:", part)
if style.Foreground == "" {
style.Foreground = part
} else if style.Background == "" {
style.Background = part
}
}
}
return style
}

func validateColor(color string) bool {
if _, err := strconv.Atoi(color); err == nil {
return true
}
if strings.HasPrefix(color, "#") {
return true
}
return false
}

func init() {
rootCmd.Flags().StringVar(&FlagCookie, "cookie", "", "your cookie")
rootCmd.PersistentFlags().StringVar(&FlagCookie, "cookie", "", "your cookie")

rootCmd.Flags().StringVar(&FlagStyleBefore, "before", "bold", "style of the lines before the current ones")
rootCmd.Flags().StringVar(&FlagStyleCurrent, "current", "bold", "style of the current lines")
rootCmd.Flags().StringVar(&FlagStyleAfter, "after", "faint", "style of the lines after the current ones")

rootCmd.Flags().StringVar(&FlagHAlignment, "halign", "center", "initial horizontal alignment (left/center/right)")

rootCmd.AddCommand(clearCmd)
rootCmd.PersistentFlags().IntVar(&FlagTimerInterval, "tinterval", 200, "interval for the internal timer (ms)")
rootCmd.PersistentFlags().IntVar(&FlagUpdateInterval, "uinterval", 200, "interval for updating playback status (ms)")

rootCmd.AddCommand(pipeCmd)
}

Expand Down
Loading

0 comments on commit 1ae3ff6

Please sign in to comment.