mirror of
https://github.com/Toxblh/MTMR.git
synced 2026-01-10 00:58:37 +00:00
Parse title/image from a script's output (#418)
This commit is contained in:
parent
211ca4be32
commit
352bf4887c
@ -12,6 +12,11 @@ class ShellScriptTouchBarItem: CustomButtonTouchBarItem {
|
|||||||
private let source: String
|
private let source: String
|
||||||
private var forceHideConstraint: NSLayoutConstraint!
|
private var forceHideConstraint: NSLayoutConstraint!
|
||||||
|
|
||||||
|
struct ScriptResult: Decodable {
|
||||||
|
var title: String?
|
||||||
|
var image: Source?
|
||||||
|
}
|
||||||
|
|
||||||
init?(identifier: NSTouchBarItem.Identifier, source: SourceProtocol, interval: TimeInterval) {
|
init?(identifier: NSTouchBarItem.Identifier, source: SourceProtocol, interval: TimeInterval) {
|
||||||
self.interval = interval
|
self.interval = interval
|
||||||
self.source = source.string ?? "echo No \"source\""
|
self.source = source.string ?? "echo No \"source\""
|
||||||
@ -31,12 +36,25 @@ class ShellScriptTouchBarItem: CustomButtonTouchBarItem {
|
|||||||
func refreshAndSchedule() {
|
func refreshAndSchedule() {
|
||||||
// Execute script and get result
|
// Execute script and get result
|
||||||
let scriptResult = execute(source)
|
let scriptResult = execute(source)
|
||||||
|
var rawTitle: String, image: NSImage?
|
||||||
|
var json: Bool
|
||||||
|
|
||||||
|
do {
|
||||||
|
let decoder = JSONDecoder()
|
||||||
|
let result = try decoder.decode(ScriptResult.self, from: scriptResult.data(using: .utf8)!)
|
||||||
|
json = true
|
||||||
|
rawTitle = result.title ?? ""
|
||||||
|
image = result.image?.image
|
||||||
|
} catch {
|
||||||
|
json = false
|
||||||
|
rawTitle = scriptResult
|
||||||
|
}
|
||||||
|
|
||||||
// Apply returned text attributes (if they were returned) to our result string
|
// Apply returned text attributes (if they were returned) to our result string
|
||||||
let helper = AMR_ANSIEscapeHelper.init()
|
let helper = AMR_ANSIEscapeHelper.init()
|
||||||
helper.defaultStringColor = NSColor.white
|
helper.defaultStringColor = NSColor.white
|
||||||
helper.font = "1".defaultTouchbarAttributedString.attribute(.font, at: 0, effectiveRange: nil) as? NSFont
|
helper.font = "1".defaultTouchbarAttributedString.attribute(.font, at: 0, effectiveRange: nil) as? NSFont
|
||||||
let title = NSMutableAttributedString.init(attributedString: helper.attributedString(withANSIEscapedString: scriptResult) ?? NSAttributedString(string: ""))
|
let title = NSMutableAttributedString.init(attributedString: helper.attributedString(withANSIEscapedString: rawTitle) ?? NSAttributedString(string: ""))
|
||||||
title.addAttributes([.baselineOffset: 1], range: NSRange(location: 0, length: title.length))
|
title.addAttributes([.baselineOffset: 1], range: NSRange(location: 0, length: title.length))
|
||||||
let newBackgoundColor: NSColor? = title.length != 0 ? title.attribute(.backgroundColor, at: 0, effectiveRange: nil) as? NSColor : nil
|
let newBackgoundColor: NSColor? = title.length != 0 ? title.attribute(.backgroundColor, at: 0, effectiveRange: nil) as? NSColor : nil
|
||||||
|
|
||||||
@ -46,6 +64,9 @@ class ShellScriptTouchBarItem: CustomButtonTouchBarItem {
|
|||||||
self?.backgroundColor = newBackgoundColor
|
self?.backgroundColor = newBackgoundColor
|
||||||
}
|
}
|
||||||
self?.attributedTitle = title
|
self?.attributedTitle = title
|
||||||
|
if json {
|
||||||
|
self?.image = image
|
||||||
|
}
|
||||||
self?.forceHideConstraint.isActive = scriptResult == ""
|
self?.forceHideConstraint.isActive = scriptResult == ""
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user