zaphyra's git: airpodsctl

Control and monitor your AirPods

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 
package types

import (
	"bytes"
	"encoding/json"
	"fmt"
	"os"

	"git.zaphyra.eu/airpodsctl/helpers"
)

func (device Device) FormatWaybarString() string {
	type WaybarFormat struct {
		Class      string      `json:"class,omitempty"`
		Percentage int         `json:"percentage"`
		Text       StringSlice `json:"text,omitempty"`
		Tooltip    string      `json:"tooltip,omitempty"`
	}

	// fmt.Printf("%+v\n", device)
	output := WaybarFormat{}
	tooltip := StringSlice{}

	tooltip.Add(fmt.Sprintf("Device:\t\t%s", device.Name))
	tooltip.Add(fmt.Sprintf("Connected:\t%s", device.Connected))

	if device.ANCMode.Valid {
		tooltip.Add(fmt.Sprintf("ANC-Mode:\t%s", device.ANCMode.Value))
	}

	if device.BatteryTTLEstimate.Valid {
		tooltip.Add(fmt.Sprintf("\n󱧦 ~%s remaining", device.BatteryTTLEstimate))
	}

	if device.Battery.Headphone.Valid {
		var chargeSymbol string
		if device.Battery.Headphone.Value.Charging {
			chargeSymbol = " <span size=\"smaller\" rise=\"1pt\"></span>"
		}

		tooltip.Add(fmt.Sprintf("󰁹  Headphones: %d% %#s", device.Battery.Headphone.Value.Level, chargeSymbol))
		output.Percentage = device.Battery.Headphone.Value.Level
	}

	if device.Battery.LeftBud.Valid || device.Battery.RightBud.Valid {
		var batteryPercentage []float64
		var batteryStateBuds StringSlice

		if device.Battery.LeftBud.Valid {
			var chargeSymbol string
			if device.Battery.LeftBud.Value.Charging {
				chargeSymbol = " <span size=\"smaller\" rise=\"1pt\"></span>"
			}

			batteryStateBuds.Add(fmt.Sprintf("<small>L:</small> %d%%%#s", device.Battery.LeftBud.Value.Level, chargeSymbol))
			batteryPercentage = append(batteryPercentage, float64(device.Battery.LeftBud.Value.Level))
		}

		if device.Battery.RightBud.Valid {
			var chargeSymbol string
			if device.Battery.RightBud.Value.Charging {
				chargeSymbol = " <span size=\"smaller\" rise=\"1pt\"></span>"
			}

			batteryStateBuds.Add(fmt.Sprintf("<small>R:</small> %d%%%#s", device.Battery.RightBud.Value.Level, chargeSymbol))
			batteryPercentage = append(batteryPercentage, float64(device.Battery.RightBud.Value.Level))
		}

		tooltip.Add(fmt.Sprintf("󰁹  Buds: %#s", batteryStateBuds.ConcatString("  ")))
		output.Percentage = int(helpers.Mean(batteryPercentage))
	}

	if device.Battery.Case.Valid {
		var chargeSymbol = ""
		if device.Battery.Case.Value.Charging {
			chargeSymbol = "<span size=\"smaller\" rise=\"1pt\"></span>"
		}

		tooltip.Add(fmt.Sprintf("󰁹  Case: %d%%%#s", device.Battery.Case.Value.Level, chargeSymbol))
	}

	output.Tooltip = tooltip.ConcatString("\n")

	jsonOutputBuffer := &bytes.Buffer{}
	jsonEncoder := json.NewEncoder(jsonOutputBuffer)
	jsonEncoder.SetEscapeHTML(false)

	err := jsonEncoder.Encode(output)
	if err != nil {
		fmt.Fprintln(os.Stderr, "[FormatWaybarString] Unable to marshal JSON: %s", err)
		return ""
	}

	return string(jsonOutputBuffer.Bytes())
}