volume

package
v0.15.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 4, 2026 License: AGPL-3.0 Imports: 7 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var Command = &cobra.Command{
	Use: "volume",
	Example: `  waybar-lyric volume 20% # Set player volume to 20%
  waybar-lyric volume 0.5 # Set player volume to 50%
  waybar-lyric volume +10% # Increase player volume by 10%
  waybar-lyric volume -5% # Decrease player volume by 5%`,
	Short: "Set player volume",
	Args:  cobra.ExactArgs(1),

	DisableFlagsInUseLine: true,
	RunE: func(_ *cobra.Command, args []string) error {
		volStr := args[0]
		var volume float64
		relative := false
		operation := 1.0

		if strings.HasPrefix(volStr, "+") || strings.HasPrefix(volStr, "-") {
			relative = true
			if strings.HasPrefix(volStr, "-") {
				operation = -1.0
			}
			volStr = volStr[1:]
		}

		if strings.HasSuffix(volStr, "%") {
			vol, err := cast.ToFloat64E(strings.TrimSuffix(volStr, "%"))
			if err != nil {
				slog.Error("Failed to convert volume to float", "error", err)
				return fmt.Errorf("failed convert volume to float: %v", err)
			}
			volume = vol / 100
		} else {
			vol, err := cast.ToFloat64E(volStr)
			if err != nil {
				slog.Error("Failed to convert volume to float", "error", err)
				return fmt.Errorf("failed convert volume to float: %v", err)
			}
			volume = vol
		}

		if !relative && (volume < 0 || volume > 1) {
			slog.Error(
				"Volume is out of range. Volume value must be between 0-1 or 0-100%",
				"got",
				volStr,
			)
			return fmt.Errorf("volume is out of range. volume=%.2f", volume)
		}

		conn, err := dbus.SessionBus()
		if err != nil {
			return fmt.Errorf("failed to create dbus connection: %w", err)
		}
		slog.Debug("Created dbus session bus")

		mp, err := player.Select(conn)
		if err != nil {
			return fmt.Errorf("failed to select player: %w", err)
		}

		slog.Debug("Selected player", "player", mp.GetName())

		if relative {
			currentVol, err := mp.GetVolume()
			if err != nil {
				slog.Error("Failed to get current volume", "error", err)
				return fmt.Errorf("failed to get current volume: %w", err)
			}
			volume = currentVol + (operation * volume)

			if volume < 0 {
				volume = 0
			} else if volume > 1 {
				volume = 1
			}
		}

		slog.Info("Setting player volume", "volume", volume)
		if err := mp.SetVolume(volume); err != nil {
			slog.Error("Failed to set volume", "error", err)
			return err
		}

		return nil
	},
}

Command is the volume changer command

Functions

This section is empty.

Types

This section is empty.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL