-- Query EnvyControl once when this widget starts; GPU mode changes require a restart.
local command = "envycontrol --query"
local labels = {
  integrated = "iGPU",
  dedicated = "dGPU",
  nvidia = "dGPU",
  hybrid = "hybrid",
}

barWidget.setGlyph("device-desktop")
barWidget.setText("GPU…")
barWidget.setTooltip("Reading EnvyControl GPU mode…")

noctalia.runAsync(command, function(result)
  if result.exitCode ~= 0 then
    barWidget.setText("GPU?")
    barWidget.setTooltip("Unable to read EnvyControl GPU mode")
    return
  end

  local mode = string.lower(string.match(result.stdout, "([%a_-]+)") or "")
  local label = labels[mode]
  if label == nil then
    barWidget.setText("GPU?")
    barWidget.setTooltip("Unknown EnvyControl GPU mode: " .. mode)
    return
  end

  barWidget.setText(label)
  barWidget.setTooltip("EnvyControl GPU mode: " .. mode)
end)
