File: pipe.go

package info (click to toggle)
sptlrx 1.2.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 424 kB
  • sloc: makefile: 6
file content (77 lines) | stat: -rw-r--r-- 1,757 bytes parent folder | download
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
package cmd

import (
	"fmt"
	"strings"

	"github.com/raitonoberu/sptlrx/config"
	"github.com/raitonoberu/sptlrx/lyrics"
	"github.com/raitonoberu/sptlrx/pool"

	"github.com/muesli/reflow/wordwrap"
	"github.com/muesli/reflow/wrap"
	"github.com/spf13/cobra"
)

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

	RunE: func(cmd *cobra.Command, args []string) error {
		conf, err := loadConfig(cmd)
		if err != nil {
			return fmt.Errorf("couldn't load config: %w", err)
		}
		player, err := loadPlayer(conf)
		if err != nil {
			return fmt.Errorf("couldn't load player: %w", err)
		}
		provider, err := loadProvider(conf, player)
		if err != nil {
			return fmt.Errorf("couldn't load provider: %w", err)
		}

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

		for update := range ch {
			printUpdate(update, conf)
		}
		return nil
	},
}

func printUpdate(update pool.Update, conf *config.Config) {
	if update.Err != nil {
		if !conf.IgnoreErrors {
			fmt.Println(update.Err.Error())
		}
		return
	}
	if update.Lines == nil || !lyrics.Timesynced(update.Lines) {
		fmt.Println("")
		return
	}
	line := update.Lines[update.Index].Words
	if conf.Pipe.Length == 0 {
		fmt.Println(line)
		return
	}
	switch conf.Pipe.Overflow {
	case "none":
		s := wrap.String(line, conf.Pipe.Length)
		fmt.Println(strings.Split(s, "\n")[0])
	case "word":
		s := wordwrap.String(line, conf.Pipe.Length)
		fmt.Println(strings.Split(s, "\n")[0])
	case "ellipsis":
		s := wrap.String(line, conf.Pipe.Length)
		lines := strings.Split(s, "\n")
		if len(lines) == 1 {
			fmt.Println(lines[0])
			return
		}
		s = wrap.String(lines[0], conf.Pipe.Length-3)
		fmt.Println(strings.Split(s, "\n")[0] + "...")
	}
}