feat(xbar): switch to xbar (BitBar replacement) and re-write my scripts

This is a complete rewrite of my old scripts, and a wholesale
replacement of BitBar's "brew-services" script with a more feature
packed variant.
This commit is contained in:
2021-10-31 22:39:25 +00:00
parent deaa9aebc8
commit 0f71533a8c
8 changed files with 742 additions and 277 deletions

1
bitbar Symbolic link
View File

@@ -0,0 +1 @@
./xbar

View File

@@ -1,57 +0,0 @@
#!/bin/bash
# <bitbar.title>Homebrew Cask Upgrades</bitbar.title>
# <bitbar.version>1.0.0</bitbar.version>
# <bitbar.author>Jim Myhrberg</bitbar.author>
# <bitbar.author.github>jimeh</bitbar.author.github>
# <bitbar.desc>Show outdated Homebrew Cask formula</bitbar.desc>
# <bitbar.image>https://i.imgur.com/VUMVwZM.png</bitbar.image>
# <bitbar.dependencies>bash</bitbar.dependencies>
shopt -s extglob
main() {
local count
local formulas
local current_version
local new_version
formulas=()
current_version=()
new_version=()
/usr/local/bin/brew update &> /dev/null
while read -r line; do
if [[ "$line" =~ ^(.+)\ \((.+)\)\ !=\ (.*)$ ]]; then
formulas+=("${BASH_REMATCH[1]}")
current_version+=("${BASH_REMATCH[2]}")
new_version+=("${BASH_REMATCH[3]}")
fi
done < <(/usr/local/bin/brew outdated --cask --verbose)
count="${#formulas[@]}"
echo ":tropical_drink:↑${count} | dropdown=false"
echo '---'
if [ "$count" == "1" ]; then
echo "$count outdated cask"
else
echo "$count outdated casks"
fi
if [ "$count" -gt 0 ]; then
echo 'Upgrade all casks | terminal=true refresh=true' \
'bash=/usr/local/bin/brew param1=upgrade param2=--cask'
echo '---'
echo 'Upgrade:'
for i in "${!formulas[@]}"; do
echo "${formulas[$i]}"
echo "--${current_version[$i]}${new_version[$i]} |" \
"terminal=true refresh=true bash=/usr/local/bin/brew param1=upgrade" \
"param2=--cask param3=${formulas[$i]}"
done
fi
}
main "$@"

View File

@@ -1,119 +0,0 @@
#!/usr/bin/env ruby
# <bitbar.title>Brew Services</bitbar.title>
# <bitbar.version>v1.0</bitbar.version>
# <bitbar.author>Adam Lindberg</bitbar.author>
# <bitbar.author.github>eproxus</bitbar.author.github>
# <bitbar.desc>Shows and manages Homebrew services.</bitbar.desc>
# <bitbar.image>http://i.imgur.com/hVfhHYP.jpg</bitbar.image>
# <bitbar.dependencies>ruby, brew, brew-services</bitbar.dependencies>
# BitBar Homebrew services plugin
# by Adam Lindbeng (@eproxus)
#--- User parameters ----------------------------------------------------------
BAR_COLORS = true
#--- Script internals ---------------------------------------------------------
require 'pathname'
SCRIPT_PATH = Pathname.new($0).realpath()
BREW = "/usr/local/bin/brew"
BREW_LINK = "http://brew.sh/"
BREW_SERVICES = "/usr/local/Homebrew/Library/Taps/homebrew/homebrew-services/cmd/services.rb"
BREW_SERVICES_LINK = "https://github.com/Homebrew/homebrew-services"
REFRESH = "---\nRefresh | refresh=true"
if BAR_COLORS
DARK_MODE=`defaults read -g AppleInterfaceStyle 2> /dev/null`.strip
RESET_COLOR = DARK_MODE == 'Dark' ? "\e[37m" : "\e[30m"
else
RESET_COLOR = "\e[37m"
end
if !File.exist?(BREW)
puts [
"Homebrew not installed | color=red",
"---",
"Install Homebrew... | href=#{BREW_LINK}",
REFRESH,
].join("\n")
exit(1)
end
if !File.exist?(BREW_SERVICES)
puts [
"Homebrew Services not installed | color=red",
"---",
"Install Homebrew Services... | href=#{BREW_SERVICES_LINK}",
REFRESH,
].join("\n")
exit(1)
end
def green(string)
"\e[1m\e[32m#{string}#{RESET_COLOR}"
end
def service(command, name)
"bash=\"#{BREW}\"" \
+ " param1=services param2=#{command} param3=\"#{name}\"" \
+ " terminal=false refresh=true"
end
def menu(name, status, user)
if status == "started"
[
"#{name} | color=#4FFF50",
"--Restart | #{service("restart", name)}",
"--Stop | #{service("stop", name)}",
"-----",
"--State: #{status}",
"--User: #{user}",
]
else
[
name,
"--Start | #{service("start", name)}",
"-----",
"--State: #{status}",
]
end
end
def plural(count)
count <= 1 ? "#{count} Service" : "#{count} Services"
end
output = `#{BREW} services list`.split("\n")[1..-1]
services = output && output.reduce({started: 0, menus: []}) do |acc, service|
name, status, user, _plist = service.split
acc[:started] += 1 if status == "started"
acc[:menus] += menu(name, status, user)
acc
end
total = (output || []).length
started = services[:started]
menus = services[:menus].join("\n")
all = ""
if total > 0
all = """
All
--Start #{plural(total - started)} | #{service("start", "--all")}
--Stop #{plural(started)} | #{service("stop", "--all")}
--Restart #{plural(total)} | #{service("restart", "--all")}
"""
end
puts """
#{started != 0 && BAR_COLORS ? green(started) : started}/#{total}
---
#{menus}
---
#{all}
#{REFRESH}
"""

View File

@@ -1,101 +0,0 @@
#!/bin/bash
# <bitbar.title>Homebrew Upgrades</bitbar.title>
# <bitbar.version>1.0.0</bitbar.version>
# <bitbar.author>Jim Myhrberg</bitbar.author>
# <bitbar.author.github>jimeh</bitbar.author.github>
# <bitbar.desc>Show outdated Homebrew formula</bitbar.desc>
# <bitbar.image>https://i.imgur.com/bZn3RYs.png</bitbar.image>
# <bitbar.dependencies>bash,comm,grep,printf</bitbar.dependencies>
shopt -s extglob
main() {
local count
local pinned
local pinned_list
local formulas
local formulas_list
local current_version
local new_version
local pinned_version
local pkg
pinned=()
formulas=()
current_version=()
new_version=()
pinned_version=()
/usr/local/bin/brew update &> /dev/null
while read -r line; do
pinned+=("$line")
done < <(/usr/local/bin/brew list --pinned)
while read -r line; do
if [[ "$line" =~ ^(.+)\ \((.+)\)\ \<\ (.*)\ \[pinned\ at\ (.*)]$ ]]; then
formulas+=("${BASH_REMATCH[1]}")
current_version+=("${BASH_REMATCH[2]}")
new_version+=("${BASH_REMATCH[3]}")
pinned_version+=("${BASH_REMATCH[4]}")
elif [[ "$line" =~ ^(.+)\ \((.+)\)\ \<\ (.*)$ ]]; then
formulas+=("${BASH_REMATCH[1]}")
current_version+=("${BASH_REMATCH[2]}")
new_version+=("${BASH_REMATCH[3]}")
pinned_version+=("NONE")
fi
done < <(/usr/local/bin/brew outdated --verbose)
pinned_list="$(printf '%s\n' "${pinned[@]}" | sort)"
formulas_list="$(printf '%s\n' "${formulas[@]}" | sort)"
count="$(
comm -13 <(echo "$pinned_list") <(echo "$formulas_list") |
grep -c '[^[:space:]]'
)"
echo ":beer:↑${count} | dropdown=false"
echo '---'
if [ "${#pinned[@]}" -gt 0 ]; then
echo "$count outdated formula (${#pinned[@]} pinned)"
else
echo "$count outdated formula"
fi
if [ "$count" -gt 0 ]; then
echo 'Upgrade all formula | terminal=true refresh=true' \
'bash=/usr/local/bin/brew param1=upgrade'
echo '---'
echo 'Upgrade:'
for i in "${!formulas[@]}"; do
pkg="${formulas[$i]}"
if echo "$pinned_list" | grep "${pkg}" > /dev/null; then
continue
fi
echo "$pkg"
echo "--${current_version[$i]}${new_version[$i]} |" \
"terminal=true refresh=true bash=/usr/local/bin/brew" \
"param1=upgrade param2=${pkg}"
done
if [ "${#pinned[@]}" -gt 0 ]; then
echo '---'
echo 'Pinned:'
for i in "${!formulas[@]}"; do
pkg="${formulas[$i]}"
if ! echo "$pinned_list" | grep "${pkg}" > /dev/null; then
continue
fi
echo "$pkg"
echo "--${pinned_version[$i]}${new_version[$i]}"
done
fi
fi
}
main "$@"