Explorar el Código

Very stupid clock ticking

master
Peter J. Jones hace 4 años
padre
commit
52d608a064
Se han modificado 6 ficheros con 108 adiciones y 33 borrados
  1. 2
    0
      .gitignore
  2. 4
    1
      clockdown.cabal
  3. 1
    1
      clockdown.hs
  4. 24
    0
      src/Clockdown/UI/Common/Action.hs
  5. 3
    31
      src/Clockdown/UI/Term/Draw.hs
  6. 74
    0
      src/Clockdown/UI/Term/Run.hs

+ 2
- 0
.gitignore Ver fichero

@@ -1,3 +1,5 @@
1 1
 /default.nix
2 2
 /dist/
3 3
 /result
4
+/.cabal-sandbox/
5
+/cabal.sandbox.config

+ 4
- 1
clockdown.cabal Ver fichero

@@ -31,7 +31,9 @@ library
31 31
     Clockdown.Core.Digital.Indicator
32 32
     Clockdown.Core.Properties
33 33
     Clockdown.Core.Window
34
+    Clockdown.UI.Common.Action
34 35
     Clockdown.UI.Term.Draw
36
+    Clockdown.UI.Term.Run
35 37
 
36 38
   default-language: Haskell2010
37 39
   hs-source-dirs: src
@@ -41,7 +43,8 @@ library
41 43
     ghc-options: -Werror
42 44
     ghc-prof-options: -prof -auto-all
43 45
 
44
-  build-depends: base          >= 4.7  && < 5.0
46
+  build-depends: async         >= 2.0  && < 2.1
47
+               , base          >= 4.7  && < 5.0
45 48
                , text          >= 0.11 && < 1.3
46 49
                , time          >= 1.5  && < 1.6
47 50
                , vty           >= 5.2  && < 5.3

+ 1
- 1
clockdown.hs Ver fichero

@@ -13,7 +13,7 @@ the LICENSE file.
13 13
 module Main (main) where
14 14
 
15 15
 --------------------------------------------------------------------------------
16
-import qualified Clockdown.UI.Term.Draw as Clockdown
16
+import qualified Clockdown.UI.Term.Run as Clockdown
17 17
 
18 18
 --------------------------------------------------------------------------------
19 19
 main :: IO ()

+ 24
- 0
src/Clockdown/UI/Common/Action.hs Ver fichero

@@ -0,0 +1,24 @@
1
+{-
2
+
3
+This file is part of the package clockdown. It is subject to the
4
+license terms in the LICENSE file found in the top-level directory of
5
+this distribution and at git://pmade.com/clockdown/LICENSE. No part of
6
+the clockdown package, including this file, may be copied, modified,
7
+propagated, or distributed except according to the terms contained in
8
+the LICENSE file.
9
+
10
+-}
11
+
12
+--------------------------------------------------------------------------------
13
+module Clockdown.UI.Common.Action
14
+       ( Action (..)
15
+       ) where
16
+
17
+--------------------------------------------------------------------------------
18
+-- Library imports:
19
+import Data.Time
20
+
21
+--------------------------------------------------------------------------------
22
+-- | Actions which can be triggered by the system or the user.
23
+data Action = Tick UTCTime
24
+              -- ^ Update the clock.

+ 3
- 31
src/Clockdown/UI/Term/Draw.hs Ver fichero

@@ -1,5 +1,3 @@
1
-{-# LANGUAGE OverloadedStrings #-}
2
-
3 1
 {-
4 2
 
5 3
 This file is part of the package clockdown. It is subject to the
@@ -14,12 +12,12 @@ the LICENSE file.
14 12
 --------------------------------------------------------------------------------
15 13
 -- | Functions for creating Vty images to display a clock/timer.
16 14
 module Clockdown.UI.Term.Draw
17
-       ( run
15
+       ( drawDisplay
16
+       , centerImage
18 17
        ) where
19 18
 
20 19
 --------------------------------------------------------------------------------
21 20
 -- Library imports:
22
-import Data.Time
23 21
 import Graphics.Vty
24 22
 import Graphics.Vty.Prelude
25 23
 
@@ -27,8 +25,7 @@ import Graphics.Vty.Prelude
27 25
 -- Local imports:
28 26
 import Clockdown.Core.Digital.Display
29 27
 import Clockdown.Core.Digital.Indicator
30
-import Clockdown.Core.Properties
31
-import Clockdown.Core.Window
28
+-- import Clockdown.Core.Properties
32 29
 
33 30
 --------------------------------------------------------------------------------
34 31
 -- | Draw a single indicator into a Vty image.
@@ -96,29 +93,3 @@ centerImage display image = translate x y image
96 93
   where
97 94
     x = (regionWidth  display `div` 2) - (imageWidth  image `div` 2)
98 95
     y = (regionHeight display `div` 2) - (imageHeight image `div` 2)
99
-
100
---------------------------------------------------------------------------------
101
-run :: IO ()
102
-run = do
103
-  cfg <- standardIOConfig
104
-  vty <- mkVty cfg
105
-  tz  <- getCurrentTimeZone
106
-  go vty $ makeClock (Properties "foo") tz
107
-  shutdown vty
108
-
109
-  where
110
-    go vty clock = do
111
-      now <- getCurrentTime
112
-      region <- displayBounds (outputIface vty)
113
-
114
-      let display = windowDigitalDisplay clock now
115
-      update vty $ picForImage (centerImage region $ drawDisplay display)
116
-
117
-      e <- nextEvent vty
118
-      case e of
119
-        EvKey KEsc []        -> return ()
120
-        EvKey (KChar '+') [] -> go vty (windowSucc clock)
121
-        EvKey (KChar '=') [] -> go vty (windowSucc clock)
122
-        EvKey (KChar '-') [] -> go vty (windowPred clock)
123
-        _                    -> go vty clock

+ 74
- 0
src/Clockdown/UI/Term/Run.hs Ver fichero

@@ -0,0 +1,74 @@
1
+{-# LANGUAGE OverloadedStrings #-}
2
+
3
+{-
4
+
5
+This file is part of the package clockdown. It is subject to the
6
+license terms in the LICENSE file found in the top-level directory of
7
+this distribution and at git://pmade.com/clockdown/LICENSE. No part of
8
+the clockdown package, including this file, may be copied, modified,
9
+propagated, or distributed except according to the terms contained in
10
+the LICENSE file.
11
+
12
+-}
13
+
14
+--------------------------------------------------------------------------------
15
+module Clockdown.UI.Term.Run
16
+       ( run
17
+       ) where
18
+
19
+--------------------------------------------------------------------------------
20
+-- Library imports:
21
+import Control.Concurrent
22
+import Control.Concurrent.Async
23
+import Control.Monad
24
+import Data.Time
25
+import Graphics.Vty
26
+
27
+--------------------------------------------------------------------------------
28
+-- Local imports:
29
+import Clockdown.Core.Properties
30
+import Clockdown.Core.Window
31
+import Clockdown.UI.Common.Action
32
+import Clockdown.UI.Term.Draw
33
+
34
+--------------------------------------------------------------------------------
35
+tickThread :: Chan Action -> IO ()
36
+tickThread channel = forever $ do
37
+  now <- getCurrentTime
38
+  writeChan channel (Tick now)
39
+  threadDelay 1000000
40
+
41
+--------------------------------------------------------------------------------
42
+drawThread :: Vty -> Chan Action -> IO ()
43
+drawThread vty channel = forever $ do
44
+  tz  <- getCurrentTimeZone
45
+  action <- readChan channel
46
+
47
+  case action of
48
+    Tick t -> do
49
+      let clock   = makeClock (Properties "foo") tz
50
+          display = windowDigitalDisplay clock t
51
+      region <- displayBounds (outputIface vty)
52
+      update vty $ picForImage (centerImage region $ drawDisplay display)
53
+
54
+--------------------------------------------------------------------------------
55
+-- | Temporary function for testing the drawing functions.
56
+run :: IO ()
57
+run = do
58
+  cfg <- standardIOConfig
59
+  vty <- mkVty cfg
60
+  channel <- newChan
61
+  ticker <- async (tickThread channel)
62
+
63
+  withAsync (drawThread vty channel) $ \_ -> do
64
+    go vty
65
+    shutdown vty
66
+    cancel ticker
67
+
68
+  where
69
+    go vty = do
70
+      e <- nextEvent vty
71
+
72
+      case e of
73
+        EvKey KEsc []        -> return ()
74
+        _                    -> go vty

Loading…
Cancelar
Guardar