Skip to content

Commit

Permalink
Fix bugs with negative times, tidy some code
Browse files Browse the repository at this point in the history
  • Loading branch information
bbrks committed Oct 8, 2018
1 parent 48ceec1 commit 0f72d3d
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 25 deletions.
Binary file removed .DS_Store
Binary file not shown.
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# OS X Statusbar Countdown
# macOS/OS X Statusbar Countdown

**Deprecated:** Use [BitBar](https://getbitbar.com) with [Countdown plugin](https://getbitbar.com/plugins/Time/countdown.1s.py) instead.
A utility to count down to a date from your macOS menubar, written in Swift.

A utility to count down to a date from your OS X menubar/statusbar written in Swift.
The countdown name and date are currently hardcoded. To change, plug in desired values here and recompile:

https://github.com/bbrks/osx-statusbar-countdown/blob/master/StatusbarCountdown/AppDelegate.swift#L17-L18

<img src="http://i.imgur.com/PDQb7VR.png" width="452" alt="OS X Statusbar Countdown" />

Expand All @@ -13,10 +15,5 @@ If you encounter issues regarding unknown developer, Ctrl+Click on the .app and

Alternatively, clone the repo and compile in Xcode :)

## Roadmap

- [x] v0.1 - Countdown displaying in Statusbar
- [ ] v0.2 - Configurable date and title

## License
This project is licensed under the [MIT License](LICENSE).
32 changes: 15 additions & 17 deletions StatusbarCountdown/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,11 @@ import Foundation

@NSApplicationMain
class AppDelegate: NSObject, NSApplicationDelegate {

// TODO: Remove these hardcoded values (into a plist)
// TODO: Provide a GUI config to set plist values
let countToDate = NSDate(timeIntervalSince1970: 1531086400)
let countdownName = "Diss"
let countToDate = NSDate(timeIntervalSince1970: 1597249800) // FIXME: Your countdown date here... - 2020-08-04 16:30:00 UTC
let countdownName = "Countdown Name" // FIXME: Your countdown name here...

var showName = true
var showSeconds = true
Expand All @@ -31,7 +31,7 @@ class AppDelegate: NSObject, NSApplicationDelegate {
statusItem.menu = statusMenu
formatter.minimumIntegerDigits = zeroPad ? 2 : 1

Timer.scheduledTimer(timeInterval: 0.25,
Timer.scheduledTimer(timeInterval: 0.16, // 16ms ~ 60fps
target: self,
selector: #selector(tick),
userInfo: nil,
Expand All @@ -40,13 +40,10 @@ class AppDelegate: NSObject, NSApplicationDelegate {

// Calculates the difference in time from now to the specified date and sets the statusItem title
@objc func tick() {
let diff = Int(countToDate.timeIntervalSinceNow)
let diffSeconds = Int(countToDate.timeIntervalSinceNow)

if (showName) {
statusItem.title = countdownName + ": " + formatTime(diff)
} else {
statusItem.title = formatTime(diff)
}
statusItem.title = (showName) ? countdownName + ": " : ""
statusItem.title! += formatTime(diffSeconds)

}

Expand All @@ -66,13 +63,14 @@ class AppDelegate: NSObject, NSApplicationDelegate {
}

// Display seconds as Days, Hours, Minutes, Seconds.
func formatTime(_ time: Int) -> (String) {
let time = secondsToTime(time)
let days = (time.0 > 0) ? String(time.0) + "d " : ""
let hours = (time.1 > 0 || time.0 > 0) ? formatter.string(from: NSNumber(value: time.1))! + "h " : ""
let minutes = (time.2 > 0 || time.1 > 0 || time.0 > 0) ? formatter.string(from: NSNumber(value: time.2))! + "m" : ""
let seconds = (showSeconds) ? " " + formatter.string(from: NSNumber(value: time.3))! + "s" : ""
return days + hours + minutes + seconds
func formatTime(_ seconds: Int) -> (String) {
let time = secondsToTime(abs(seconds))
let daysStr = (time.0 != 0) ? String(time.0) + "d " : ""
let hoursStr = (time.1 != 0 || time.0 != 0) ? formatter.string(from: NSNumber(value: time.1))! + "h " : ""
let minutesStr = (time.2 != 0 || time.1 != 0 || time.0 != 0) ? formatter.string(from: NSNumber(value: time.2))! + "m" : ""
let secondsStr = (showSeconds) ? " " + formatter.string(from: NSNumber(value: time.3))! + "s" : ""
let suffixStr = (seconds < 0) ? " ago" : "" // TODO: i18n?
return daysStr + hoursStr + minutesStr + secondsStr + suffixStr
}

// MenuItem click event to toggle showSeconds
Expand Down

0 comments on commit 0f72d3d

Please sign in to comment.