* Give built-in plugins an honest on/off state Every built-in reported itself enabled no matter what. A bar widget said "enabled" while sitting nowhere near the bar, and disabling a built-in service silently did nothing, because enabled meant "listed in plugins[]" and a built-in never is. Nothing surfaced that, since the only caller listing plugins was the CLI. For a widget, on and off is its place in the bar, so listPlugins reports layout membership -- what enable/disable actually toggles. For everything else built in, loading by default is the right behaviour to keep, so switching one off is recorded the other way round, in disabledPlugins[]. shell.json still carries only the deviation from the defaults: the key is dropped the moment nothing is switched off, leaving a config that never disabled anything byte-identical. isEnabled still answers a separate question -- whether the component loads at all -- and deliberately does not follow a widget out of the bar. omarchy.menu is both a widget and the menu itself, so tying the two together would let taking its button off the bar lock the menu out of the shell, with no way back that isn't the CLI. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Manage plugins from Setup > Plugins Plugins were CLI-only. Setup > Plugins now offers Enable, Disable, Add, and Remove, each list living in the menu itself so picking a row acts on it. Enable and Disable cover the built-ins as well as anything installed -- the bar widgets you can put in the bar, the services and overlays you can switch off. Remove is limited to plugins the user installed, since a built-in has no checkout to delete, and stays hidden until there is one. Whole-bar replacements are left out; those are chosen under Style. Enabling a bar widget asks for a section first, because enabling alone drops it on the right and the only way to move it was a follow-up bar plugin move. The CLI asks the same question after its own add, so both paths place a widget the same way. Add and Remove run in a terminal: one needs a git URL and shows the trust warning before cloning, the other deletes a checkout and prints where it backed it up. Providers grew two hooks for this. placementFor turns a row into a submenu instead of an action, and volatile re-runs the enumeration when its submenu is entered -- picking from these lists is what changes them, and rows a provider no longer returns now drop out instead of lingering forever. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Trim the plugin menu after review Menu.qml carried its own shellQuote while already importing Util and calling Util.shellQuote a few lines up; two copies of the same escaping is one place for a future fix to miss. isDisabled walked the array by hand to compare values it writes itself, and dropDisabled was an eight-line helper with one caller. Two bugs came out of the same pass. A whole-bar replacement belongs under Style rather than these lists, but the exclusion sat in the shared row builder, so a third-party bar could be installed and never removed -- Remove would show an empty list under a guard that said something was there. The exclusion now sits on the two lists that mean it. Rows are keyed by id, and distinct plugin ids can slugify alike: acme.foo, acme_foo and acme-foo all give acme-foo. The merge keeps the first row per id, so the rest simply vanished from the list with nothing to say why. Row ids are now made distinct before merging. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Pick a plugin the way we pick a theme Setup > Plugins listed plugins as menu rows, which needed three providers, a placement submenu, a volatile-refresh hook and a row-swap in the merge. Only Font and Apps are built that way. Theme, Background, Unlock, Timezone and Keybindings all pipe a list into omarchy-menu-select instead, which is one action string and a small script -- so that is what these use now. The trade is search: a plugin name is no longer findable from the root prompt. Neither is a theme name or a timezone, and Enable Plugin still is, so the loss sits where the rest of the menu already puts it. Two pieces of the row machinery stay, because they are worth having for the lists that remain. A volatile provider re-runs when its submenu is entered, so a font installed since the shell started now shows up without restarting it, and rows a provider stops returning drop out. Row ids are still made distinct before merging: Fira Code and Fira-Code both slug to fira-code, and a repeated id was silently dropped. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Let a picked option carry an icon Moving the plugin lists onto omarchy-menu-select cost them their glyphs: the select mode has always hardcoded an empty icon, which is why Timezone and Keybindings have none either. An option may now lead with one, as "<glyph><TAB><label>". The menu shows the glyph, filters on the label, and hands the label back, so a caller never strips a glyph off its own selection and a list of plain strings behaves exactly as before. The plugin picker uses it for the puzzle glyph on each plugin and the align glyphs on the sections, which also regain the capitals they lost when the section names were passed through raw. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Switch bars by enabling one A bar option was kept out of Enable and Disable on the grounds that picking which bar to run belongs under Style -- but nothing under Style ever offered it, so an installed bar could be added and removed and never actually put to use. The menu was guarding a door to a room that was never built. Enabling one is the switch. setEnabled already assigns bar.id for a bar option, so a bar has always replaced the one before it; only the picker's filter stood in the way. Dropping it costs nothing else, because enabled for a bar option means active: the bar in use is the one row absent from Enable, every other installed bar is one pick away, and the built-in is just another entry, so going back to it is enabling Bar. Disable keeps the exclusion. That is the one verb a bar cannot answer -- there is no off, only a successor -- and offering it would have listed the built-in bar on a stock system, where turning it off deletes a bar.id that was never set and nothing happens. A bar carries the bar glyph rather than the puzzle one, so a row that replaces the whole bar does not read like one more widget to switch on, and enable now says "Now using X as the bar" instead of "Enabled X", which understated a whole-bar swap in both the enable and the freshly-added path. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Refuse a plugin that declares a kind it cannot load A kind is a promise to supply something to load, and the shell reads that something from a fixed key: entryPoints.bar to draw a bar, entryPoints.menu to open a menu. Nothing checked the promise. A manifest could claim kinds ["bar"] with no bar entry point, pass validation, install, and enable -- and then the bar would fall back to the built-in and the widget would be skipped, leaving a plugin that does nothing, explained only by a console.warn nobody reads. Our own plugins have been held to this table by plugins-test.sh all along. This holds third-party ones to the same table, at add and update time, where there is still someone to tell. A kind outside the table is left alone rather than guessed at, so a shell that learns a new kind does not need this list updated first. The cost is that a misspelled kind still installs quietly. omarchy-plugin-validate had no tests; it has some now. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Act on the plugin whose row was picked The picker showed a name and then looked that name up again across every plugin, filtered set or not, taking the first match. Two plugins can share a name: cloning one keeps the name it was cloned from, so the documented `omarchy plugin clone omarchy.clock local.clock` leaves two plugins called Clock. Enable listed the clone -- the built-in was already enabled, so only the clone was eligible -- and then enabled omarchy.clock, moving the built-in widget instead. Remove listed the clone and tried to delete a built-in that has no checkout to delete. A row now carries its id alongside its label, and the id is read back off the row that was picked instead of being derived from the name a second time. Where a name is not unique among the rows on offer, the label carries the id too, so two rows that would both say Clock can be told apart at all -- which they could not before, whichever one the pick resolved to. The verb prompt only ever sees the first two fields, so the menu shows what it always did. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Never ask a bar where to sit in the bar A manifest may declare both bar and bar-widget, and validation accepts it. The picker saw bar-widget, asked for a section, and passed it to enable. setEnabled takes bar as the dominant kind: it writes bar.id and returns, adding nothing to any layout, so the move that followed had no widget to find and failed -- after the bar had already been switched. A partial success with an error on the way out. Bar wins ahead of bar-widget now, in the picker and in the placement prompt `plugin add --enable` asks, so a bar is enabled without a placement it cannot use. The CLI refuses a placement on a bar outright, before the bar is switched rather than after, since `omarchy plugin enable <bar> --section left` could reach the same half-applied state without going through either. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com> * Only replacement, no off * Add default placement for bar widgets * Simplify plugin menu actions * Document plugin placement behavior * Allow dropping widgets in empty bar space * Treat plugin dependencies as runtime invariants * Reject duplicate plugin ids on add --------- Co-authored-by: Claude Opus 5 (1M context) <noreply@anthropic.com>
503 lines
19 KiB
Bash
Executable File
503 lines
19 KiB
Bash
Executable File
#!/bin/bash
|
|
|
|
# omarchy:summary=Add, move, remove, and configure bar plugin widgets in the layout
|
|
# omarchy:group=bar
|
|
# omarchy:args=add <id> [placement] | move <id> [placement] | remove <id> [placement] | set <id> <key> <value> [--json] [placement] | replace <old-id> <new-id>
|
|
# omarchy:examples=omarchy bar plugin add omarchy.tailscale | omarchy bar plugin add omarchy.clock center | omarchy bar plugin move omarchy.media left | omarchy bar plugin move omarchy.clock --section center --index 0 | omarchy bar plugin remove omarchy.tailscale | omarchy bar plugin set omarchy.clock format HH:mm
|
|
|
|
set -euo pipefail
|
|
|
|
source omarchy-shell-config
|
|
|
|
usage() {
|
|
cat <<USAGE
|
|
Usage: omarchy bar plugin <command> [args...]
|
|
|
|
add <id> [placement] Add a bar widget
|
|
move <id> [placement] [--from-section S --from-index N]
|
|
Move a widget within/between sections
|
|
remove <id> [placement] (alias: rm, drop)
|
|
Remove a widget
|
|
set <id> <key> <value> [--json] [placement]
|
|
Set a per-widget option
|
|
replace <old-id> <new-id> Replace the first instance of a widget id
|
|
|
|
Placement (for add/move/remove/set):
|
|
--section <left|center|right> Target section
|
|
--index <n> Insert/remove at index in the target section
|
|
--before <id> Insert before the first matching widget
|
|
--after <id> Insert after the first matching widget
|
|
--from-section <section> Source section (move/remove/set)
|
|
--from-index <n> Source index (move/remove/set)
|
|
--duplicate add: allow a second instance of the widget
|
|
--all remove: remove every matching instance
|
|
|
|
Without placement flags, 'add' inserts after the section anchor (workspaces on
|
|
left, weather on center, tray on right) and dedupes across sections; 'move'
|
|
preserves the source section; 'remove' drops the first match (or every match
|
|
with --all).
|
|
|
|
Widget ids are listed by 'omarchy plugin list'.
|
|
|
|
Examples:
|
|
omarchy bar plugin add omarchy.tailscale
|
|
omarchy bar plugin add omarchy.clock center
|
|
omarchy bar plugin move omarchy.media left
|
|
omarchy bar plugin move omarchy.clock --section center --index 0
|
|
omarchy bar plugin remove omarchy.tailscale
|
|
omarchy bar plugin remove omarchy.tailscale --all
|
|
omarchy bar plugin set omarchy.clock format HH:mm
|
|
omarchy bar plugin set omarchy.indicators items '["Dnd","NightLight"]' --json
|
|
USAGE
|
|
}
|
|
|
|
# jq defs used by the mutation pipelines. All `def`s come first so the pipeline
|
|
# that follows them stays valid jq; NORMALIZE adds its own shape helpers.
|
|
JQ_DEFS='
|
|
def entry_id: if type == "object" then (.id // "" | tostring) else tostring end;
|
|
def anchor_for($section): { left: "omarchy.workspaces", center: "omarchy.weather", right: "omarchy.tray" }[$section];
|
|
def find_all($id; $only):
|
|
[ ["left","center","right"][] as $section
|
|
| ((.bar.layout[$section] // []) | to_entries[])
|
|
| select((($only == "" or $section == $only)) and ((.value | entry_id) == $id))
|
|
| {section: $section, index: .key, entry: .value} ];
|
|
def entry_at($section; $index):
|
|
if $index < 0 or $index >= (.bar.layout[$section] | length) then
|
|
error("no widget at " + $section + "[" + ($index | tostring) + "]")
|
|
else .bar.layout[$section][$index] end;
|
|
def resolve_target($defaultSection):
|
|
. as $root
|
|
| (if $before != "" or $after != "" then
|
|
($root | find_all($before + $after; $targetSection) | .[0]
|
|
// error("could not find target widget " + ($before + $after) + ($targetSection | if . == "" then "" else " in " + . end)))
|
|
| .section as $section | (.index + (if $after == "" then 0 else 1 end)) as $index
|
|
| {section: $section, index: $index, anchor: false}
|
|
elif $targetSection != "" or $targetIndex != "" then
|
|
{ section: ($targetSection | if . == "" then $defaultSection else . end),
|
|
index: (if $targetIndex == "" then null else ($targetIndex | tonumber) end),
|
|
anchor: false }
|
|
else
|
|
{ section: $defaultSection, index: null, anchor: true }
|
|
end)
|
|
| .section as $section
|
|
| .index as $rawIndex
|
|
| (if $rawIndex == null then
|
|
if .anchor then
|
|
($root.bar.layout[$section] | map(entry_id) | index(anchor_for($section))) as $anchorIndex
|
|
| if $anchorIndex == null then ($root.bar.layout[$section] | length) else ($anchorIndex + 1) end
|
|
else ($root.bar.layout[$section] | length) end
|
|
elif $rawIndex > ($root.bar.layout[$section] | length) then ($root.bar.layout[$section] | length)
|
|
else $rawIndex end) as $index
|
|
| {section: $section, index: $index};
|
|
def resolve_source:
|
|
if $fromIndex != "" then
|
|
if $fromSection == "" then error("--from-index requires --from-section")
|
|
else
|
|
($fromIndex | tonumber) as $index
|
|
| entry_at($fromSection; $index) as $entry
|
|
| if $widgetId != "" and ($entry | entry_id) != $widgetId then
|
|
error("widget at " + $fromSection + "[" + ($index | tostring) + "] is not " + $widgetId)
|
|
else {section: $fromSection, index: $index, entry: $entry} end
|
|
end
|
|
else
|
|
(find_all($widgetId; $fromSection)
|
|
| if length == 0 then
|
|
error("could not find widget " + $widgetId + ($fromSection | if . == "" then "" else " in " + . end))
|
|
else .[0] end)
|
|
end;
|
|
'
|
|
|
|
validate_section() {
|
|
[[ $1 =~ ^(left|center|right)$ ]] || fail "section must be left, center, or right"
|
|
}
|
|
|
|
validate_index() {
|
|
[[ $1 =~ ^[0-9]+$ ]] || fail "index must be a non-negative integer"
|
|
}
|
|
|
|
widget_is_known() {
|
|
omarchy-plugin-catalog | jq -e --arg id "$1" '
|
|
any(.[]; (.kinds | index("bar-widget")) and .id == $id)
|
|
' >/dev/null
|
|
}
|
|
|
|
widget_default_section() {
|
|
omarchy-plugin-catalog | jq -r --arg id "$1" '
|
|
(map(select(.id == $id))[0].barWidget.defaultSection // "center") as $section
|
|
| if ["left", "center", "right"] | index($section) then $section else "center" end
|
|
'
|
|
}
|
|
|
|
# --------------------------------------------------------------------- placement
|
|
|
|
# Parse placement flags shared by add/move/remove/set. Sets the globals below.
|
|
PLACEMENT_SECTION=""
|
|
PLACEMENT_INDEX=""
|
|
PLACEMENT_BEFORE=""
|
|
PLACEMENT_AFTER=""
|
|
PLACEMENT_FROM_SECTION=""
|
|
PLACEMENT_FROM_INDEX=""
|
|
PLACEMENT_DUPLICATE="false"
|
|
PLACEMENT_ALL="false"
|
|
|
|
parse_placement() {
|
|
while (( $# > 0 )); do
|
|
case "$1" in
|
|
--section)
|
|
PLACEMENT_SECTION="${2:-}"
|
|
validate_section "$PLACEMENT_SECTION"
|
|
shift 2
|
|
;;
|
|
--index)
|
|
PLACEMENT_INDEX="${2:-}"
|
|
validate_index "$PLACEMENT_INDEX"
|
|
shift 2
|
|
;;
|
|
--before)
|
|
PLACEMENT_BEFORE="${2:-}"
|
|
[[ -n $PLACEMENT_BEFORE ]] || fail "--before requires a widget id"
|
|
shift 2
|
|
;;
|
|
--after)
|
|
PLACEMENT_AFTER="${2:-}"
|
|
[[ -n $PLACEMENT_AFTER ]] || fail "--after requires a widget id"
|
|
shift 2
|
|
;;
|
|
--from-section)
|
|
PLACEMENT_FROM_SECTION="${2:-}"
|
|
validate_section "$PLACEMENT_FROM_SECTION"
|
|
shift 2
|
|
;;
|
|
--from-index)
|
|
PLACEMENT_FROM_INDEX="${2:-}"
|
|
validate_index "$PLACEMENT_FROM_INDEX"
|
|
shift 2
|
|
;;
|
|
--duplicate)
|
|
PLACEMENT_DUPLICATE="true"
|
|
shift
|
|
;;
|
|
--all)
|
|
PLACEMENT_ALL="true"
|
|
shift
|
|
;;
|
|
-h | --help)
|
|
usage
|
|
exit 0
|
|
;;
|
|
*)
|
|
fail "unknown option: $1"
|
|
;;
|
|
esac
|
|
done
|
|
|
|
[[ -z $PLACEMENT_BEFORE || -z $PLACEMENT_AFTER ]] || fail "use only one of --before or --after"
|
|
}
|
|
|
|
# --------------------------------------------------------------------- commands
|
|
|
|
cmd_add() {
|
|
local id="${1:-}"
|
|
[[ -n $id ]] || fail "add requires a widget id"
|
|
shift
|
|
|
|
local positional_section=""
|
|
while (( $# > 0 )); do
|
|
if [[ $1 == --* ]]; then
|
|
break
|
|
fi
|
|
[[ -z $positional_section ]] || fail "unexpected argument: $1"
|
|
positional_section="$1"
|
|
shift
|
|
done
|
|
|
|
parse_placement "$@"
|
|
[[ -z $positional_section || -z $PLACEMENT_SECTION ]] || fail "specify a section positionally or with --section, not both"
|
|
[[ -z $positional_section || -z $PLACEMENT_INDEX ]] || fail "specify a section positionally or use --index, not both"
|
|
[[ -z $positional_section || -z $PLACEMENT_BEFORE ]] || fail "specify a section positionally or use --before, not both"
|
|
[[ -z $positional_section || -z $PLACEMENT_AFTER ]] || fail "specify a section positionally or use --after, not both"
|
|
[[ -z $positional_section || ( $PLACEMENT_DUPLICATE == false ) ]] || fail "--duplicate needs explicit placement flags"
|
|
if [[ -n $positional_section ]]; then
|
|
validate_section "$positional_section"
|
|
PLACEMENT_SECTION="$positional_section"
|
|
fi
|
|
|
|
widget_is_known "$id" || fail "$id is not a known bar widget; run 'omarchy plugin list' to see valid ids"
|
|
|
|
local default_section="${PLACEMENT_SECTION:-$(widget_default_section "$id")}"
|
|
local explicit="false"
|
|
if [[ -n $PLACEMENT_INDEX || -n $PLACEMENT_BEFORE || -n $PLACEMENT_AFTER || $PLACEMENT_DUPLICATE == true ]]; then
|
|
explicit="true"
|
|
fi
|
|
|
|
if [[ $explicit == "false" ]]; then
|
|
# Anchor-based dedupe insert: remove any existing instance across sections,
|
|
# then insert after the section anchor (workspaces/weather/tray) — or append
|
|
# when the anchor is absent.
|
|
commit "$JQ_DEFS $NORMALIZE
|
|
| .bar.layout.left = (.bar.layout.left | map(select(entry_id != \$id)))
|
|
| .bar.layout.center = (.bar.layout.center | map(select(entry_id != \$id)))
|
|
| .bar.layout.right = (.bar.layout.right | map(select(entry_id != \$id)))
|
|
| (.bar.layout[\$section] | map(entry_id) | index(anchor_for(\$section))) as \$anchorIndex
|
|
| .bar.layout[\$section] = (
|
|
if \$anchorIndex == null then .bar.layout[\$section] + [{id: \$id}]
|
|
else .bar.layout[\$section][0:\$anchorIndex+1] + [{id: \$id}] + .bar.layout[\$section][\$anchorIndex+1:] end)
|
|
" \
|
|
--arg id "$id" \
|
|
--arg section "$default_section"
|
|
else
|
|
local prog
|
|
prog=$(cat <<JQ
|
|
$JQ_DEFS $NORMALIZE
|
|
| (.bar.layout.left + .bar.layout.center + .bar.layout.right | map(entry_id) | index(\$id)) as \$existing
|
|
| if \$existing != null and \$allowDuplicate != "true" then
|
|
error(\$id + " is already in the bar; use 'omarchy bar plugin move' or pass --duplicate")
|
|
else . end
|
|
| resolve_target(\$defaultSection) as \$target
|
|
| .bar.layout[\$target.section] = (.bar.layout[\$target.section][0:\$target.index] + [{id: \$id}] + .bar.layout[\$target.section][\$target.index:])
|
|
JQ
|
|
)
|
|
commit "$prog" \
|
|
--arg id "$id" \
|
|
--arg defaultSection "$default_section" \
|
|
--arg targetSection "$PLACEMENT_SECTION" \
|
|
--arg targetIndex "$PLACEMENT_INDEX" \
|
|
--arg before "$PLACEMENT_BEFORE" \
|
|
--arg after "$PLACEMENT_AFTER" \
|
|
--arg allowDuplicate "$PLACEMENT_DUPLICATE"
|
|
fi
|
|
echo "Added $id to the bar"
|
|
}
|
|
|
|
cmd_move() {
|
|
local id="${1:-}"
|
|
[[ -n $id ]] || fail "move requires a widget id"
|
|
shift
|
|
|
|
local positional_section=""
|
|
while (( $# > 0 )); do
|
|
if [[ $1 == --* ]]; then
|
|
break
|
|
fi
|
|
[[ -z $positional_section ]] || fail "unexpected argument: $1"
|
|
positional_section="$1"
|
|
shift
|
|
done
|
|
|
|
parse_placement "$@"
|
|
[[ -z $positional_section || -z $PLACEMENT_SECTION ]] || fail "specify a section positionally or with --section, not both"
|
|
[[ -z $positional_section || -z $PLACEMENT_INDEX ]] || fail "specify a section positionally or use --index, not both"
|
|
[[ -z $positional_section || -z $PLACEMENT_BEFORE ]] || fail "specify a section positionally or use --before, not both"
|
|
[[ -z $positional_section || -z $PLACEMENT_AFTER ]] || fail "specify a section positionally or use --after, not both"
|
|
if [[ -n $positional_section ]]; then
|
|
validate_section "$positional_section"
|
|
PLACEMENT_SECTION="$positional_section"
|
|
fi
|
|
|
|
local default_section="${PLACEMENT_SECTION:-}"
|
|
|
|
# A bare section names the section, not the slot, so let it fall through to
|
|
# the same anchor placement 'add' uses. Passing it as an explicit target
|
|
# instead drops the widget on the far end of the row.
|
|
local target_section="$PLACEMENT_SECTION"
|
|
if [[ -z $PLACEMENT_INDEX && -z $PLACEMENT_BEFORE && -z $PLACEMENT_AFTER ]]; then
|
|
target_section=""
|
|
fi
|
|
|
|
local prog
|
|
prog=$(cat <<JQ
|
|
$JQ_DEFS $NORMALIZE
|
|
| resolve_source as \$source
|
|
| .bar.layout[\$source.section] = (.bar.layout[\$source.section][0:\$source.index] + .bar.layout[\$source.section][\$source.index+1:])
|
|
| (\$defaultSection | if . == "" then \$source.section else . end) as \$fallback
|
|
| resolve_target(\$fallback) as \$target
|
|
| (\$source.entry | if type == "object" then .id = \$id else . end) as \$entry
|
|
| .bar.layout[\$target.section] = (.bar.layout[\$target.section][0:\$target.index] + [\$entry] + .bar.layout[\$target.section][\$target.index:])
|
|
JQ
|
|
)
|
|
commit "$prog" \
|
|
--arg id "$id" \
|
|
--arg defaultSection "$default_section" \
|
|
--arg targetSection "$target_section" \
|
|
--arg targetIndex "$PLACEMENT_INDEX" \
|
|
--arg before "$PLACEMENT_BEFORE" \
|
|
--arg after "$PLACEMENT_AFTER" \
|
|
--arg fromSection "$PLACEMENT_FROM_SECTION" \
|
|
--arg fromIndex "$PLACEMENT_FROM_INDEX" \
|
|
--arg widgetId "$id"
|
|
echo "Moved $id"
|
|
}
|
|
|
|
cmd_remove() {
|
|
local id=""
|
|
if (( $# > 0 )) && [[ $1 != --* ]]; then
|
|
id="$1"
|
|
shift
|
|
fi
|
|
parse_placement "$@"
|
|
|
|
local prog
|
|
prog=$(cat <<JQ
|
|
$JQ_DEFS $NORMALIZE
|
|
| if \$fromIndex != "" then
|
|
resolve_source as \$source
|
|
| .bar.layout[\$source.section] = (.bar.layout[\$source.section][0:\$source.index] + .bar.layout[\$source.section][\$source.index+1:])
|
|
elif \$targetIndex != "" then
|
|
if \$targetSection == "" then error("--index requires --section for remove") else . end
|
|
| (\$targetIndex | tonumber) as \$index
|
|
| .bar.layout[\$targetSection][\$index] as \$entry
|
|
| if \$widgetId != "" and (\$entry | entry_id) != \$widgetId then
|
|
error("widget at " + \$targetSection + "[" + (\$index | tostring) + "] is not " + \$widgetId)
|
|
else . end
|
|
| .bar.layout[\$targetSection] = (.bar.layout[\$targetSection][0:\$index] + .bar.layout[\$targetSection][\$index+1:])
|
|
else
|
|
find_all(\$widgetId; \$targetSection) as \$matches
|
|
| if (\$matches | length) == 0 then
|
|
error("could not find widget " + \$widgetId + (\$targetSection | if . == "" then "" else " in " + . end))
|
|
elif \$removeAll != "true" then
|
|
(\$matches | sort_by(.section) | reverse | .[0]) as \$first
|
|
| .bar.layout[\$first.section] = (.bar.layout[\$first.section][0:\$first.index] + .bar.layout[\$first.section][\$first.index+1:])
|
|
else
|
|
reduce (\$matches | sort_by(.section) | reverse | .[]) as \$m (.;
|
|
.bar.layout[\$m.section] = (.bar.layout[\$m.section][0:\$m.index] + .bar.layout[\$m.section][\$m.index+1:]))
|
|
end
|
|
end
|
|
JQ
|
|
)
|
|
commit "$prog" \
|
|
--arg widgetId "$id" \
|
|
--arg targetSection "$PLACEMENT_SECTION" \
|
|
--arg targetIndex "$PLACEMENT_INDEX" \
|
|
--arg fromSection "$PLACEMENT_FROM_SECTION" \
|
|
--arg fromIndex "$PLACEMENT_FROM_INDEX" \
|
|
--arg removeAll "$PLACEMENT_ALL"
|
|
if [[ -n $id ]]; then
|
|
echo "Removed $id from the bar"
|
|
else
|
|
echo "Removed bar widget"
|
|
fi
|
|
}
|
|
|
|
cmd_set() {
|
|
local id="${1:-}"
|
|
local key="${2:-}"
|
|
local value="${3:-}"
|
|
[[ -n $id ]] || fail "set requires a widget id"
|
|
[[ -n $key ]] || fail "set requires a setting key"
|
|
(( $# >= 3 )) || fail "set requires a value"
|
|
shift 3
|
|
local value_is_json="false"
|
|
while (( $# > 0 )); do
|
|
case "$1" in
|
|
--json) value_is_json="true" ;;
|
|
-h | --help) usage; exit 0 ;;
|
|
*) break ;;
|
|
esac
|
|
shift
|
|
done
|
|
parse_placement "$@"
|
|
|
|
if [[ $value_is_json == "true" ]]; then
|
|
jq -n --argjson value "$value" empty >/dev/null 2>&1 || fail "invalid JSON value: $value"
|
|
fi
|
|
|
|
local value_arg
|
|
if [[ $value_is_json == "true" ]]; then
|
|
value_arg=(--argjson value "$value")
|
|
else
|
|
value_arg=(--arg value "$value")
|
|
fi
|
|
|
|
local prog
|
|
prog=$(cat <<JQ
|
|
$JQ_DEFS $NORMALIZE
|
|
| if \$fromIndex != "" then
|
|
resolve_source as \$source
|
|
| .bar.layout[\$source.section][\$source.index] = (.bar.layout[\$source.section][\$source.index] | if type == "object" then .[\$key] = \$value else error("widget entry must be an object") end)
|
|
elif \$targetIndex != "" then
|
|
if \$targetSection == "" then error("--index requires --section for set") else . end
|
|
| (\$targetIndex | tonumber) as \$index
|
|
| .bar.layout[\$targetSection][\$index] as \$entry
|
|
| if (\$entry | entry_id) != \$widgetId then
|
|
error("widget at " + \$targetSection + "[" + (\$index | tostring) + "] is not " + \$widgetId)
|
|
elif \$entry | type != "object" then
|
|
error("widget entry must be an object")
|
|
else . end
|
|
| .bar.layout[\$targetSection][\$index][\$key] = \$value
|
|
else
|
|
find_all(\$widgetId; \$targetSection) as \$matches
|
|
| if (\$matches | length) == 0 then
|
|
error("could not find widget " + \$widgetId + (\$targetSection | if . == "" then "" else " in " + . end))
|
|
else
|
|
(\$matches | sort_by(.section) | .[0]) as \$first
|
|
| if \$first.entry | type != "object" then error("widget entry must be an object") else . end
|
|
| .bar.layout[\$first.section][\$first.index][\$key] = \$value
|
|
end
|
|
end
|
|
JQ
|
|
)
|
|
commit "$prog" \
|
|
--arg widgetId "$id" \
|
|
--arg key "$key" \
|
|
"${value_arg[@]}" \
|
|
--arg targetSection "$PLACEMENT_SECTION" \
|
|
--arg targetIndex "$PLACEMENT_INDEX" \
|
|
--arg fromSection "$PLACEMENT_FROM_SECTION" \
|
|
--arg fromIndex "$PLACEMENT_FROM_INDEX"
|
|
echo "Set $key on $id"
|
|
}
|
|
|
|
cmd_replace() {
|
|
local old="${1:-}"
|
|
local new="${2:-}"
|
|
[[ -n $old ]] || fail "replace requires the source widget id"
|
|
[[ -n $new ]] || fail "replace requires the replacement widget id"
|
|
(( $# == 2 )) || fail "replace takes two widget ids"
|
|
|
|
local prog
|
|
prog=$(cat <<JQ
|
|
$JQ_DEFS $NORMALIZE
|
|
| resolve_source as \$source
|
|
| if \$source.entry | type != "object" then error("widget entry must be an object") else . end
|
|
| .bar.layout[\$source.section][\$source.index].id = \$new
|
|
JQ
|
|
)
|
|
# resolve_source finds by widget id when no placement is given; replace
|
|
# takes no placement flags, so pass empty placement explicitly.
|
|
commit "$prog" \
|
|
--arg widgetId "$old" \
|
|
--arg new "$new" \
|
|
--arg fromSection "" \
|
|
--arg fromIndex ""
|
|
echo "Replaced $old with $new"
|
|
}
|
|
|
|
# --------------------------------------------------------------------- dispatch
|
|
|
|
command="${1:-}"
|
|
(( $# > 0 )) && shift || true
|
|
|
|
case "$command" in
|
|
add)
|
|
cmd_add "$@"
|
|
;;
|
|
move)
|
|
cmd_move "$@"
|
|
;;
|
|
remove | rm | drop)
|
|
cmd_remove "$@"
|
|
;;
|
|
set)
|
|
cmd_set "$@"
|
|
;;
|
|
replace)
|
|
cmd_replace "$@"
|
|
;;
|
|
-h | --help | help | "")
|
|
usage
|
|
;;
|
|
*)
|
|
fail "unknown command: $command"
|
|
;;
|
|
esac
|