summarylogtreecommitdiffstats
path: root/sqlchknob
blob: 668a84d06e93b23a8927932aa2711f65012ca0d6 (plain)
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
#!/usr/bin/env bash

# 🎛 sqlchknob — Interactive tuner

set -euo pipefail
IFS=$'\n\t'

CONFIG_DIR="$HOME/.config/sqlch"
STATION_FILE="$CONFIG_DIR/stations"
<<<<<<< HEAD
CHANNEL_FILE="$CONFIG_DIR/channels"
=======
>>>>>>> 7121b57 (Clean flat AUR-compliant repo)
LAST_PLAYED="$CONFIG_DIR/last"
DEFAULT_STATION="$CONFIG_DIR/default"
SOCKET="/tmp/mpvsocket"

mkdir -p "$CONFIG_DIR"
touch "$STATION_FILE" "$LAST_PLAYED"

ensure_gum() {
  if ! command -v gum >/dev/null; then
    echo "❌ 'gum' required but not found in PATH."
    exit 1
  fi
}

splash() {
  gum style --foreground 2 --border double --padding "1 2" --margin "1 1" <<EOF
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
▓▓   sqlchKNOB v3   ▓▓
▓▓────────────────────▓▓
▓▓   • Tune In        ▓▓
▓▓   • Drop Out       ▓▓
▓▓   • Mono Loud      ▓▓
▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓▓
EOF
}

choose_play() {
  local choices name
  choices=$(cut -d= -f1 "$STATION_FILE")
  [[ -z "$choices" ]] && echo "⚠️ No stations available." && return
  name=$(echo "$choices" | gum choose)
  [[ -n "$name" ]] && sqlchctl play "$name"
}

search_add() {
  local query
  query=$(gum input --placeholder "Search RadioBrowser")
  [[ -z "$query" ]] && return

  local fetched
  fetched=$(curl -s "https://de1.api.radio-browser.info/json/stations/search?name=$query" | jq -r '.[] | "\(.name)=\(.url_resolved)"')
  if [[ -z "$fetched" ]]; then
    gum style --foreground 1 --border double "❌ No results found for '$query'"
    return
  fi

  local results
  results=$(echo "$fetched" | gum choose --no-limit)
  [[ -z "$results" ]] && return

  # Ensure the stations file exists
  mkdir -p "$(dirname "$STATION_FILE")"
  touch "$STATION_FILE"

  # Normalize and save to stations file
  echo "$results" | tr -d '\r' | while IFS= read -r line; do
    [[ -n "$line" ]] && grep -qxF "$line" "$STATION_FILE" || echo "$line" >> "$STATION_FILE"
  done

  gum style --foreground 3 "✅ Saved new station(s) to $STATION_FILE"
  echo "DEBUG: Writing to $STATION_FILE" >&2
  ls -l "$STATION_FILE" >&2
}

browse_genre() {
  local genre
  genre=$(curl -s https://de1.api.radio-browser.info/json/genres | jq -r '.[].name' | gum choose --placeholder "Pick a genre")
  [[ -z "$genre" ]] && return

  local fetched
  fetched=$(curl -s "https://de1.api.radio-browser.info/json/stations/bytag/$genre" | jq -r '.[] | "\(.name)=\(.url_resolved)"')
  if [[ -z "$fetched" ]]; then
    gum style --foreground 1 --border double "❌ No stations found for '$genre'"
    return
  fi

  local results
  results=$(echo "$fetched" | tr '\r' '
' | gum choose --no-limit)

  if [[ -z "$results" ]]; then
    return
  fi

  while IFS= read -r line; do
    [[ -n "$line" ]] && grep -qxF "$line" "$STATION_FILE" || echo "$line" >> "$STATION_FILE"
  done <<< "$results"
}

edit_station() {
  local tmpfile backup
  tmpfile=$(mktemp)
  backup="${STATION_FILE}.bak.$(date +%s)"
  cp "$STATION_FILE" "$backup"
  cp "$STATION_FILE" "$tmpfile"
  gum write --placeholder "Edit your stations..." < "$tmpfile" > "$tmpfile" && mv "$tmpfile" "$STATION_FILE"
}

delete_station() {
  local name
  name=$(cut -d= -f1 "$STATION_FILE" | gum choose --placeholder "Select station to delete")
  [[ -n "$name" ]] && sed -i "/^\*\?$name=/d" "$STATION_FILE"
}

<<<<<<< HEAD
select_channel() {
  local selected
  selected=$(cut -d= -f1 "$CHANNEL_FILE" | gum choose --height=20)
  [[ -n "$selected" ]] && sqlchctl play-channel "$selected"
}