From 58dd05f600bc1ccee9ff9d76a842e6cf78a0a1e6 Mon Sep 17 00:00:00 2001 From: AppScoreOli Date: Sat, 11 May 2024 19:02:49 +1000 Subject: [PATCH 1/2] Added in the ability to click on the CPU usage widget Adding in the ability to click on this widget I wanted users to be able to use either 'top' or 'activity monitor'. Because 'top' is run in a terminal I wanted users to be able to specify their preferred Terminal. I added two terminal options Terminal and iTerm2. Hopefully the ability to open a terminal of the users choosing and run an arbitrary command will be helpful else where in the project. --- lib/components/data/cpu.jsx | 22 ++++++++++++++++--- lib/scripts/run-command-in-iterm2.applescript | 9 ++++++++ .../run-command-in-terminal.applescript | 7 ++++++ lib/settings.js | 15 +++++++++++++ lib/utils.js | 21 ++++++++++++++++++ 5 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 lib/scripts/run-command-in-iterm2.applescript create mode 100644 lib/scripts/run-command-in-terminal.applescript diff --git a/lib/components/data/cpu.jsx b/lib/components/data/cpu.jsx index d00cc33f..33a366ef 100644 --- a/lib/components/data/cpu.jsx +++ b/lib/components/data/cpu.jsx @@ -19,7 +19,7 @@ export const Widget = React.memo(() => { const { displayIndex, settings } = useSimpleBarContext(); const { widgets, cpuWidgetOptions } = settings; const { cpuWidget } = widgets; - const { refreshFrequency, showOnDisplay, displayAsGraph } = cpuWidgetOptions; + const { refreshFrequency, showOnDisplay, displayAsGraph, cpuMonitorApp } = cpuWidgetOptions; const visible = Utils.isVisibleOnDisplay(displayIndex, showOnDisplay) && cpuWidget; @@ -65,9 +65,14 @@ export const Widget = React.memo(() => { const { usage } = state; + const onClick = cpuMonitorApp === "None" ? undefined : (e) => { + Utils.clickEffect(e); + openCpuUsageApp(cpuMonitorApp); + }; + if (displayAsGraph) { return ( - + { } return ( - + {usage}% ); }); Widget.displayName = "Cpu"; + +function openCpuUsageApp(cpuUsageApp) { + switch (cpuUsageApp) { + case "Activity Monitor": + Uebersicht.run(`open -a "Activity Monitor"`); + break; + case "Top": + Utils.runInUserTerminal('top') + break; + } +} diff --git a/lib/scripts/run-command-in-iterm2.applescript b/lib/scripts/run-command-in-iterm2.applescript new file mode 100644 index 00000000..ab00115b --- /dev/null +++ b/lib/scripts/run-command-in-iterm2.applescript @@ -0,0 +1,9 @@ +on run argv + set commandToRun to item 1 of argv + tell application "iTerm" + set newWindow to (create window with default profile) + tell current session of newWindow + write text commandToRun + end tell + end tell +end run \ No newline at end of file diff --git a/lib/scripts/run-command-in-terminal.applescript b/lib/scripts/run-command-in-terminal.applescript new file mode 100644 index 00000000..898ec7a6 --- /dev/null +++ b/lib/scripts/run-command-in-terminal.applescript @@ -0,0 +1,7 @@ +on run argv + set commandToRun to item 1 of argv + tell application "Terminal" + do script commandToRun + activate + end tell +end run \ No newline at end of file diff --git a/lib/settings.js b/lib/settings.js index f693cb14..037a171c 100644 --- a/lib/settings.js +++ b/lib/settings.js @@ -75,6 +75,12 @@ export const data = { type: "radio", options: ["sh", "bash", "dash"], }, + terminal: { + title: "Which terminal should user facing commands be run in?", + label: "", + type: "radio", + options: ["Terminal", "iTerm2"], + }, slidingAnimationPace: { label: "Sliding animation speed", type: "number", @@ -300,6 +306,13 @@ export const data = { fullWidth: true, }, + cpuMonitorApp: { + title: "Cpu Monitor", + label: "", + type: "radio", + options: ["Top", "Activity Monitor", "None"], + }, + batteryWidgetOptions: { label: "Battery", documentation: "/battery/", @@ -515,6 +528,7 @@ export const defaultSettings = { fontSize: "11px", yabaiPath: "/usr/local/bin/yabai", shell: "sh", + terminal: "Terminal", slidingAnimationPace: 4, externalConfigFile: false, enableServer: false, @@ -589,6 +603,7 @@ export const defaultSettings = { refreshFrequency: 2000, showOnDisplay: "", displayAsGraph: false, + cpuMonitorApp: "Top" }, batteryWidgetOptions: { refreshFrequency: 10000, diff --git a/lib/utils.js b/lib/utils.js index a0427fa3..f4659615 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -425,3 +425,24 @@ export function getRefreshFrequency(value, defaultValue) { const parsedValue = parseInt(value, 10); return isNaN(parsedValue) ? defaultValue : parsedValue; } + +export function runInUserTerminal(command) { + const settings = Settings.get(); + const { terminal } = settings.global; + switch (terminal) { + case "Terminal": + Uebersicht.run( + `osascript ./simple-bar/lib/scripts/run-command-in-terminal.applescript` + + ` "${command}"` + ) + break; + case "iTerm2": + Uebersicht.run( + `osascript ./simple-bar/lib/scripts/run-command-in-iterm2.applescript` + + ` "${command}"` + ) + break; + default: + break; + } +} \ No newline at end of file From 83574ce29de0a151b46fdd2be36f36b214e31d4b Mon Sep 17 00:00:00 2001 From: AppScoreOli Date: Sat, 11 May 2024 19:20:40 +1000 Subject: [PATCH 2/2] Format and lint --- lib/components/data/cpu.jsx | 24 ++++++++++++++++-------- lib/settings.js | 2 +- lib/utils.js | 10 +++++----- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/lib/components/data/cpu.jsx b/lib/components/data/cpu.jsx index 33a366ef..c1e776d6 100644 --- a/lib/components/data/cpu.jsx +++ b/lib/components/data/cpu.jsx @@ -19,7 +19,8 @@ export const Widget = React.memo(() => { const { displayIndex, settings } = useSimpleBarContext(); const { widgets, cpuWidgetOptions } = settings; const { cpuWidget } = widgets; - const { refreshFrequency, showOnDisplay, displayAsGraph, cpuMonitorApp } = cpuWidgetOptions; + const { refreshFrequency, showOnDisplay, displayAsGraph, cpuMonitorApp } = + cpuWidgetOptions; const visible = Utils.isVisibleOnDisplay(displayIndex, showOnDisplay) && cpuWidget; @@ -65,14 +66,21 @@ export const Widget = React.memo(() => { const { usage } = state; - const onClick = cpuMonitorApp === "None" ? undefined : (e) => { - Utils.clickEffect(e); - openCpuUsageApp(cpuMonitorApp); - }; + const onClick = + cpuMonitorApp === "None" + ? undefined + : (e) => { + Utils.clickEffect(e); + openCpuUsageApp(cpuMonitorApp); + }; if (displayAsGraph) { return ( - +