Main.hs (8825B)
1 {-# LANGUAGE LambdaCase #-} 2 3 -- | 4 -- Module: Main 5 -- Copyright: (c) 2026 Jared Tobin 6 -- License: MIT 7 -- Maintainer: Jared Tobin <jared@jtobin.io> 8 -- 9 -- A simple pomodoro timer for the terminal. 10 11 module Main where 12 13 import qualified Brick as B 14 import qualified Brick.BChan as BC 15 import qualified Brick.Widgets.Center as C 16 import Control.Concurrent (forkIO, threadDelay) 17 import Control.Monad (forever, replicateM_, void, when) 18 import Control.Monad.IO.Class (liftIO) 19 import Data.List (intersperse) 20 import GHC.Clock (getMonotonicTime) 21 import qualified Graphics.Vty as V 22 import System.Info (os) 23 import qualified System.Process as P 24 25 -- timer ---------------------------------------------------------------------- 26 27 -- | Timer modes. 28 data Mode = 29 Pomodoro 30 | Short 31 | Long 32 deriving Eq 33 34 -- | Duration of a mode, in seconds. 35 duration :: Mode -> Double 36 duration = \case 37 Pomodoro -> 25 * 60 38 Short -> 05 * 60 39 Long -> 10 * 60 40 41 -- | A timer either runs toward a deadline on the monotonic clock, or is 42 -- stopped with some time remaining. 43 data Timer = 44 Running !Double 45 | Stopped !Double 46 47 -- | Application state. 48 data State = State { 49 st_mode :: !Mode 50 , st_timer :: !Timer 51 , st_now :: !Double 52 } 53 54 -- | Seconds remaining on the timer. 55 remaining :: State -> Double 56 remaining (State _ t now) = case t of 57 Running d -> max 0 (d - now) 58 Stopped r -> r 59 60 -- events --------------------------------------------------------------------- 61 62 -- | Clock ticks. 63 data Tick = Tick 64 65 handle :: B.BrickEvent () Tick -> B.EventM () State () 66 handle = \case 67 B.AppEvent Tick -> do 68 now <- liftIO getMonotonicTime 69 st <- B.get 70 case st_timer st of 71 Running d | now >= d -> do 72 B.put st { st_timer = Stopped 0, st_now = now } 73 bell 74 _ -> B.put st { st_now = now } 75 B.VtyEvent (V.EvKey k ms) -> case (k, ms) of 76 (V.KChar 'p', []) -> start Pomodoro 77 (V.KChar 's', []) -> start Short 78 (V.KChar 'l', []) -> start Long 79 (V.KChar ' ', []) -> toggle 80 (V.KChar 'r', []) -> reset 81 (V.KChar 'q', []) -> B.halt 82 (V.KEsc, []) -> B.halt 83 (V.KChar 'c', [V.MCtrl]) -> B.halt 84 _ -> pure () 85 _ -> pure () 86 87 -- | Start a fresh timer in the given mode. 88 start :: Mode -> B.EventM () State () 89 start m = do 90 now <- liftIO getMonotonicTime 91 B.put (State m (Running (now + duration m)) now) 92 93 -- | Start or stop the timer. A timer that has run out starts afresh. 94 toggle :: B.EventM () State () 95 toggle = do 96 now <- liftIO getMonotonicTime 97 B.modify $ \st -> st { 98 st_now = now 99 , st_timer = case st_timer st of 100 Running d -> Stopped (max 0 (d - now)) 101 Stopped r 102 | r > 0 -> Running (now + r) 103 | otherwise -> Running (now + duration (st_mode st)) 104 } 105 106 -- | Stop the timer and restore the full duration of the current mode. 107 reset :: B.EventM () State () 108 reset = B.modify $ \st -> st { st_timer = Stopped (duration (st_mode st)) } 109 110 -- | Ring the terminal bell and, on macOS, play an alert sound thrice. 111 bell :: B.EventM () State () 112 bell = do 113 vty <- B.getVtyHandle 114 liftIO $ do 115 V.ringTerminalBell (V.outputIface vty) 116 when (os == "darwin") . void . forkIO . replicateM_ 3 $ 117 P.readProcessWithExitCode "afplay" [chime] "" 118 119 -- | Alert sound, for macOS. 120 chime :: FilePath 121 chime = "/System/Library/Sounds/Ping.aiff" 122 123 -- drawing -------------------------------------------------------------------- 124 125 draw :: State -> [B.Widget ()] 126 draw st = pure . C.center $ 127 B.raw (sprite tomato) B.<+> B.padLeft (B.Pad 3) (B.vBox [ 128 clock st 129 , B.padTop (B.Pad 1) (tabs (st_mode st)) 130 , B.padTop (B.Pad 1) (help st) 131 ]) 132 133 -- | The time remaining, in large digits. 134 clock :: State -> B.Widget () 135 clock st = B.withAttr attr . B.vBox . fmap B.str $ 136 foldr1 (zipWith (\a b -> a <> " " <> b)) (fmap glyph (mmss secs)) 137 where 138 secs = ceiling (remaining st) :: Int 139 attr = case st_timer st of 140 Running _ -> running_attr 141 Stopped _ -> stopped_attr 142 143 -- | Render seconds as MM:SS. 144 mmss :: Int -> String 145 mmss s = pad (s `quot` 60) <> ":" <> pad (s `rem` 60) where 146 pad n = if n < 10 then '0' : show n else show n 147 148 -- | A large glyph, five rows tall. 149 glyph :: Char -> [String] 150 glyph = fmap (concatMap px) . \case 151 '0' -> ["###", "#.#", "#.#", "#.#", "###"] 152 '1' -> [".#.", "##.", ".#.", ".#.", "###"] 153 '2' -> ["###", "..#", "###", "#..", "###"] 154 '3' -> ["###", "..#", "###", "..#", "###"] 155 '4' -> ["#.#", "#.#", "###", "..#", "..#"] 156 '5' -> ["###", "#..", "###", "..#", "###"] 157 '6' -> ["###", "#..", "###", "#.#", "###"] 158 '7' -> ["###", "..#", "..#", "..#", "..#"] 159 '8' -> ["###", "#.#", "###", "#.#", "###"] 160 '9' -> ["###", "#.#", "###", "..#", "###"] 161 _ -> [".", "#", ".", "#", "."] 162 where 163 px '#' = "██" 164 px _ = " " 165 166 -- | The mode tabs, with the current mode highlighted and each mode's key 167 -- underlined. 168 tabs :: Mode -> B.Widget () 169 tabs cur = B.hBox (fmap tab modes) where 170 modes = [ 171 (Pomodoro, "pomodoro") 172 , (Short, "short break") 173 , (Long, "long break") 174 ] 175 tab (m, label) = 176 let a = if m == cur then current_attr else tab_attr 177 (k, rest) = splitAt 1 label 178 in B.withAttr a $ B.hBox [ 179 B.str " " 180 , B.withAttr (a <> key_attr) (B.str k) 181 , B.str (rest <> " ") 182 ] 183 184 -- | Key hints. 185 help :: State -> B.Widget () 186 help st = B.withAttr help_attr . B.hBox . intersperse (B.str " ") $ 187 [hint "space" toggle_label, hint "r" "reset", hint "q" "quit"] 188 where 189 hint k v = 190 B.withAttr (help_attr <> key_attr) (B.str k) B.<+> B.str (' ' : v) 191 toggle_label = case st_timer st of 192 Running _ -> "stop" 193 Stopped _ -> "start" 194 195 -- | Render rows of pixels, two to a cell, with half blocks. 196 sprite :: [String] -> V.Image 197 sprite = \case 198 (a:b:rest) -> V.horizCat (zipWith cell a b) V.<-> sprite rest 199 _ -> V.emptyImage 200 where 201 cell t b = case (pixel t, pixel b) of 202 (Nothing, Nothing) -> V.char V.defAttr ' ' 203 (Just f, Nothing) -> V.char (V.defAttr `V.withForeColor` f) '▀' 204 (Nothing, Just g) -> V.char (V.defAttr `V.withForeColor` g) '▄' 205 (Just f, Just g) -> V.char (f `B.on` g) '▀' 206 207 -- | Tomato palette. 208 pixel :: Char -> Maybe V.Color 209 pixel = \case 210 'R' -> Just red 211 'r' -> Just (rgb 150 25 25) 212 'H' -> Just (rgb 255 150 130) 213 'G' -> Just (rgb 70 160 50) 214 'g' -> Just (rgb 40 100 30) 215 _ -> Nothing 216 217 red :: V.Color 218 red = rgb 220 50 40 219 220 rgb :: Int -> Int -> Int -> V.Color 221 rgb = V.linearColor 222 223 tomato :: [String] 224 tomato = [ 225 "...........gg..........." 226 , "...........gg..........." 227 , "....GGGRRRRggRRRRGGG...." 228 , "...RRRGGGGGGGGGGGGRRr..." 229 , "..RRGGGGRGGGGGGRGGGGRr.." 230 , ".RRRRRRRRGRGGRGRRRRRRRr." 231 , "RRRRRHHRRRRRRRRRRRRRRRrr" 232 , "RRRRHRRRRRRRRRRRRRRRRRrr" 233 , "RRRHRRRRRRRRRRRRRRRRRRrr" 234 , "RRRHRRRRRRRRRRRRRRRRRRrr" 235 , "RRRRRRRRRRRRRRRRRRRRRRrr" 236 , "RRRRRRRRRRRRRRRRRRRRRrrr" 237 , ".RRRRRRRRRRRRRRRRRRRRrr." 238 , ".RRRRRRRRRRRRRRRRRRRrrr." 239 , "..RRRRRRRRRRRRRRRRRrrr.." 240 , "...RRRRRRRRRRRRRRrrrr..." 241 , ".....rRRRRRRRRrrrrr....." 242 , "........rrrrrrrr........" 243 ] 244 245 -- attributes ----------------------------------------------------------------- 246 247 running_attr, stopped_attr, tab_attr, current_attr, help_attr, key_attr 248 :: B.AttrName 249 running_attr = B.attrName "running" 250 stopped_attr = B.attrName "stopped" 251 tab_attr = B.attrName "tab" 252 current_attr = B.attrName "current" 253 help_attr = B.attrName "help" 254 key_attr = B.attrName "key" 255 256 attrs :: B.AttrMap 257 attrs = B.attrMap V.defAttr [ 258 (running_attr, B.fg red `V.withStyle` V.bold) 259 , (stopped_attr, V.defAttr `V.withStyle` V.dim) 260 , (tab_attr, V.defAttr `V.withStyle` V.dim) 261 , (tab_attr <> key_attr, V.defAttr `V.withStyle` V.dim 262 `V.withStyle` V.underline) 263 , (current_attr, (V.brightWhite `B.on` red) `V.withStyle` V.bold) 264 , (current_attr <> key_attr, V.defAttr `V.withStyle` V.bold 265 `V.withStyle` V.underline) 266 , (help_attr, V.defAttr `V.withStyle` V.dim) 267 , (help_attr <> key_attr, V.defAttr `V.withStyle` V.bold) 268 ] 269 270 -- main ----------------------------------------------------------------------- 271 272 app :: B.App State Tick () 273 app = B.App { 274 B.appDraw = draw 275 , B.appChooseCursor = B.neverShowCursor 276 , B.appHandleEvent = handle 277 , B.appStartEvent = pure () 278 , B.appAttrMap = const attrs 279 } 280 281 main :: IO () 282 main = do 283 chan <- BC.newBChan 1 284 _ <- forkIO . forever $ do 285 BC.writeBChan chan Tick 286 threadDelay 200000 287 now <- getMonotonicTime 288 let st = State Pomodoro (Stopped (duration Pomodoro)) now 289 (_, vty) <- B.customMainWithDefaultVty (Just chan) app st 290 V.shutdown vty