Last active
May 31, 2024 13:19
-
-
Save TonioGela/c0baef2873ef4ddbe5e10b88a4a636e0 to your computer and use it in GitHub Desktop.
Just type `in 20m Have a pause and rest` (on MacOs) and in 20 minutes a reminder notification saying "Have a pause and rest" will appear
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//> using scala 3.4.2 | |
//> using platform native | |
//> using packaging.output in | |
//> using nativeMode release-full | |
//> using nativeLto full | |
//> using nativeGc none | |
import scala.concurrent.duration.* | |
@main def main(durationString: String, text: String, others: String*): Unit = | |
runDetach(parseDuration(durationString), (text :: others.toList).mkString(" ") ) | |
def parseDuration(s:String): Duration = | |
def parse(cs:Array[Char], acc: Long, duration: Duration): Duration = cs.headOption.fold(duration){ | |
case d if d.isDigit => parse(cs.tail, acc * 10 + d.asDigit, duration) | |
case 'h' => parse(cs.tail, 0, duration + Duration(acc, HOURS)) | |
case 'm' => parse(cs.tail, 0, duration + Duration(acc, MINUTES)) | |
case 's' => parse(cs.tail, 0, duration + Duration(acc, SECONDS)) | |
case x => throw new Exception(s"Unrecognised token in duration $x") | |
} | |
parse(s.toCharArray(), 0L, Duration.Zero) | |
def runDetach(waitTime: Duration, message: String): Unit = | |
val command = s"""osascript -e "display notification \\"$message\\" with title \\"Reminder\\""""" | |
val processBuilder: ProcessBuilder = | |
new ProcessBuilder("sh", "-c", s"sleep ${waitTime.toSeconds} && $command") | |
processBuilder.inheritIO() | |
val process: Process = processBuilder.start() | |
process.getInputStream().close() | |
process.getOutputStream().close() | |
process.getErrorStream().close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment