Skip to content

Commit

Permalink
Merge pull request #28 from dirtyhenry/small-updates
Browse files Browse the repository at this point in the history
feat: add my own hooks
  • Loading branch information
dirtyhenry authored Jul 24, 2024
2 parents 8215beb + 99f87f7 commit 1271b61
Show file tree
Hide file tree
Showing 8 changed files with 95 additions and 6 deletions.
14 changes: 11 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,10 @@ format:
lint:
swiftformat --lint .
swiftlint lint .
shellcheck Resources/SampleHooks/didFinish.sh
shellcheck Resources/SampleHooks/didStart.sh
shellcheck Resources/SampleHooks/sample-pomodoro-finish.sh
shellcheck Resources/SampleHooks/sample-pomodoro-start.sh
shellcheck Resources/SampleHooks/mickf-pomodoro-finish.sh
shellcheck Resources/SampleHooks/mickf-pomodoro-start.sh

run-test: build
.build/debug/${PRODUCT} --duration 5
Expand All @@ -51,7 +53,13 @@ deploy:
sudo install ".build/release/${PRODUCT}" "$(bindir)/pomodoro-cli"
mkdir -p "$(HOME)/.pomodoro-cli"
touch "$(HOME)/.pomodoro-cli/journal.yml"
cp Resources/SampleHooks/did*.sh "$(HOME)/.pomodoro-cli"
cp Resources/SampleHooks/sample-pomodoro-finish.sh "$(HOME)/.pomodoro-cli/pomodoro-finish.sh"
cp Resources/SampleHooks/sample-pomodoro-start.sh "$(HOME)/.pomodoro-cli/pomodoro-start.sh"

deploy-my-hooks:
cp Resources/SampleHooks/mickf-pomodoro-finish.sh "$(HOME)/.pomodoro-cli/pomodoro-finish.sh"
cp Resources/SampleHooks/mickf-pomodoro-start.sh "$(HOME)/.pomodoro-cli/pomodoro-start.sh"
cp Resources/SampleHooks/notify.js "$(HOME)/.pomodoro-cli/notify.js"

docs:
xcodebuild docbuild -scheme "Pomodoro" -derivedDataPath tmp/derivedDataPath -destination platform=macOS
Expand Down
38 changes: 38 additions & 0 deletions Resources/SampleHooks/mickf-pomodoro-finish.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#!/usr/bin/env bash

set -e

# Check if exactly 4 arguments are provided
if [ "$#" -ne 4 ]; then
echo "Usage: $0 arg1 arg2 arg3 arg4"
exit 1
fi

# This would stop focus.
# /usr/bin/open focus://unfocus

# # Writing to SQLite databse
DATABASE="/Users/mick/Desktop/personal-cms.sqlite"
TABLE="pomodoros"

# Create the table if it doesn't exist
sqlite3 "$DATABASE" <<EOF
CREATE TABLE IF NOT EXISTS $TABLE (
id INTEGER PRIMARY KEY AUTOINCREMENT,
start TEXT NOT NULL,
end TEXT NOT NULL,
message TEXT
);
EOF

# Insert the 4 arguments into the table
sqlite3 "$DATABASE" <<EOF
INSERT INTO $TABLE (start, end, message)
VALUES ('$1', '$2', '$4');
EOF

osascript -l JavaScript /Users/mick/.pomodoro-cli/notify.js "🍅 Pomodoro" "Completed" "$4" "Blow"

# Turn off the display after a 5 seconds delay.
sleep 5
/usr/bin/pmset displaysleepnow
11 changes: 11 additions & 0 deletions Resources/SampleHooks/mickf-pomodoro-start.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#!/usr/bin/env bash

set -e

# Check if exactly 4 arguments are provided
if [ "$#" -ne 4 ]; then
echo "Usage: $0 arg1 arg2 arg3 arg4"
exit 1
fi

afplay ~/Documents/Private/Pop\ Culture/be-curious-not-judgemental.mp3
32 changes: 32 additions & 0 deletions Resources/SampleHooks/notify.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
// Import standard library
ObjC.import("stdlib");

// Convert arguments from Objective-C array to JavaScript array
const args = $.NSProcessInfo.processInfo.arguments;
const argc = $.NSProcessInfo.processInfo.arguments.count;
var arguments = [];
for (var i = 4; i < argc; i++) {
// Starting from 4 to skip script name and AppleScript overhead arguments
arguments.push(ObjC.unwrap(args.objectAtIndex(i)));
}

const displayNotification = (title, subtitle, message, soundName) => {
const app = Application.currentApplication();
app.includeStandardAdditions = true;

// 📜 Checkout out sound names in /System/Library/Sounds/
app.displayNotification(message, {
withTitle: title,
subtitle: subtitle,
soundName: soundName,
});
};

// Use arguments in the function
if (arguments.length >= 4) {
displayNotification(arguments[0], arguments[1], arguments[2], arguments[3]);
} else {
console.log(
"Usage: osascript myScript.js <title> <subtitle> <message> <soundName>"
);
}
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions Sources/Pomodoro/Hook.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ enum Hook {
extension Hook {
// MARK: - Script Support of Hooks

static let didStartScript = "didStart.sh"
static let didFinishScript = "didFinish.sh"
static let didStartScript = "pomodoro-start.sh"
static let didFinishScript = "pomodoro-finish.sh"

private var scriptURL: URL {
switch self {
Expand Down
2 changes: 1 addition & 1 deletion Sources/PomodoroCLI/RootCommand.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ struct PomodoroCLI: ParsableCommand {
@Option(name: .shortAndLong, help: "The intent of the pomodoro (example: email zero)")
var message: String?

@Flag(name: .shortAndLong, help: "Exit right away (escape-hatch to run hooks only)")
@Flag(name: .shortAndLong, help: "Exit right away (escape-hatch to run didFinish hook only)")
var catchUp: Bool = false

func run() throws {
Expand Down

0 comments on commit 1271b61

Please sign in to comment.