Skip to content

Commit

Permalink
Add commands to control the timer
Browse files Browse the repository at this point in the history
  • Loading branch information
galassie committed May 1, 2024
1 parent 7d7aae3 commit a9c3b96
Showing 1 changed file with 78 additions and 18 deletions.
96 changes: 78 additions & 18 deletions Program.fs
Original file line number Diff line number Diff line change
@@ -1,15 +1,49 @@
open System.Threading.Tasks
open System
open System.Threading.Tasks
open FsSpectre
open Spectre.Console

let focusColor = Color.Lime.ToMarkup()
let breakColor = Color.Aqua.ToMarkup()

[<RequireQualifiedAccess>]
type FocusTime =
| ``25Mins``
| ``30Mins``
| ``45Mins``
| ``1Hour``

override this.ToString() : string =
match this with
| FocusTime.``25Mins`` -> "25 minutes"
| FocusTime.``30Mins`` -> "30 minutes"
| FocusTime.``45Mins`` -> "45 minutes"
| FocusTime.``1Hour`` -> "1 hour"

[<RequireQualifiedAccess>]
type BreakTime =
| ``5Mins``
| ``10Mins``
| ``15Mins``
| ``20Mins``

override this.ToString() : string =
match this with
| BreakTime.``5Mins`` -> "5 minutes"
| BreakTime.``10Mins`` -> "10 minutes"
| BreakTime.``15Mins`` -> "15 minutes"
| BreakTime.``20Mins`` -> "20 minutes"

let focusTime =
AnsiConsole.Prompt
<| selectionPrompt {
title $"[{focusColor}]Focus time[/]"
choices [| "25 minutes"; "30 minutes"; "45 minutes"; "1 hour" |]

choices
[| FocusTime.``25Mins``
FocusTime.``30Mins``
FocusTime.``45Mins``
FocusTime.``1Hour`` |]
}

AnsiConsole.MarkupLine($"You have [{focusColor}]{focusTime}[/] of focus time!")
Expand All @@ -18,27 +52,53 @@ let breakTime =
AnsiConsole.Prompt
<| selectionPrompt {
title $"[{breakColor}]Break time[/]"
choices [| "5 minutes"; "10 minutes"; "15 minutes"; "20 minutes" |]

choices
[| BreakTime.``5Mins``
BreakTime.``10Mins``
BreakTime.``15Mins``
BreakTime.``20Mins`` |]
}

AnsiConsole.MarkupLine($"You have [{breakColor}]{breakTime}[/] of break time!")
AnsiConsole.MarkupLine($"You have [{breakColor}]{breakTime}[/] of break time!\n")

let mutable pause: bool = false
let mutable skip: bool = false
let mutable quit: bool = false
let mutable cmd: option<ConsoleKey> = None

while not quit do
let write =
Task.Run(fun () ->
while not quit do
match Console.ReadKey(true).Key with
| ConsoleKey.P -> pause <- true
| ConsoleKey.R -> pause <- false
| ConsoleKey.S -> skip <- true
| ConsoleKey.Q -> quit <- true
| _ -> ())

let progress = AnsiConsole.Progress()
let result =
AnsiConsole
.Progress()
.StartAsync(fun (ctx) ->
task {
let task1 = ctx.AddTask($"[{focusColor}]Focus[/]")

let result =
progress.StartAsync(fun (ctx) ->
task {
let task1 = ctx.AddTask($"[{focusColor}]Focus[/]")
let task2 = ctx.AddTask($"[{breakColor}]Break[/]")
while (not ctx.IsFinished && not quit) do
do! Task.Delay(1000)

while (not ctx.IsFinished) do
do! Task.Delay(250)
task1.Increment(1.5)
task2.Increment(0.5)
let incrementTime = if pause then 0 else 5
task1.Increment(incrementTime)

return 0
})
return 0
})

Task.WaitAll(result)
AnsiConsole.WriteLine("Running...\n")
AnsiConsole.MarkupLine("[grey]Press 'p' to pause[/]")
AnsiConsole.MarkupLine("[grey]Press 'r' to resume[/]")
AnsiConsole.MarkupLine("[grey]Press 's' to skip[/]")
AnsiConsole.MarkupLine("[grey]Press 'q' to quit[/]")
Task.WaitAll(result, write)

AnsiConsole.WriteLine("Finished!")
AnsiConsole.WriteLine($"Finished!")

0 comments on commit a9c3b96

Please sign in to comment.