wip(xbar/brew-services): update xbar printer class

Adds the updated xbar printer class from brew-updates, which simplifies
the shell syntax.
This commit is contained in:
2021-11-09 12:23:56 +00:00
parent ac48156a18
commit 630558dce2

View File

@@ -2,7 +2,7 @@
# frozen_string_literal: true # frozen_string_literal: true
# <xbar.title>Brew Services</xbar.title> # <xbar.title>Brew Services</xbar.title>
# <xbar.version>v2.1.0</xbar.version> # <xbar.version>v2.2.0</xbar.version>
# <xbar.author>Jim Myhrberg</xbar.author> # <xbar.author>Jim Myhrberg</xbar.author>
# <xbar.author.github>jimeh</xbar.author.github> # <xbar.author.github>jimeh</xbar.author.github>
# <xbar.desc>List and manage Homebrew Services</xbar.desc> # <xbar.desc>List and manage Homebrew Services</xbar.desc>
@@ -46,6 +46,7 @@ module Xbar
def print_item(text, **props) def print_item(text, **props)
output = [text] output = [text]
unless props.empty? unless props.empty?
props = normalize_props(props)
output << PARAM_SEP output << PARAM_SEP
output += props.map { |k, v| "#{k}=\"#{v}\"" } output += props.map { |k, v| "#{k}=\"#{v}\"" }
end end
@@ -54,6 +55,35 @@ module Xbar
$stdout.puts $stdout.puts
end end
def plugin_refresh_uri
@plugin_refresh_uri ||= 'xbar://app.xbarapp.com/refreshPlugin' \
"?path=#{File.basename(__FILE__)}"
end
def normalize_props(props = {})
props = props.dup
if props[:shell].is_a?(Array)
cmd = props[:shell]
props[:shell] = cmd[0]
cmd[1..-1].each_with_index do |c, i|
props["param#{i + 1}".to_sym] = c
end
end
# Refresh Xbar after shell command has run in terminal
if props[:terminal] && props[:refresh] && props[:shell]
props[:refresh] = false
i = 1
i += 1 while props.key?("param#{i}".to_sym)
props["param#{i}".to_sym] = ';'
props["param#{i + 1}".to_sym] = 'open'
props["param#{i + 2}".to_sym] = "'#{plugin_refresh_uri}'"
end
props
end
def sub_printer def sub_printer
@sub_printer || self.class.new(nested_level + 1) @sub_printer || self.class.new(nested_level + 1)
end end
@@ -175,8 +205,8 @@ module Brew
if stopped_services.size.positive? if stopped_services.size.positive?
printer.item( printer.item(
"Start All (#{stopped_services.size} services)", "Start All (#{stopped_services.size} services)",
terminal: false, refresh: true, shell: brew_path, terminal: false, refresh: true,
param1: 'services', param2: 'start', param3: '--all' shell: [brew_path, 'services', 'start', '--all']
) )
else else
printer.item("Start All (#{stopped_services.size} services)") printer.item("Start All (#{stopped_services.size} services)")
@@ -184,8 +214,8 @@ module Brew
if started_services.size.positive? if started_services.size.positive?
printer.item( printer.item(
"Stop All (#{started_services.size} services)", "Stop All (#{started_services.size} services)",
terminal: false, refresh: true, shell: brew_path, terminal: false, refresh: true,
param1: 'services', param2: 'stop', param3: '--all' shell: [brew_path, 'services', 'stop', '--all']
) )
else else
printer.item("Stop All (#{started_services.size} services)") printer.item("Stop All (#{started_services.size} services)")
@@ -194,8 +224,8 @@ module Brew
printer.item( printer.item(
'Restart All ' \ 'Restart All ' \
"(#{started_services.size + stopped_services.size} services)", "(#{started_services.size + stopped_services.size} services)",
terminal: false, refresh: true, shell: brew_path, terminal: false, refresh: true,
param1: 'services', param2: 'restart', param3: '--all' shell: [brew_path, 'services', 'restart', '--all']
) )
else else
printer.item("Restart All (#{services.size} services)") printer.item("Restart All (#{services.size} services)")
@@ -282,20 +312,20 @@ module Brew
if service.started? || service.error? || service.unknown_status? if service.started? || service.error? || service.unknown_status?
printer.item( printer.item(
'Stop', 'Stop',
terminal: false, refresh: true, shell: brew_path, terminal: false, refresh: true,
param1: 'services', param2: 'stop', param3: service.name shell: [brew_path, 'services', 'stop', service.name]
) )
printer.item( printer.item(
'Restart', 'Restart',
terminal: false, refresh: true, shell: brew_path, terminal: false, refresh: true,
param1: 'services', param2: 'restart', param3: service.name shell: [brew_path, 'services', 'restart', service.name]
) )
end end
if service.stopped? || service.unknown_status? if service.stopped? || service.unknown_status?
printer.item( printer.item(
'Start', 'Start',
terminal: false, refresh: true, shell: brew_path, terminal: false, refresh: true,
param1: 'services', param2: 'start', param3: service.name shell: [brew_path, 'services', 'start', service.name]
) )
end end
@@ -311,8 +341,7 @@ module Brew
printer.item( printer.item(
'Yes', 'Yes',
terminal: true, refresh: true, terminal: true, refresh: true,
shell: brew_path, param1: 'uninstall', shell: [brew_path, 'uninstall', service.name]
param2: service.name
) )
end end
end end