--!nonstrict -- Sourcehut Builds - Bar widget that toggles the build status panel and shows the status of recent tasks. -- -- You can configure the glyphs shown for the different statuses, as well as -- when there are no recent builds. local K = require("./constants.luau") -------------------------------------------------------------------------------- -- Constants -------------------------------------------------------------------------------- local GLYPHS = { default = "glyph", ok = "ok_glyph", failed = "failed_glyph", inProgress = "in_progress_glyph", } local STYLE = { color = { closed = "on_surface", open = "primary" } } -------------------------------------------------------------------------------- -- State -------------------------------------------------------------------------------- local isPanelOpen: boolean = false local buildState: SourcehutBuildsState = require("./empty_state.luau") -------------------------------------------------------------------------------- -- Internal Functions -------------------------------------------------------------------------------- ------------------------------------------------------------------ Logic Helpers local function hasRecentBuilds(): boolean return buildState.successful > 0 or buildState.failed > 0 or buildState.inProgress > 0 end ----------------------------------------------------------------------------- UI -- Renders the bar local function render() barWidget.setGlyphColor(isPanelOpen and STYLE.color.open or STYLE.color.closed) barWidget.setTooltip(noctalia.tr("bar.tooltip")) local container = barWidget.isVertical() and ui.column or ui.row barWidget.render(container({ gap = 6, align = "center" }, { not hasRecentBuilds() and ui.glyph({ name = noctalia.getConfig(GLYPHS.default), size = 14, color = isPanelOpen and STYLE.color.open or STYLE.color.closed }) or nil, buildState.successful > 0 and ui.glyph({ name = noctalia.getConfig(GLYPHS.ok), size = 14, color = isPanelOpen and STYLE.color.open or STYLE.color.closed }) or nil, buildState.failed > 0 and ui.glyph({ name = noctalia.getConfig(GLYPHS.failed), size = 14, color = isPanelOpen and STYLE.color.open or STYLE.color.closed }) or nil, buildState.inProgress > 0 and ui.glyph({ name = noctalia.getConfig(GLYPHS.inProgress), size = 14, color = isPanelOpen and STYLE.color.open or STYLE.color.closed }) or nil, })) end ---------------------------------------------------------------- Event Listeners local function onPanelStateChanged(value: boolean) isPanelOpen = value render() end local function onBuildsStateChanged(value: SourcehutBuildsState) buildState = value render() end ---------------------------------------------------------------------- Lifecycle local function run() noctalia.setUpdateInterval(K.bar.update_interval) end -------------------------------------------------------------------------------- -- Listeners -------------------------------------------------------------------------------- noctalia.state.watch(K.events.panel_state_updated, onPanelStateChanged) noctalia.state.watch(K.events.builds_state_updated, onBuildsStateChanged) -------------------------------------------------------------------------------- -- Public API -------------------------------------------------------------------------------- function update() render() end function onClick() noctalia.togglePanel(K.panel.id) end function onConfigChanged() end run()