Skip to content

Commit

Permalink
Fix encoding issues with Powershell
Browse files Browse the repository at this point in the history
  • Loading branch information
0x000015 committed Jan 30, 2024
1 parent abd431f commit 7b63077
Showing 1 changed file with 12 additions and 5 deletions.
17 changes: 12 additions & 5 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@ import (
"math"
"net/http"
"os"
"os/exec"
"runtime"
"strconv"
"strings"
"time"

"github.com/abdfnx/gosh"
"github.com/hypebeast/go-osc/osc"
)

Expand All @@ -27,6 +28,10 @@ func main() {
}
ReadConfig()

if runtime.GOOS != "windows" {
config.ShowSpotify = false
}

fmt.Printf("=== HR-VR-OSC ===\nOSC Port: %d\nSpotify Enabled: %t\nTrend Enabled: %t\nHR Source: %s\n=================\n", config.OSCPort, config.ShowSpotify, config.ShowTrend, config.HeartRateSource)

client = osc.NewClient("localhost", config.OSCPort)
Expand Down Expand Up @@ -130,13 +135,15 @@ func GetHeartRate() (string, float64) {
func GetSpotifyPlaying() string {
// Grab the data using Powershell. Calling WinAPI funcs directly is kinda ass.
// tldr; filter processes and just get ones called 'Spotify' and the windowtitle isn't null.
err, out, errOut := gosh.RunOutput("Get-Process | Where-Object { $_.ProcessName -eq 'Spotify' -and $_.MainWindowTitle -ne '' } | Select-Object MainWindowTitle | Format-Table -AutoSize")
// 65001 instructs powershell to use a specific encoding to fix issues where it would output replacement chars for some songs/artists
cmd := exec.Command("powershell.exe", "65001", ">", "$null", ";", "Get-Process | Where-Object { $_.ProcessName -eq 'Spotify' -and $_.MainWindowTitle -ne '' } | Select-Object MainWindowTitle | Format-Table -AutoSize")
out, err := cmd.CombinedOutput()
if err != nil {
fmt.Printf("GetSpotifyPlaying(): Error grabbing processes\nErr: %s\n=================\n", err)
fmt.Println(errOut)
fmt.Printf("GetSpotifyPlaying(): Error grabbing Error parsing output\nErr: %s\n=================\n", err)
return "Spotify"
}
// Split the output otherwise we'll get data from powershell with useless bits
data := strings.Split(out, "\n")
data := strings.Split(string(out), "\n")
// Spotify not running?
if len(data) <= 1 {
return "Spotify"
Expand Down

0 comments on commit 7b63077

Please sign in to comment.