Skip to content

Commit

Permalink
Merge pull request #400 from Oliver-Bagin/master
Browse files Browse the repository at this point in the history
CPU Monitor Click Through
  • Loading branch information
Jean-Tinland authored May 20, 2024
2 parents c3a63f9 + 83574ce commit d908bca
Show file tree
Hide file tree
Showing 5 changed files with 79 additions and 3 deletions.
30 changes: 27 additions & 3 deletions lib/components/data/cpu.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ 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;
Expand Down Expand Up @@ -65,9 +66,21 @@ export const Widget = React.memo(() => {

const { usage } = state;

const onClick =
cpuMonitorApp === "None"
? undefined
: (e) => {
Utils.clickEffect(e);
openCpuUsageApp(cpuMonitorApp);
};

if (displayAsGraph) {
return (
<DataWidget.Widget classes="cpu cpu--graph" disableSlider>
<DataWidget.Widget
classes="cpu cpu--graph"
onClick={onClick}
disableSlider
>
<Graph
className="cpu__graph"
caption={{
Expand All @@ -86,10 +99,21 @@ export const Widget = React.memo(() => {
}

return (
<DataWidget.Widget classes="cpu" Icon={Icons.CPU}>
<DataWidget.Widget classes="cpu" Icon={Icons.CPU} onClick={onClick}>
<span className="cpu__usage">{usage}%</span>
</DataWidget.Widget>
);
});

Widget.displayName = "Cpu";

function openCpuUsageApp(cpuUsageApp) {
switch (cpuUsageApp) {
case "Activity Monitor":
Uebersicht.run(`open -a "Activity Monitor"`);
break;
case "Top":
Utils.runInUserTerminal("top");
break;
}
}
9 changes: 9 additions & 0 deletions lib/scripts/run-command-in-iterm2.applescript
Original file line number Diff line number Diff line change
@@ -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
7 changes: 7 additions & 0 deletions lib/scripts/run-command-in-terminal.applescript
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down Expand Up @@ -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/",
Expand Down Expand Up @@ -515,6 +528,7 @@ export const defaultSettings = {
fontSize: "11px",
yabaiPath: "/usr/local/bin/yabai",
shell: "sh",
terminal: "Terminal",
slidingAnimationPace: 4,
externalConfigFile: false,
enableServer: false,
Expand Down Expand Up @@ -589,6 +603,7 @@ export const defaultSettings = {
refreshFrequency: 2000,
showOnDisplay: "",
displayAsGraph: false,
cpuMonitorApp: "Top",
},
batteryWidgetOptions: {
refreshFrequency: 10000,
Expand Down
21 changes: 21 additions & 0 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit d908bca

Please sign in to comment.