summaryrefslogtreecommitdiffstats
path: root/src/Much/Core.hs
blob: b0f9a5148091c39fc6a11ffffa13067b3033e89d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
{-# LANGUAGE LambdaCase #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
module Much.Core where

import Blessings.String (Blessings(Plain,SGR),pp)
import Control.Concurrent
import Control.Monad
import Data.Aeson
import Data.Functor
import Data.Maybe
import Data.Time
import Much.API
import Much.Action
import Much.Event
import Much.RenderTreeView (renderTreeView)
import Much.Screen
import Much.State
import Much.TreeSearch
import Much.TreeView
import Much.Utils
import Options.Applicative
import Scanner (scan,Scan(..))
import System.Environment
import System.Exit
import System.IO
import System.Posix.Signals
import qualified Blessings.Internal as Blessings
import qualified Data.Map as M
import qualified Data.Tree as Tree
import qualified Data.Tree.Zipper as Z
import qualified Much.Config as Config
import qualified Notmuch
import qualified System.Console.Terminal.Size as Term



emptyState :: State
emptyState = State
    { cursor = Z.fromTree (Tree.Node (TVSearch "<emptyState>") [])
    , xoffset = 0
    , yoffset = 0
    , flashMessage = "Welcome to much; quit with ^C"
    , screenWidth = 0
    , screenHeight = 0
    , headBuffer = []
    , treeBuffer = []
    , now = UTCTime (fromGregorian 1984 5 23) 49062
    , signalHandlers = []
    , query = "tag:inbox AND NOT tag:killed"
    , keymap = displayKey
    , mousemap = displayMouse
    , colorConfig = ColorConfig
        { tagMap = M.fromList
            [ ("killed", SGR [38,5,088])
            , ("star", SGR [38,5,226])
            , ("draft", SGR [38,5,202])
            ]
        , alt = SGR [38,5,182]
        , search = SGR [38,5,162]
        , focus = SGR [38,5,160]
        , quote = SGR [38,5,242]
        , boring = SGR [38,5,240]
        , prefix = SGR [38,5,235]
        , date = SGR [38,5,071]
        , tags = SGR [38,5,036]
        , boringMessage = SGR [38,5,023]
        , unreadMessage = SGR [38,5,117]
        , unreadSearch = SGR [38,5,250]
        }
    , tagSymbols = M.empty
    , apiConfig = Much.API.emptyConfig
    }


importConfig :: Config.Config -> State -> State
importConfig config state = state
  { tagSymbols = fromMaybe (tagSymbols state) (Config.tagSymbols config)
  , query = fromMaybe (query state) (Config.query config)
  , colorConfig =
    let fromColorConfig key1 key2 = case Config.colorConfig config of
                                      Just colorC -> maybe (key1 (colorConfig state)) SGR (key2 colorC)
                                      Nothing -> key1 (colorConfig state)
    in ColorConfig
      { tagMap =
        case tagMap <$> Config.colorConfig config of
          Just tagMap' ->
            M.foldlWithKey
              (\previous k v -> maybe previous (\code -> M.insert k (SGR code) previous) v)
              (tagMap (colorConfig state))
              tagMap'
          Nothing -> tagMap (colorConfig state)
      , alt = fromColorConfig alt alt
      , search = fromColorConfig search search
      , focus = fromColorConfig focus focus
      , quote = fromColorConfig quote quote
      , boring = fromColorConfig boring boring
      , prefix = fromColorConfig prefix prefix
      , date = fromColorConfig date date
      , tags = fromColorConfig tags tags
      , boringMessage = fromColorConfig boringMessage boringMessage
      , unreadSearch = fromColorConfig unreadSearch unreadSearch
      , unreadMessage = fromColorConfig unreadMessage unreadMessage
      }
  }

notmuchSearch :: State -> IO State
notmuchSearch q@State{query} = do
  r_ <- either error id <$> Notmuch.search
                                [ "--offset=0"
                                , "--limit=100"
                                , query
                                ]

  return q { cursor = Z.fromTree $ fromSearchResults query r_ }


mainWithState :: State -> IO ()
mainWithState state@State{query = defaultSearch} = do
    (query, maybeConfigPath) <- execParser (options defaultSearch)
    newState <- case maybeConfigPath of
      Just configPath -> do
        eitherConfig <- eitherDecodeFileStrict configPath
        case eitherConfig of
          Left err -> do
            hPutStrLn stderr err
            exitFailure
          Right config -> return $ importConfig config state
      Nothing -> return state
    withScreen s0 (\_-> notmuchSearch newState { query = query } >>= runState)
  where
    options defaultQuery =
      info (muchArgs <**> helper) (fullDesc <> progDesc "Command-line MUA using notmuch.")
        where
          muchArgs =
            (,)
            <$> strOption (long "query" <> short 'q' <> metavar "QUERY" <> help "Open specific search" <> value defaultQuery <> showDefault)
            <*> optional (strOption (long "config" <> short 'c' <> metavar "PATH" <> help "Import settings from a JSON config"))

    s0 = Screen False NoBuffering (BlockBuffering $ Just 4096)
            [ 1000 -- X & Y on button press and release
            , 1005 -- UTF-8 mouse mode
            , 1047 -- use alternate screen buffer
            ]
            [   25 -- hide cursor
            ]

runState :: State -> IO ()
runState q0 = do

    -- load-env hack
    maybe (return ()) (setEnv "HOME") =<< lookupEnv "OLDHOME"

    (putEvent, getEvent) <- do
        v <- newEmptyMVar
        return (putMVar v, takeMVar v)

    let q1 = q0 { signalHandlers =
                    [ (sigINT, putEvent EShutdown)
                    , (28, winchHandler putEvent)
                    ] }

    installHandlers (signalHandlers q1)

    threadIds <- mapM forkIO
        [ forever $ scan stdin >>= putEvent . EScan
        , Much.API.main (apiConfig q1) putEvent
        ]

    winchHandler putEvent

    run getEvent q1
    mapM_ killThread threadIds


installHandlers :: [(Signal, IO ())] -> IO ()
installHandlers =
    mapM_ (\(s, h) -> installHandler s (Catch h) Nothing)

uninstallHandlers :: [(Signal, IO ())] -> IO ()
uninstallHandlers =
    mapM_ (\(s, _) -> installHandler s Ignore Nothing)


winchHandler :: (Event -> IO ()) -> IO ()
winchHandler putEvent =
    Term.size >>= \case
        Just Term.Window {Term.width = w, Term.height = h} ->
            putEvent $ EResize w h
        Nothing ->
            return ()

run :: IO Event -> State -> IO ()
run getEvent = rec . Right where
    rec = \case
        Right q -> rec =<< do
            t <- getCurrentTime
            let q' = render q { now = t }
            redraw q' >> getEvent >>= processEvent q'
        Left _q -> return ()


processEvent :: State -> Event -> IO (Either State State)
processEvent q = \case
    EFlash t ->
        return $ Right q { flashMessage = t }
    EScan (ScanKey s) ->
        Right <$> keymap q s q
    EScan mouseInfo@ScanMouse{..} ->
        Right <$> mousemap q mouseInfo q
    EShutdown ->
        return $ Left q
    EResize w h ->
        return $ Right q
            { screenWidth = w, screenHeight = h
            , flashMessage = Plain $ "resize " <> show (w,h)
            }
    EStateGet f ->
        forkIO (f q) $> Right q
    ev ->
        return $ Right q
            { flashMessage = SGR [31,1] $ Plain $ "unhandled event: " <> show ev
            }


render :: State -> State
render q@State{..} =
    q { treeBuffer = newTreeBuf
      , headBuffer = newHeadBuf
      }
  where
    newTreeBuf = renderTreeView q (Z.root cursor)
    newHeadBuf =
        [ Plain (show screenWidth) <> "x" <> Plain (show screenHeight)
          <> " " <> Plain (show $ linearPos cursor - yoffset)
          <> " " <> Plain (show $ topOverrun q)
          <> " " <> Plain (show $ botOverrun q)
          <> " " <> flashMessage
          <> " " <> Plain (show (xoffset, yoffset))
        ]

render0 :: State -> [Blessings String]
render0 _q@State{..} = do
    let buffer =
            map (Blessings.take screenWidth . Blessings.drop xoffset) $
            take screenHeight $
            headBuffer ++ drop yoffset treeBuffer
    buffer ++ replicate (screenHeight - length buffer) "~"


redraw :: State -> IO ()
redraw q@State{..} = do
    hPutStr stdout $ map (sub '\t' ' ') $ "\ESC[H" ++ pp (mintercalate "\n" $ map eraseRight $ render0 q)
    hFlush stdout
  where
    sub x x' c = if c == x then x' else c
    eraseRight s =
        if Blessings.length s < screenWidth
            then s <> "\ESC[K"
            else s