Repository navigation
Expand file tree
/
Copy pathcommands.go
More file actions
1302 lines (1262 loc) · 38.4 KB
/
Copy pathcommands.go
File metadata and controls
1302 lines (1262 loc) · 38.4 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package main
import (
"context"
"encoding/json"
"fmt"
"os"
"path/filepath"
"slices"
"strconv"
"strings"
"time"
cli "github.com/urfave/cli/v3"
"github.com/bjarneo/cliamp/applog"
"github.com/bjarneo/cliamp/cmd"
"github.com/bjarneo/cliamp/config"
"github.com/bjarneo/cliamp/external/bandcamp"
"github.com/bjarneo/cliamp/external/qobuz"
"github.com/bjarneo/cliamp/external/spotify"
"github.com/bjarneo/cliamp/external/tidal"
"github.com/bjarneo/cliamp/external/ytmusic"
"github.com/bjarneo/cliamp/ipc"
"github.com/bjarneo/cliamp/player"
"github.com/bjarneo/cliamp/playlist"
"github.com/bjarneo/cliamp/pluginmgr"
"github.com/bjarneo/cliamp/resolve"
"github.com/bjarneo/cliamp/theme"
"github.com/bjarneo/cliamp/ui"
"github.com/bjarneo/cliamp/upgrade"
)
func buildApp() *cli.Command {
rootFlags := []cli.Flag{
&cli.Float64Flag{Name: "vol", Usage: "startup volume in dB [volume_min, +6]"},
&cli.BoolWithInverseFlag{Name: "shuffle", Usage: "shuffle playback"},
&cli.StringFlag{Name: "repeat", Usage: "repeat mode: off, all, one"},
&cli.BoolWithInverseFlag{Name: "mono", Usage: "mono output"},
&cli.BoolWithInverseFlag{Name: "auto-play", Usage: "start playback immediately"},
&cli.BoolWithInverseFlag{Name: "simplified", Usage: "simplified playback view (no visualizer or playlist)"},
&cli.BoolWithInverseFlag{Name: "help-bar", Usage: "show the key-binding hint bar (? still opens the full keymap)", Value: true},
&cli.BoolWithInverseFlag{Name: "expanded", Usage: "start with the playlist expanded (the Ctrl+X state)"},
&cli.StringFlag{Name: "provider", Usage: providerFlagUsage()},
&cli.StringFlag{Name: "start-theme", Usage: "UI theme name"},
&cli.StringFlag{Name: "visualizer", Usage: "visualizer mode"},
&cli.BoolFlag{Name: "visualizer-60fps", Usage: "render visualizer at 60 FPS (higher CPU use)"},
&cli.StringFlag{Name: "eq-preset", Usage: "EQ preset name"},
&cli.IntFlag{Name: "sample-rate", Usage: "output sample rate in Hz (0=auto)", HideDefault: true},
&cli.IntFlag{Name: "buffer-ms", Usage: "speaker buffer in milliseconds (50-5000)", HideDefault: true},
&cli.IntFlag{Name: "resample-quality", Usage: "resample quality factor (1-4)", HideDefault: true},
&cli.IntFlag{Name: "bit-depth", Usage: "PCM bit depth: 16 or 32", HideDefault: true},
&cli.StringFlag{Name: "audio-device", Usage: "audio output device (use 'list' to show)"},
&cli.StringFlag{Name: "playlist", Usage: "load a local TOML playlist by name and start playing"},
&cli.StringFlag{Name: "log-level", Usage: "log level: debug, info, warn, error"},
&cli.BoolWithInverseFlag{Name: "expand-playlist", Usage: "expand YouTube and YouTube Music list= URLs to the full playlist", Value: true},
&cli.BoolWithInverseFlag{Name: "low-power", Usage: "low-power mode: reduce CPU by lowering UI cadence and disabling visualization"},
&cli.BoolFlag{Name: "daemon", Aliases: []string{"d"}, Usage: "run headless (no TUI), serving IPC for scripts/Waybar"},
}
return &cli.Command{
Name: "cliamp",
Usage: "retro terminal music player",
Version: buildVersion(),
EnableShellCompletion: true,
Flags: rootFlags,
Action: func(ctx context.Context, c *cli.Command) error {
if strings.EqualFold(c.String("audio-device"), "list") {
return listAudioDevices()
}
ov, err := overridesFromFlags(c)
if err != nil {
return err
}
return run(ov, c.Args().Slice(), c.Bool("daemon"), c.Bool("visualizer-60fps"))
},
Commands: []*cli.Command{
upgradeCommand(),
pluginsCommand(),
playlistCommand(),
historyCommand(),
radioCommand(),
setupCommand(),
spotifyCommand(),
qobuzCommand(),
tidalCommand(),
bandcampCommand(),
ytmusicCommand(),
ipcSimpleCommand("play", "resume playback"),
ipcSimpleCommand("pause", "pause playback"),
ipcSimpleCommand("toggle", "play/pause toggle"),
ipcSimpleCommand("next", "next track"),
ipcSimpleCommand("prev", "previous track"),
ipcSimpleCommand("stop", "stop playback"),
statusCommand(),
volumeCommand(),
seekCommand(),
loadCommand(),
queueCommand(),
themeCommand(),
visCommand(),
visStreamCommand(),
shuffleCommand(),
repeatCommand(),
monoCommand(),
speedCommand(),
eqCommand(),
deviceCommand(),
remoteCommand(),
openCommand(),
protocolCommand(),
},
}
}
func listAudioDevices() error {
devices, err := player.ListAudioDevices()
if err != nil {
return err
}
if len(devices) == 0 {
fmt.Println("No audio output devices found.")
} else {
for _, d := range devices {
marker := " "
if d.Active {
marker = "* "
}
fmt.Printf("%s%-50s %s\n", marker, d.Description, d.Name)
}
}
return nil
}
func overridesFromFlags(c *cli.Command) (config.Overrides, error) {
var ov config.Overrides
if c.IsSet("vol") {
v := c.Float64("vol")
ov.Volume = &v
}
if c.IsSet("shuffle") {
v := c.Bool("shuffle")
ov.Shuffle = &v
}
if c.IsSet("repeat") {
v := strings.ToLower(c.String("repeat"))
switch v {
case "off", "all", "one":
ov.Repeat = &v
default:
return ov, fmt.Errorf("--repeat must be off, all, or one (got %q)", v)
}
}
if c.IsSet("mono") {
v := c.Bool("mono")
ov.Mono = &v
}
if c.IsSet("auto-play") {
v := c.Bool("auto-play")
ov.Play = &v
}
if c.IsSet("simplified") {
v := c.Bool("simplified")
ov.Simplified = &v
}
if c.IsSet("help-bar") {
v := !c.Bool("help-bar")
ov.HideHelpBar = &v
}
if c.IsSet("expanded") {
v := c.Bool("expanded")
ov.Expanded = &v
}
if c.IsSet("provider") {
v, err := parseProviderKey(c.String("provider"))
if err != nil {
return ov, err
}
ov.Provider = &v
}
if c.IsSet("start-theme") {
v := c.String("start-theme")
ov.Theme = &v
}
if c.IsSet("visualizer") {
v := c.String("visualizer")
ov.Visualizer = &v
}
if c.IsSet("eq-preset") {
v := c.String("eq-preset")
ov.EQPreset = &v
}
if c.IsSet("sample-rate") {
v := int(c.Int("sample-rate"))
ov.SampleRate = &v
}
if c.IsSet("buffer-ms") {
v := int(c.Int("buffer-ms"))
ov.BufferMs = &v
}
if c.IsSet("resample-quality") {
v := int(c.Int("resample-quality"))
ov.ResampleQuality = &v
}
if c.IsSet("bit-depth") {
v := int(c.Int("bit-depth"))
ov.BitDepth = &v
}
if c.IsSet("audio-device") {
v := c.String("audio-device")
ov.AudioDevice = &v
}
if c.IsSet("playlist") {
v := c.String("playlist")
ov.Playlist = &v
}
if c.IsSet("log-level") {
v := c.String("log-level")
if _, err := applog.ParseLevel(v); err != nil {
return ov, fmt.Errorf("--log-level: %w", err)
}
ov.LogLevel = &v
}
if c.IsSet("low-power") {
v := c.Bool("low-power")
ov.LowPower = &v
}
if c.IsSet("expand-playlist") {
v := c.Bool("expand-playlist")
ov.ExpandPlaylist = &v
}
return ov, nil
}
func upgradeCommand() *cli.Command {
return &cli.Command{
Name: "upgrade",
Usage: "upgrade cliamp to the latest stable release",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "prerelease", Usage: "upgrade to the newest release, prereleases included"},
},
Action: func(ctx context.Context, c *cli.Command) error {
return upgrade.Run(version, c.Bool("prerelease"))
},
}
}
func pluginsCommand() *cli.Command {
return &cli.Command{
Name: "plugins",
Usage: "manage Lua plugins",
Commands: []*cli.Command{
{
Name: "list",
Usage: "list installed plugins",
Action: func(ctx context.Context, c *cli.Command) error {
return pluginmgr.List()
},
},
{
Name: "install",
Usage: "install a plugin",
ArgsUsage: "<source>",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "yes", Aliases: []string{"y"}, Usage: "approve plugin trust without prompting"},
},
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp plugins install <source>")
}
return pluginmgr.Install(c.Args().First(), c.Bool("yes"))
},
},
{
Name: "trust",
Usage: "approve the current contents of an installed plugin",
ArgsUsage: "<name>",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "yes", Aliases: []string{"y"}, Usage: "approve plugin trust without prompting"},
},
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp plugins trust <name>")
}
return pluginmgr.Trust(c.Args().First(), c.Bool("yes"))
},
},
{
Name: "remove",
Usage: "remove a plugin",
ArgsUsage: "<name>",
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp plugins remove <name>")
}
return pluginmgr.Remove(c.Args().First())
},
},
{
Name: "call",
Usage: "invoke a plugin command in the running cliamp",
ArgsUsage: "<plugin> <command> [args...]",
Action: func(ctx context.Context, c *cli.Command) error {
args := c.Args().Slice()
if len(args) < 2 {
return fmt.Errorf("usage: cliamp plugins call <plugin> <command> [args...]")
}
resp, err := ipcSendWithin("plugin.call", ipc.Request{
Name: args[0],
Sub: args[1],
Args: args[2:],
}, 6*time.Minute)
if err != nil {
return err
}
if resp.Output != "" {
fmt.Println(resp.Output)
}
return nil
},
},
{
Name: "commands",
Usage: "list plugin commands registered in the running cliamp",
Action: func(ctx context.Context, c *cli.Command) error {
resp, err := ipcSend("plugin.commands", ipc.Request{})
if err != nil {
return err
}
if len(resp.Items) == 0 {
fmt.Println("No plugin commands registered.")
return nil
}
for _, item := range resp.Items {
fmt.Println(item)
}
return nil
},
},
},
}
}
func protocolCommand() *cli.Command {
return &cli.Command{
Name: "protocol",
Usage: "register cliamp:// links with the desktop",
Description: "Makes cliamp the handler for cliamp:// links, so clicking one plays\n" +
"or queues its target. install.sh already registers the scheme; use\n" +
"these commands after a go install build, to point the scheme at a\n" +
"different binary, or to remove the registration.",
Commands: []*cli.Command{
{
Name: "register",
Usage: "make cliamp the handler for cliamp:// links",
Action: func(ctx context.Context, c *cli.Command) error {
return cmd.ProtocolRegister(os.Stdout)
},
},
{
Name: "unregister",
Usage: "remove the cliamp:// handler registration",
Action: func(ctx context.Context, c *cli.Command) error {
return cmd.ProtocolUnregister(os.Stdout)
},
},
{
Name: "status",
Usage: "report whether cliamp:// is registered",
Action: func(ctx context.Context, c *cli.Command) error {
return cmd.ProtocolStatus(os.Stdout)
},
},
},
}
}
// radioCommand is an easter egg: the "who's listening" globe from
// cliamp.stream, in the terminal. The website's stats card shows the command
// as a prompt; it is left out of the help listing so that stays the only hint.
func radioCommand() *cli.Command {
return &cli.Command{
Name: "radio",
Usage: "who is listening to the cliamp radio channels",
Hidden: true,
Description: "Shows live listener statistics for the cliamp radio channels from\n" +
"radio.cliamp.stream: listeners now on the live streams and the channel\n" +
"playlists, by country and by channel, plus the all-time totals of the\n" +
"live streams. --globe draws them on a spinning globe, like the one on\n" +
"cliamp.stream.",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "stats", Usage: "print live listener statistics"},
&cli.BoolFlag{Name: "globe", Usage: "show the statistics on an animated globe (implies --stats)"},
&cli.BoolFlag{Name: "json", Usage: "print the raw statistics document of the live streams (implies --stats)"},
},
Action: func(ctx context.Context, c *cli.Command) error {
switch {
case c.Bool("globe") && c.Bool("json"):
return fmt.Errorf("--globe and --json cannot be combined")
case c.Bool("globe"):
return cmd.RadioGlobe(ctx, c.Root().String("start-theme"))
case c.Bool("stats") || c.Bool("json"):
return cmd.RadioStats(ctx, os.Stdout, c.Bool("json"))
default:
return cli.ShowSubcommandHelp(c)
}
},
}
}
func setupCommand() *cli.Command {
return &cli.Command{
Name: "setup",
Usage: "interactive wizard to configure remote providers",
Description: "Walks through configuring Navidrome, Lyrion, Bandcamp, Plex,\n" +
"Jellyfin, Emby, Audiobookshelf, Spotify, Qobuz, Tidal, NetEase,\n" +
"Mixcloud, and YouTube Music. Validates connections and writes\n" +
"~/.config/cliamp/config.toml.",
Action: func(ctx context.Context, c *cli.Command) error {
return cmd.Setup()
},
}
}
func spotifyCommand() *cli.Command {
return providerCredsCommand("spotify", "Spotify", spotify.CredsPath, spotify.DeleteCreds)
}
func qobuzCommand() *cli.Command {
return providerCredsCommand("qobuz", "Qobuz", qobuz.CredsPath, qobuz.DeleteCreds)
}
func tidalCommand() *cli.Command {
cmd := providerCredsCommand("tidal", "Tidal", tidal.CredsPath, tidal.DeleteCreds)
cmd.Commands = append(cmd.Commands, &cli.Command{
Name: "probe",
Usage: "print sanitized playback diagnostics for each quality tier (no tokens or signed URLs)",
ArgsUsage: "[search query]",
Action: func(ctx context.Context, c *cli.Command) error {
query := strings.Join(c.Args().Slice(), " ")
if query == "" {
query = "Random Access Memories Get Lucky"
}
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("config: %w", err)
}
probeCtx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
return tidal.Probe(probeCtx, os.Stdout, query, cfg.Tidal.ClientID, cfg.Tidal.ClientSecret)
},
})
return cmd
}
func bandcampCommand() *cli.Command {
return &cli.Command{
Name: "bandcamp",
Usage: "manage Bandcamp integration",
Commands: []*cli.Command{
{
Name: "probe",
Usage: "print sanitized Subsonic API diagnostics (no tokens or signed URLs)",
Action: func(ctx context.Context, c *cli.Command) error {
cfg, err := config.Load()
if err != nil {
return fmt.Errorf("config: %w", err)
}
probeCtx, cancel := context.WithTimeout(ctx, 5*time.Minute)
defer cancel()
return bandcamp.Probe(probeCtx, os.Stdout, cfg.Bandcamp)
},
},
},
}
}
func ytmusicCommand() *cli.Command {
return providerCredsCommand("ytmusic", "YouTube Music", ytmusic.CredsPath, ytmusic.DeleteCreds)
}
// providerCredsCommand builds the `cliamp <provider> reset` subcommand shared
// by providers that cache OAuth credentials on disk.
func providerCredsCommand(key, display string, credsPath func() (string, error), deleteCreds func() (bool, error)) *cli.Command {
return &cli.Command{
Name: key,
Usage: "manage " + display + " integration",
Commands: []*cli.Command{
{
Name: "reset",
Usage: "clear stored " + display + " credentials and force re-authentication",
Action: func(ctx context.Context, c *cli.Command) error {
path, err := credsPath()
if err != nil {
return fmt.Errorf("locate credentials: %w", err)
}
removed, err := deleteCreds()
if err != nil {
return fmt.Errorf("remove credentials: %w", err)
}
if !removed {
fmt.Printf("No stored %s credentials to remove.\n", display)
return nil
}
fmt.Printf("Removed %s\n", path)
fmt.Printf("Restart cliamp and select %s to sign in again.\n", display)
return nil
},
},
},
}
}
func playlistCommand() *cli.Command {
return &cli.Command{
Name: "playlist",
Usage: "manage local playlists",
Commands: []*cli.Command{
{
Name: "list",
Usage: "list playlists with track counts",
Action: func(ctx context.Context, c *cli.Command) error {
return cmd.PlaylistList()
},
},
{
Name: "create",
Usage: "create a new playlist, optionally from files/directories or directory sources",
ArgsUsage: "\"Name\" [file|dir ...]",
Flags: []cli.Flag{
&cli.StringFlag{Name: "ssh", Usage: "SSH host for remote directory walking"},
&cli.StringSliceFlag{Name: "dir", Usage: "reference a directory as a [[dir]] source (repeatable)"},
},
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("playlist name is required")
}
name := c.Args().First()
paths := c.Args().Slice()[1:]
return cmd.PlaylistCreate(name, paths, c.String("ssh"), c.StringSlice("dir"))
},
},
{
Name: "rename",
Usage: "rename a playlist",
ArgsUsage: "\"Old\" \"New\"",
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() != 2 {
return fmt.Errorf("usage: cliamp playlist rename \"Old\" \"New\"")
}
args := c.Args().Slice()
return cmd.PlaylistRename(args[0], args[1])
},
},
{
Name: "add",
Usage: "append tracks or directory sources to an existing playlist",
ArgsUsage: "\"Name\" [file|dir ...]",
Flags: []cli.Flag{
&cli.StringSliceFlag{Name: "dir", Usage: "add a directory as a [[dir]] source (repeatable)"},
},
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp playlist add \"Name\" file1 [file2 ...] [--dir dir]")
}
name := c.Args().First()
paths := c.Args().Slice()[1:]
return cmd.PlaylistAdd(name, paths, c.StringSlice("dir"))
},
},
{
Name: "dirs",
Usage: "list directory sources referenced by a playlist",
ArgsUsage: "\"Name\"",
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp playlist dirs \"Name\"")
}
return cmd.PlaylistDirs(c.Args().First())
},
},
{
Name: "show",
Usage: "display tracks in a playlist",
ArgsUsage: "\"Name\"",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "json", Usage: "machine-readable JSON output"},
},
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp playlist show \"Name\" [--json]")
}
return cmd.PlaylistShow(c.Args().First(), c.Bool("json"))
},
},
{
Name: "remove",
Usage: "remove a track by index",
ArgsUsage: "\"Name\"",
Flags: []cli.Flag{
&cli.IntFlag{Name: "index", Usage: "track index (1-based)", Required: true},
},
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp playlist remove \"Name\" --index N")
}
return cmd.PlaylistRemove(c.Args().First(), int(c.Int("index")))
},
},
{
Name: "delete",
Usage: "delete an entire playlist",
ArgsUsage: "\"Name\"",
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp playlist delete \"Name\"")
}
return cmd.PlaylistDelete(c.Args().First())
},
},
{
Name: "dedupe",
Usage: "remove duplicate tracks by exact path",
ArgsUsage: "\"Name\"",
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp playlist dedupe \"Name\"")
}
return cmd.PlaylistDedupe(c.Args().First())
},
},
{
Name: "sort",
Usage: "sort a playlist in place",
ArgsUsage: "\"Name\"",
Flags: []cli.Flag{
&cli.StringFlag{Name: "by", Usage: "sort key: track, title, artist, album, artist+album, path", Value: "title"},
},
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp playlist sort \"Name\" --by album")
}
return cmd.PlaylistSort(c.Args().First(), c.String("by"))
},
},
{
Name: "doctor",
Usage: "report missing local files in playlists",
ArgsUsage: "[Name]",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "fix", Usage: "prune missing local files"},
},
Action: func(ctx context.Context, c *cli.Command) error {
name := ""
if c.Args().Len() > 0 {
name = c.Args().First()
}
return cmd.PlaylistDoctor(name, c.Bool("fix"))
},
},
{
Name: "export",
Usage: "export a playlist as M3U or PLS",
ArgsUsage: "\"Name\"",
Flags: []cli.Flag{
&cli.StringFlag{Name: "format", Usage: "format: m3u or pls", Value: "m3u"},
&cli.StringFlag{Name: "output", Aliases: []string{"o"}, Usage: "output file (default stdout)"},
},
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp playlist export \"Name\" [--format m3u|pls] [-o file]")
}
return cmd.PlaylistExport(c.Args().First(), c.String("format"), c.String("output"))
},
},
{
Name: "import",
Usage: "import a local M3U or PLS file",
ArgsUsage: "file.m3u",
Flags: []cli.Flag{
&cli.StringFlag{Name: "name", Usage: "playlist name (default: file basename)"},
},
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp playlist import file.m3u [--name Name]")
}
return cmd.PlaylistImport(c.Args().First(), c.String("name"))
},
},
{
Name: "favorite",
Aliases: []string{"bookmark"},
Usage: "toggle the favorite of a track by index",
ArgsUsage: "\"Name\"",
Flags: []cli.Flag{
&cli.IntFlag{Name: "index", Usage: "track index (1-based)", Required: true},
},
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp playlist favorite \"Name\" --index N")
}
return cmd.PlaylistFavorite(c.Args().First(), int(c.Int("index")))
},
},
{
Name: "favorites",
Aliases: []string{"bookmarks"},
Usage: "list all favorite tracks",
Action: func(ctx context.Context, c *cli.Command) error {
return cmd.PlaylistFavorites()
},
},
{
Name: "enrich",
Usage: "probe duration and album metadata for SSH tracks",
ArgsUsage: "\"Name\"",
Flags: []cli.Flag{
&cli.StringFlag{Name: "source", Usage: "source: metadata, path", Value: "path"},
},
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp playlist enrich \"Name\" --source metadata")
}
return cmd.PlaylistEnrich(c.Args().First(), c.String("source"))
},
},
},
}
}
func historyCommand() *cli.Command {
return &cli.Command{
Name: "history",
Usage: "show recently played tracks",
Description: "Lists recently played tracks. A track is recorded when it starts to play.\n" +
"Browse the same data inside the TUI under Local Playlists →\n" +
"\"Recently Played\".",
Flags: []cli.Flag{
&cli.IntFlag{Name: "limit", Usage: "max entries to show (0 = all)", Value: 50},
&cli.BoolFlag{Name: "json", Usage: "machine-readable JSON output"},
},
Action: func(ctx context.Context, c *cli.Command) error {
return cmd.HistoryShow(int(c.Int("limit")), c.Bool("json"))
},
Commands: []*cli.Command{
{
Name: "clear",
Usage: "delete the history file",
Action: func(ctx context.Context, c *cli.Command) error {
return cmd.HistoryClear()
},
},
},
}
}
// ipcSimpleCommand creates a fire-and-forget IPC command (play, pause, etc.).
func ipcSimpleCommand(name, usage string) *cli.Command {
return &cli.Command{
Name: name,
Usage: usage,
Action: func(ctx context.Context, c *cli.Command) error {
_, err := ipcSend(name, ipc.Request{})
return err
},
}
}
func statusCommand() *cli.Command {
return &cli.Command{
Name: "status",
Usage: "show current playback state",
Flags: []cli.Flag{
&cli.BoolFlag{Name: "json", Usage: "machine-readable JSON output"},
},
Action: func(ctx context.Context, c *cli.Command) error {
snapshot, err := ipcState()
if err != nil {
return err
}
resp := stateResult(snapshot)
if c.Bool("json") {
enc := json.NewEncoder(os.Stdout)
enc.SetIndent("", " ")
return enc.Encode(newStatusJSON(resp))
}
state := resp.State
if state == "" {
state = "stopped"
}
fmt.Printf("State: %s\n", state)
if resp.Track != nil {
fmt.Printf("Track: %s\n", resp.Track.Title)
if resp.Track.Artist != "" {
fmt.Printf("Artist: %s\n", resp.Track.Artist)
}
}
if resp.Duration > 0 {
fmt.Printf("Position: %.0f / %.0f sec\n", resp.Position, resp.Duration)
}
fmt.Printf("Volume: %.0f dB\n", resp.Volume)
if resp.Shuffle != nil {
if *resp.Shuffle {
fmt.Println("Shuffle: on")
} else {
fmt.Println("Shuffle: off")
}
}
if resp.Repeat != "" {
fmt.Printf("Repeat: %s\n", resp.Repeat)
}
if resp.Mono != nil {
if *resp.Mono {
fmt.Println("Mono: on")
} else {
fmt.Println("Mono: off")
}
}
if resp.Speed > 0 {
fmt.Printf("Speed: %.2fx\n", resp.Speed)
}
if resp.EQPreset != "" {
fmt.Printf("EQ: %s\n", resp.EQPreset)
}
return nil
},
}
}
func volumeCommand() *cli.Command {
return &cli.Command{
Name: "volume",
Usage: "set the volume to an absolute level in dB",
ArgsUsage: "<dB>",
Description: "Sets the volume to <dB>. A sign does not make the value relative:\n" +
"cliamp volume -3 sets the volume to -3 dB. cliamp clamps the value to\n" +
"the range volume_min to +6 dB. To step the volume, run\n" +
"cliamp remote call volume.adjust --params '{\"value\":3}'.",
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp volume <dB>")
}
db, err := strconv.ParseFloat(c.Args().First(), 64)
if err != nil {
return fmt.Errorf("invalid volume value %q", c.Args().First())
}
_, err = ipcSend("volume", ipc.Request{Value: db})
return err
},
}
}
func seekCommand() *cli.Command {
return &cli.Command{
Name: "seek",
Usage: "seek by a relative offset in seconds",
ArgsUsage: "<seconds>",
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp seek <seconds>")
}
secs, err := strconv.ParseFloat(c.Args().First(), 64)
if err != nil {
return fmt.Errorf("invalid seek value %q", c.Args().First())
}
_, err = ipcSend("seek", ipc.Request{Value: secs})
return err
},
}
}
func loadCommand() *cli.Command {
return &cli.Command{
Name: "load",
Usage: "load a playlist into the player",
ArgsUsage: "\"Playlist Name\"",
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp load \"Playlist Name\"")
}
_, err := ipcSendWithin("load", ipc.Request{Playlist: c.Args().First()}, ipcLoadWait)
return err
},
}
}
func queueCommand() *cli.Command {
return &cli.Command{
Name: "queue",
Usage: "queue a track for playback",
ArgsUsage: "</path/to/file.mp3>",
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp queue /path/to/file.mp3")
}
path, err := queuePath(c.Args().First())
if err != nil {
return err
}
_, err = ipcSend("queue", ipc.Request{Path: path})
return err
},
}
}
// queuePath returns the path that cliamp queue sends for arg. A local file
// gets an absolute path, because the running cliamp has its own working
// directory. A URL or another URI, such as spotify:track:..., goes as it is.
// A missing file or a directory is an error.
func queuePath(arg string) (string, error) {
info, err := os.Stat(arg)
switch {
case err == nil && info.IsDir():
return "", fmt.Errorf("queue: %s is a directory", arg)
case err == nil:
return filepath.Abs(arg)
case playlist.IsURL(arg) || resolve.URIScheme(arg) != "":
return arg, nil
}
return "", fmt.Errorf("queue: %w", err)
}
func themeCommand() *cli.Command {
return &cli.Command{
Name: "theme",
Usage: "set or list UI themes",
ArgsUsage: "<name|list>",
Action: func(ctx context.Context, c *cli.Command) error {
if c.Args().Len() == 0 {
return fmt.Errorf("usage: cliamp theme <name|list>")
}
if strings.EqualFold(c.Args().First(), "list") {
// The list is read here, so it works when cliamp is not
// running. It starts with the terminal colors, as the IPC
// theme list does. No log file is open yet, so the skipped
// theme files go to stderr.
themes, errs := theme.LoadAllWithErrors()
for _, err := range errs {
fmt.Fprintln(os.Stderr, err)
}
fmt.Printf(" %s\n", theme.DefaultName)
for _, t := range themes {
fmt.Printf(" %s\n", t.Name)
}
return nil
}
_, err := ipcSend("theme", ipc.Request{Name: c.Args().First()})
if err != nil {
return err
}
fmt.Printf("Theme: %s\n", c.Args().First())
return nil
},
}
}
func visStreamCommand() *cli.Command {
return &cli.Command{
Name: "visstream",
Usage: "stream visualizer bands as NDJSON (one frame per line)",
Flags: []cli.Flag{
&cli.IntFlag{Name: "fps", Value: 30, Usage: "frames per second (1-60)"},
},
Action: func(ctx context.Context, c *cli.Command) error {
fps := c.Int("fps")
if fps < 1 {
fps = 1
}
if fps > 60 {
fps = 60
}
return userIPCError(ipc.StreamBands(ctx, ipc.DefaultSocketPath(), time.Second/time.Duration(fps), os.Stdout))
},
}
}
// visModes returns the modes that cliamp vis list prints and the row of the
// active mode, or -1. A running cliamp lists its Lua visualizers too and
// gives the active row, because a Lua mode can have the name of a built-in
// mode. A cliamp that gives no row marks the first row with the active
// name. With no running cliamp, or in headless mode, it lists the built-in
// modes.
func visModes() (names []string, active int, running bool) {
names = ui.VisModeNames()
snapshot, err := ipcState()
if err != nil {
return names, -1, false
}
if resp, err := ipcSend("vis", ipc.Request{Name: "list"}); err == nil && len(resp.Items) > 0 {
names = resp.Items
if resp.Visualizer != "" && resp.Index >= 0 && resp.Index < len(names) {
return names, resp.Index, true
}
}
active = slices.IndexFunc(names, func(name string) bool { return strings.EqualFold(name, snapshot.Visualizer) })
return names, active, true
}
func visCommand() *cli.Command {