-
-
Notifications
You must be signed in to change notification settings - Fork 51
Expand file tree
/
Copy pathTypes.hs
More file actions
857 lines (772 loc) · 29.6 KB
/
Copy pathTypes.hs
File metadata and controls
857 lines (772 loc) · 29.6 KB
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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
{-# OPTIONS_GHC -fno-warn-orphans #-}
module Termonad.Types where
import Termonad.Prelude
import Control.Lens (ifoldMap, imap)
import Data.FocusList (FocusList, emptyFL, getFocusItemFL, lengthFL)
import Data.Foldable (toList)
import Data.Map.Strict (Map)
import qualified Data.Map.Strict as Map
import Data.Set (Set)
import Data.Unique (Unique, hashUnique, newUnique)
import Data.Yaml
( FromJSON(parseJSON)
, ToJSON(toJSON)
, Value(String)
, withText
)
import GI.Gdk
( pattern KEY_0
, pattern KEY_1
, pattern KEY_2
, pattern KEY_3
, pattern KEY_4
, pattern KEY_5
, pattern KEY_6
, pattern KEY_7
, pattern KEY_8
, pattern KEY_9
, ModifierType(..)
)
import GI.Gtk
( Application
, ApplicationWindow
, IsWidget
, Label
, Notebook
, ScrolledWindow
, Widget
, notebookGetCurrentPage
, notebookGetNthPage
, notebookGetNPages
, notebookSetCurrentPage
)
import GI.Pango (FontDescription, fontDescriptionGetSize, fontDescriptionGetSizeIsAbsolute, pattern SCALE, fontDescriptionGetFamily, fontDescriptionNew, fontDescriptionSetFamily, fontDescriptionSetSize, fontDescriptionSetAbsoluteSize)
import GI.Vte (Terminal, CursorBlinkMode(..))
import Termonad.Gtk (widgetEq)
import Termonad.IdMap (IdMap, IdMapKey, singletonIdMap, lookupIdMap)
import Text.Pretty.Simple (pPrint)
-- | A wrapper around a VTE 'Terminal'. This also stores the process ID of the
-- process running on this terminal, as well as a 'Unique' that can be used for
-- comparing terminals.
data TMTerm = TMTerm
{ term :: !Terminal
-- ^ The actual 'Terminal'.
, pid :: !Int
-- ^ The process ID of the process running in 'term'.
, unique :: !Unique
-- ^ A 'Unique' for comparing different 'TMTerm' for uniqueness.
}
instance Show TMTerm where
showsPrec :: Int -> TMTerm -> ShowS
showsPrec d TMTerm{..} =
showParen (d > 10) $
showString "TMTerm {" .
showString "term = " .
showString "(GI.GTK.Terminal)" .
showString ", " .
showString "pid = " .
showsPrec (d + 1) pid .
showString ", " .
showString "unique = " .
showsPrec (d + 1) (hashUnique unique) .
showString "}"
-- | A container that holds everything in a given terminal window. The 'term'
-- in the 'TMTerm' is inside the 'tmNotebookTabTermContainer' 'ScrolledWindow'.
-- The notebook tab 'Label' is also available.
data TMNotebookTab = TMNotebookTab
{ tmNotebookTabTermContainer :: !ScrolledWindow
-- ^ The 'ScrolledWindow' holding the VTE 'Terminal'.
, tmNotebookTabTerm :: !TMTerm
-- ^ The 'Terminal' insidie the 'ScrolledWindow'.
, tmNotebookTabLabel :: !Label
-- ^ The 'Label' holding the title of the 'Terminal' in the 'Notebook' tab.
}
instance Show TMNotebookTab where
showsPrec :: Int -> TMNotebookTab -> ShowS
showsPrec d TMNotebookTab{..} =
showParen (d > 10) $
showString "TMNotebookTab {" .
showString "tmNotebookTabTermContainer = " .
showString "(GI.GTK.ScrolledWindow)" .
showString ", " .
showString "tmNotebookTabTerm = " .
showsPrec (d + 1) tmNotebookTabTerm .
showString ", " .
showString "tmNotebookTabLabel = " .
showString "(GI.GTK.Label)" .
showString "}"
-- | This holds the GTK 'Notebook' containing multiple tabs of 'Terminal's. We
-- keep a separate list of terminals in 'tmNotebookTabs'.
data TMNotebook = TMNotebook
{ tmNotebook :: !Notebook
-- ^ This is the GTK 'Notebook' that holds multiple tabs of 'Terminal's.
, tmNotebookTabs :: !(FocusList TMNotebookTab)
-- ^ A 'FocusList' containing references to each individual 'TMNotebookTab'.
}
instance Show TMNotebook where
showsPrec :: Int -> TMNotebook -> ShowS
showsPrec d TMNotebook{..} =
showParen (d > 10) $
showString "TMNotebook {" .
showString "tmNotebook = " .
showString "(GI.GTK.Notebook)" .
showString ", " .
showString "tmNotebookTabs = " .
showsPrec (d + 1) tmNotebookTabs .
showString "}"
getNotebookFromTMState :: TMState -> TMWindowId -> IO Notebook
getNotebookFromTMState mvarTMState tmWinId = do
tmNote <- getTMNotebookFromTMState mvarTMState tmWinId
pure $ tmNotebook tmNote
getNotebookFromTMState' :: TMState' -> TMWindowId -> IO Notebook
getNotebookFromTMState' tmState tmWinId = do
tmNote <- getTMNotebookFromTMState' tmState tmWinId
pure $ tmNotebook tmNote
getTMNotebookFromTMState :: TMState -> TMWindowId -> IO TMNotebook
getTMNotebookFromTMState mvarTMState tmWinId = do
tmWin <- getTMWindowFromTMState mvarTMState tmWinId
pure $ tmWindowNotebook tmWin
getTMNotebookFromTMState' :: TMState' -> TMWindowId -> IO TMNotebook
getTMNotebookFromTMState' tmState tmWinId = do
tmWin <- getTMWindowFromTMState' tmState tmWinId
pure $ tmWindowNotebook tmWin
data TMWindow = TMWindow
{ tmWindowAppWin :: !ApplicationWindow
, tmWindowNotebook :: !TMNotebook
}
instance Show TMWindow where
showsPrec :: Int -> TMWindow -> ShowS
showsPrec d TMWindow{..} =
showParen (d > 10) $
showString "TMWindow {" .
showString "tmWindowAppWin = " .
showString "(GI.GTK.ApplicationWindow)" .
showString ", " .
showString "tmWindowNotebook = " .
showsPrec (d + 1) tmWindowNotebook .
showString "}"
type TMWindowId = IdMapKey
-- | Get a given 'TMWindow' from a set of 'TMWindow's given a 'TMWindowId'.
--
-- This throws an error if the 'TMWindowId' can't be found within the 'IdMap'.
getTMWindowFromWins :: IdMap TMWindow -> TMWindowId -> IO TMWindow
getTMWindowFromWins tmWins tmWinId =
case lookupIdMap tmWinId tmWins of
Nothing -> error $ "getTMWindowFromWins: trying to get id " <> show tmWinId <> " from wins, but doesn't exist: " <> show tmWins
Just tmWin -> pure tmWin
-- | Get a given 'TMWindow' froma a 'TMState' given a 'TMWindowId'.
--
-- This throws an error if the 'TMWindowId' can't be found within the 'TMState'.
getTMWindowFromTMState :: TMState -> TMWindowId -> IO TMWindow
getTMWindowFromTMState mvarTMState tmWinId = do
TMState{tmStateWindows} <- readMVar mvarTMState
getTMWindowFromWins tmStateWindows tmWinId
getTMWindowFromTMState' :: TMState' -> TMWindowId -> IO TMWindow
getTMWindowFromTMState' tmState tmWinId =
getTMWindowFromWins (tmStateWindows tmState) tmWinId
data TMState' = TMState
{ tmStateApp :: !Application
, tmStateConfig :: !TMConfig
, tmStateFontDesc :: !FontDescription
, tmStateWindows :: !(IdMap TMWindow)
}
instance Show TMState' where
showsPrec :: Int -> TMState' -> ShowS
showsPrec d TMState{..} =
showParen (d > 10) $
showString "TMState {" .
showString "tmStateApp = " .
showString "(GI.GTK.Application)" .
showString ", " .
showString "tmStateConfig = " .
showsPrec (d + 1) tmStateConfig .
showString ", " .
showString "tmStateFontDesc = " .
showString "(GI.Pango.FontDescription)" .
showString ", " .
showString "tmStateWindows = " .
showsPrec (d + 1) tmStateWindows .
showString "}"
type TMState = MVar TMState'
instance Eq TMTerm where
(==) :: TMTerm -> TMTerm -> Bool
(==) = (==) `on` (unique :: TMTerm -> Unique)
instance Eq TMNotebookTab where
(==) :: TMNotebookTab -> TMNotebookTab -> Bool
(==) = (==) `on` tmNotebookTabTerm
createTMTerm :: Terminal -> Int -> Unique -> TMTerm
createTMTerm trm pd unq =
TMTerm
{ term = trm
, pid = pd
, unique = unq
}
newTMTerm :: Terminal -> Int -> IO TMTerm
newTMTerm trm pd = createTMTerm trm pd <$> newUnique
getFocusedTermFromState :: TMState -> TMWindowId -> IO (Maybe Terminal)
getFocusedTermFromState mvarTMState tmWinId =
withMVar mvarTMState go
where
go :: TMState' -> IO (Maybe Terminal)
go tmState = do
tmNote <- getTMNotebookFromTMState' tmState tmWinId
let maybeNotebookTab = getFocusItemFL $ tmNotebookTabs tmNote
pure $ fmap (term . tmNotebookTabTerm) maybeNotebookTab
createTMNotebookTab :: Label -> ScrolledWindow -> TMTerm -> TMNotebookTab
createTMNotebookTab tabLabel scrollWin trm =
TMNotebookTab
{ tmNotebookTabTermContainer = scrollWin
, tmNotebookTabTerm = trm
, tmNotebookTabLabel = tabLabel
}
createTMNotebook :: Notebook -> FocusList TMNotebookTab -> TMNotebook
createTMNotebook note tabs =
TMNotebook
{ tmNotebook = note
, tmNotebookTabs = tabs
}
createEmptyTMNotebook :: Notebook -> TMNotebook
createEmptyTMNotebook notebook = createTMNotebook notebook emptyFL
notebookToList :: Notebook -> IO [Widget]
notebookToList notebook =
unfoldHelper 0 []
where unfoldHelper :: Int32 -> [Widget] -> IO [Widget]
unfoldHelper index32 acc = do
notePage <- notebookGetNthPage notebook index32
case notePage of
Nothing -> pure acc
Just notePage' -> unfoldHelper (index32 + 1) (acc ++ [notePage'])
createTMWindow :: ApplicationWindow -> TMNotebook -> TMWindow
createTMWindow appwin notebook =
TMWindow
{ tmWindowAppWin = appwin
, tmWindowNotebook = notebook
}
newEmptyTMState :: TMConfig -> Application -> ApplicationWindow -> Notebook -> FontDescription -> IO (TMState, TMWindowId)
newEmptyTMState tmConfig app appWin note fontDesc = do
let tmnote = createEmptyTMNotebook note
tmwin = createTMWindow appWin tmnote
(tmwinId, tmwins) = singletonIdMap tmwin
tmState <-
newMVar $
TMState
{ tmStateApp = app
, tmStateConfig = tmConfig
, tmStateFontDesc = fontDesc
, tmStateWindows = tmwins
}
pure (tmState, tmwinId)
------------
-- Config --
------------
-- | The font size for the Termonad terminal. There are two ways to set the
-- fontsize, corresponding to the two different ways to set the font size in
-- the Pango font rendering library.
--
-- If you're not sure which to use, try 'FontSizePoints' first and see how it
-- looks. It should generally correspond to font sizes you are used to from
-- other applications.
data FontSize
= FontSizePoints Int
-- ^ This sets the font size based on \"points\". The conversion between a
-- point and an actual size depends on the system configuration and the
-- output device. The function 'GI.Pango.fontDescriptionSetSize' is used
-- to set the font size. See the documentation for that function for more
-- info.
| FontSizeUnits Double
-- ^ This sets the font size based on \"device units\". In general, this
-- can be thought of as one pixel. The function
-- 'GI.Pango.fontDescriptionSetAbsoluteSize' is used to set the font size.
-- See the documentation for that function for more info.
deriving (Eq, FromJSON, Generic, Show, ToJSON)
-- | The default 'FontSize' used if not specified.
--
-- >>> defaultFontSize
-- FontSizePoints 12
defaultFontSize :: FontSize
defaultFontSize = FontSizePoints 12
-- | Modify a 'FontSize' by adding some value.
--
-- >>> modFontSize 1 (FontSizePoints 13)
-- FontSizePoints 14
-- >>> modFontSize 1 (FontSizeUnits 9.0)
-- FontSizeUnits 10.0
--
-- You can reduce the font size by passing a negative value.
--
-- >>> modFontSize (-2) (FontSizePoints 13)
-- FontSizePoints 11
--
-- If you try to create a font size less than 1, then the old font size will be
-- used.
--
-- >>> modFontSize (-10) (FontSizePoints 5)
-- FontSizePoints 5
-- >>> modFontSize (-1) (FontSizeUnits 1.0)
-- FontSizeUnits 1.0
modFontSize :: Int -> FontSize -> FontSize
modFontSize i (FontSizePoints oldPoints) =
let newPoints = oldPoints + i
in FontSizePoints $ if newPoints < 1 then oldPoints else newPoints
modFontSize i (FontSizeUnits oldUnits) =
let newUnits = oldUnits + fromIntegral i
in FontSizeUnits $ if newUnits < 1 then oldUnits else newUnits
fontSizeFromFontDescription :: FontDescription -> IO FontSize
fontSizeFromFontDescription fontDesc = do
currSize <- fontDescriptionGetSize fontDesc
currAbsolute <- fontDescriptionGetSizeIsAbsolute fontDesc
pure $
if currAbsolute
then FontSizeUnits $ fromIntegral currSize / fromIntegral SCALE
else
let fontRatio :: Double = fromIntegral currSize / fromIntegral SCALE
in FontSizePoints $ round fontRatio
-- | Create a 'FontDescription' from a 'FontSize' and font family.
createFontDesc
:: FontSize
-> Text
-- ^ font family
-> IO FontDescription
createFontDesc fontSz fontFam = do
fontDesc <- fontDescriptionNew
fontDescriptionSetFamily fontDesc fontFam
setFontDescSize fontDesc fontSz
pure fontDesc
-- | Set the size of a 'FontDescription' from a 'FontSize'.
setFontDescSize :: FontDescription -> FontSize -> IO ()
setFontDescSize fontDesc (FontSizePoints points) =
fontDescriptionSetSize fontDesc $ fromIntegral (points * fromIntegral SCALE)
setFontDescSize fontDesc (FontSizeUnits units) =
fontDescriptionSetAbsoluteSize fontDesc $ units * fromIntegral SCALE
-- | Create a 'FontDescription' from the 'fontSize' and 'fontFamily' inside a 'TMConfig'.
createFontDescFromConfig :: TMConfig -> IO FontDescription
createFontDescFromConfig tmConfig = do
let fontConf = fontConfig (options tmConfig)
createFontDesc (fontSize fontConf) (fontFamily fontConf)
-- | Settings for the font to be used in Termonad.
data FontConfig = FontConfig
{ fontFamily :: !Text
-- ^ The font family to use. Example: @"DejaVu Sans Mono"@ or @"Source Code Pro"@
, fontSize :: !FontSize
-- ^ The font size.
} deriving (Eq, FromJSON, Generic, Show, ToJSON)
-- | The default 'FontConfig' to use if not specified.
--
-- >>> defaultFontConfig == FontConfig {fontFamily = "Monospace", fontSize = defaultFontSize}
-- True
defaultFontConfig :: FontConfig
defaultFontConfig =
FontConfig
{ fontFamily = "Monospace"
, fontSize = defaultFontSize
}
fontConfigFromFontDescription :: FontDescription -> IO (Maybe FontConfig)
fontConfigFromFontDescription fontDescription = do
fontSize <- fontSizeFromFontDescription fontDescription
maybeFontFamily <- fontDescriptionGetFamily fontDescription
return $ (`FontConfig` fontSize) <$> maybeFontFamily
-- | This data type represents an option that can either be 'Set' or 'Unset'.
--
-- This data type is used in situations where leaving an option unset results
-- in a special state that is not representable by setting any specific value.
--
-- Examples of this include the 'cursorFgColour' and 'cursorBgColour' options
-- supplied by the 'ColourConfig' @ConfigExtension@. By default,
-- 'cursorFgColour' and 'cursorBgColour' are both 'Unset'. However, when
-- 'cursorBgColour' is 'Set', 'cursorFgColour' defaults to the color of the text
-- underneath. There is no way to represent this by explicitly setting
-- 'cursorFgColour'.
data Option a = Unset | Set !a
deriving (Show, Read, Eq, Ord, Functor, Foldable)
-- | Run a function over the value contained in an 'Option'. Return 'mempty'
-- when 'Option' is 'Unset'.
--
-- >>> whenSet (Set [1,2,3]) (++ [4,5,6]) :: [Int]
-- [1,2,3,4,5,6]
-- >>> whenSet Unset (++ [4,5,6]) :: [Int]
-- []
whenSet :: Monoid m => Option a -> (a -> m) -> m
whenSet = \case
Unset -> const mempty
Set x -> \f -> f x
-- | Whether or not to show the scroll bar in a terminal.
data ShowScrollbar
= ShowScrollbarNever -- ^ Never show the scroll bar, even if there are too
-- many lines on the terminal to show all at once. You
-- should still be able to scroll with the mouse wheel.
| ShowScrollbarAlways -- ^ Always show the scrollbar, even if it is not
-- needed.
| ShowScrollbarIfNeeded -- ^ Only show the scrollbar if there are too many
-- lines on the terminal to show all at once.
deriving (Enum, Eq, Generic, FromJSON, Show, ToJSON)
showScrollbarToString :: ShowScrollbar -> Text
showScrollbarToString = \case
ShowScrollbarNever -> "never"
ShowScrollbarAlways -> "always"
ShowScrollbarIfNeeded -> "if-needed"
showScrollbarFromString :: Text -> Maybe ShowScrollbar
showScrollbarFromString = \case
"never" -> Just ShowScrollbarNever
"always" -> Just ShowScrollbarAlways
"if-needed" -> Just ShowScrollbarIfNeeded
_ -> Nothing
-- | Whether or not to show the tab bar for switching tabs.
data ShowTabBar
= ShowTabBarNever -- ^ Never show the tab bar, even if there are multiple tabs
-- open. This may be confusing if you plan on using multiple tabs.
| ShowTabBarAlways -- ^ Always show the tab bar, even if you only have one tab open.
| ShowTabBarIfNeeded -- ^ Only show the tab bar if you have multiple tabs open.
deriving (Enum, Eq, Generic, FromJSON, Show, ToJSON)
showTabBarToString :: ShowTabBar -> Text
showTabBarToString = \case
ShowTabBarNever -> "never"
ShowTabBarAlways -> "always"
ShowTabBarIfNeeded -> "if-needed"
showTabBarFromString :: Text -> Maybe ShowTabBar
showTabBarFromString = \case
"never" -> Just ShowTabBarNever
"always" -> Just ShowTabBarAlways
"if-needed" -> Just ShowTabBarIfNeeded
_ -> Nothing
-- | Configuration options for Termonad.
--
-- See 'defaultConfigOptions' for the default values.
data ConfigOptions = ConfigOptions
{ fontConfig :: !FontConfig
-- ^ Specific options for fonts.
, showScrollbar :: !ShowScrollbar
-- ^ When to show the scroll bar.
, scrollbackLen :: !Integer
-- ^ The number of lines to keep in the scroll back history for each terminal.
, confirmExit :: !Bool
-- ^ Whether or not to ask you for confirmation when closing individual
-- terminals or Termonad itself. It is generally safer to keep this as
-- 'True'.
, wordCharExceptions :: !Text
-- ^ When double-clicking on text in the terminal with the mouse, Termonad
-- will use this value to determine what to highlight. The individual
-- characters in this list will be counted as part of a word.
--
-- For instance if 'wordCharExceptions' is @""@, then when you double-click
-- on the text @http://@, only the @http@ portion will be highlighted. If
-- 'wordCharExceptions' is @":"@, then the @http:@ portion will be
-- highlighted.
, showMenu :: !Bool
-- ^ Whether or not to show the @File@ @Edit@ etc menu.
, showTabBar :: !ShowTabBar
-- ^ When to show the tab bar.
, cursorBlinkMode :: !CursorBlinkMode
-- ^ How to handle cursor blink.
, boldIsBright :: !Bool
-- ^ This option controls whether or not to force bold text to use colors
-- from the 'Termonad.Config.Colour.ExtendedPalatte'.
--
-- If 'True', then colored bold text will /always/ use colors from the
-- 'Termonad.Config.Colour.ExtendedPalatte'. There will be no way to print
-- bold text colored with the 'Termonad.Config.Colour.BasicPalatte'.
--
-- This often isn't a big problem, since many TUI applications use
-- bold in combination with colors from the 'Termonad.Config.Colour.ExtendedPalatte'.
-- Also, the VTE default blue color can be difficult to read with a dark
-- background, and enabling this can work around the problem.
-- See <https://github.com/cdepillabout/termonad/issues/177> for more information.
--
-- If 'False', then bold can be applied separately to colors from both the
-- 'Termonad.Config.Colour.BasicPalatte' and
-- 'Termonad.Config.Colour.ExtendedPalatte'.
, enableSixel :: !Bool
-- ^ Enable SIXEL to draw graphics in a terminal.
--
-- In order for this option to do anything, you need to be using a version
-- of VTE >= 0.63, and compile VTE with SIXEL support.
--
-- Note that even if you do the above, there may still be some problems
-- with SIXEL support in VTE. Follow
-- <https://gitlab.gnome.org/GNOME/vte/-/issues/253> for more information.
, allowBold :: !Bool
-- ^ Allow terminal to use bold text.
--
-- You may want to disable this, for instance, if you use a font that
-- doesn't look good when bold.
} deriving (Eq, Generic, FromJSON, Show, ToJSON)
cursorBlinkModeToString :: CursorBlinkMode -> Text
cursorBlinkModeToString = \case
CursorBlinkModeSystem -> "system"
CursorBlinkModeOn -> "on"
CursorBlinkModeOff -> "off"
AnotherCursorBlinkMode _ -> "other"
cursorBlinkModeFromString :: Text -> Maybe CursorBlinkMode
cursorBlinkModeFromString = \case
"system" -> Just CursorBlinkModeSystem
"on" -> Just CursorBlinkModeOn
"off" -> Just CursorBlinkModeOff
_ -> Nothing
instance FromJSON CursorBlinkMode where
parseJSON = withText "CursorBlinkMode" $ \c -> do
case (c :: Text) of
"CursorBlinkModeSystem" -> pure CursorBlinkModeSystem
"CursorBlinkModeOn" -> pure CursorBlinkModeOn
"CursorBlinkModeOff" -> pure CursorBlinkModeOff
_ -> fail "Wrong value for CursorBlinkMode"
instance ToJSON CursorBlinkMode where
toJSON CursorBlinkModeSystem = String "CursorBlinkModeSystem"
toJSON CursorBlinkModeOn = String "CursorBlinkModeOn"
toJSON CursorBlinkModeOff = String "CursorBlinkModeOff"
-- Not supposed to happened fall back to system
toJSON (AnotherCursorBlinkMode _) = String "CursorBlinkModeSystem"
-- | The default 'ConfigOptions'.
--
-- >>> :{
-- let defConfOpt =
-- ConfigOptions
-- { fontConfig = defaultFontConfig
-- , showScrollbar = ShowScrollbarIfNeeded
-- , scrollbackLen = 10000
-- , confirmExit = True
-- , wordCharExceptions = "-#%&+,./=?@\\_~\183:"
-- , showMenu = True
-- , showTabBar = ShowTabBarIfNeeded
-- , cursorBlinkMode = CursorBlinkModeOn
-- , boldIsBright = False
-- , enableSixel = False
-- , allowBold = True
-- }
-- in defaultConfigOptions == defConfOpt
-- :}
-- True
defaultConfigOptions :: ConfigOptions
defaultConfigOptions =
ConfigOptions
{ fontConfig = defaultFontConfig
, showScrollbar = ShowScrollbarIfNeeded
, scrollbackLen = 10000
, confirmExit = True
, wordCharExceptions = "-#%&+,./=?@\\_~\183:"
, showMenu = True
, showTabBar = ShowTabBarIfNeeded
, cursorBlinkMode = CursorBlinkModeOn
, boldIsBright = False
, enableSixel = False
, allowBold = True
}
-- | The Termonad 'ConfigOptions' along with the 'ConfigHooks'.
data TMConfig = TMConfig
{ options :: !ConfigOptions
, hooks :: !ConfigHooks
, keys :: Map Key (TMState -> TMWindowId -> IO Bool)
}
instance Show TMConfig where
show cfg = (show . options) cfg <> (show . hooks) cfg
-- | The default 'TMConfig'.
--
-- 'options' is 'defaultConfigOptions' and 'hooks' is 'defaultConfigHooks'.
defaultTMConfig :: TMConfig
defaultTMConfig =
TMConfig
{ options = defaultConfigOptions
, hooks = defaultConfigHooks
, keys = keyMap
}
data Key = Key
{ keyVal :: !Word32
, keyMods :: !(Set ModifierType)
} deriving (Eq, Ord, Show)
toKey :: Word32 -> Set ModifierType -> Key
toKey = Key
keyMap :: Map Key (TMState -> TMWindowId -> IO Bool)
keyMap =
let numKeys :: [Word32]
numKeys =
[ KEY_1
, KEY_2
, KEY_3
, KEY_4
, KEY_5
, KEY_6
, KEY_7
, KEY_8
, KEY_9
, KEY_0
]
altNumKeys :: [(Key, TMState -> TMWindowId -> IO Bool)]
altNumKeys =
imap
(\i k ->
(toKey k [ModifierTypeMod1Mask], stopProp (altNumSwitchTerm i))
)
numKeys
in
Map.fromList altNumKeys
stopProp :: (TMState -> TMWindowId -> IO a) -> TMState -> TMWindowId -> IO Bool
stopProp callback terState tmWinId = callback terState tmWinId $> True
focusTerm :: Int -> TMState -> TMWindowId -> IO ()
focusTerm i mvarTMState tmWinId = do
note <- getNotebookFromTMState mvarTMState tmWinId
notebookSetCurrentPage note (fromIntegral i)
altNumSwitchTerm :: Int -> TMState -> TMWindowId -> IO ()
altNumSwitchTerm = focusTerm
---------------------
-- ConfigHooks --
---------------------
-- | Hooks into certain termonad operations and VTE events. Used to modify
-- termonad's behaviour in order to implement new functionality. Fields should
-- have sane @Semigroup@ and @Monoid@ instances so that config extensions can
-- be combined uniformly and new hooks can be added without incident.
newtype ConfigHooks = ConfigHooks {
-- | Produce an IO action to run on creation of new @Terminal@, given @TMState@
-- and the @Terminal@ in question.
createTermHook :: TMState -> Terminal -> IO ()
}
instance Show ConfigHooks where
showsPrec :: Int -> ConfigHooks -> ShowS
showsPrec _ _ =
showString "ConfigHooks {" .
showString "createTermHook = <function>" .
showString "}"
-- | Default values for the 'ConfigHooks'.
--
-- - The default function for 'createTermHook' is 'defaultCreateTermHook'.
defaultConfigHooks :: ConfigHooks
defaultConfigHooks =
ConfigHooks
{ createTermHook = defaultCreateTermHook
}
-- | Default value for 'createTermHook'. Does nothing.
defaultCreateTermHook :: TMState -> Terminal -> IO ()
defaultCreateTermHook _ _ = pure ()
----------------
-- Invariants --
----------------
data FocusNotSameErr
= FocusListFocusExistsButNoNotebookTabWidget
| NotebookTabWidgetDiffersFromFocusListFocus
| NotebookTabWidgetExistsButNoFocusListFocus
deriving Show
data TabsDoNotMatch
= TabLengthsDifferent Int Int -- ^ The first 'Int' is the number of tabs in the
-- actual GTK 'Notebook'. The second 'Int' is
-- the number of tabs in the 'FocusList'.
| TabAtIndexDifferent Int -- ^ The tab at index 'Int' is different between
-- the actual GTK 'Notebook' and the 'FocusList'.
deriving (Show)
-- | An invariant error on a given 'TMWindow'.
data TMWinInvariantErr
= FocusNotSame FocusNotSameErr Int
| TabsDoNotMatch TabsDoNotMatch
deriving Show
-- | An invariant error on a whole 'TMState'.
data TMStateInvariantErr
= TMWinInvariantErr Int TMWinInvariantErr
-- ^ An invariant error with a given 'TMWindow'. The 'Int' indicates
-- which window it is as an index into 'tmStateWindows'.
deriving Show
invariantTMWindow :: TMWindow -> IO [TMWinInvariantErr]
invariantTMWindow tmWin =
runInvariants
[ invariantFocusSame
, invariantTMTabLength
, invariantTabsAllMatch
]
where
runInvariants :: [IO (Maybe TMWinInvariantErr)] -> IO [TMWinInvariantErr]
runInvariants = fmap catMaybes . sequence
invariantFocusSame :: IO (Maybe TMWinInvariantErr)
invariantFocusSame = do
let tmNote = tmNotebook $ tmWindowNotebook tmWin
index32 <- notebookGetCurrentPage tmNote
maybeWidgetFromNote <- notebookGetNthPage tmNote index32
let focusList = tmNotebookTabs $ tmWindowNotebook tmWin
maybeScrollWinFromFL =
tmNotebookTabTermContainer <$> getFocusItemFL focusList
idx = fromIntegral index32
case (maybeWidgetFromNote, maybeScrollWinFromFL) of
(Nothing, Nothing) -> pure Nothing
(Just _, Nothing) ->
pure $
Just $
FocusNotSame NotebookTabWidgetExistsButNoFocusListFocus idx
(Nothing, Just _) ->
pure $
Just $
FocusNotSame FocusListFocusExistsButNoNotebookTabWidget idx
(Just widgetFromNote, Just scrollWinFromFL) -> do
isEq <- widgetEq widgetFromNote scrollWinFromFL
if isEq
then pure Nothing
else
pure $
Just $
FocusNotSame NotebookTabWidgetDiffersFromFocusListFocus idx
invariantTMTabLength :: IO (Maybe TMWinInvariantErr)
invariantTMTabLength = do
let tmNote = tmNotebook $ tmWindowNotebook tmWin
noteLength32 <- notebookGetNPages tmNote
let noteLength = fromIntegral noteLength32
focusListLength = lengthFL $ tmNotebookTabs $ tmWindowNotebook tmWin
lengthEqual = focusListLength == noteLength
if lengthEqual
then pure Nothing
else pure $
Just $
TabsDoNotMatch $
TabLengthsDifferent noteLength focusListLength
-- Turns a FocusList and Notebook into two lists of widgets and compares each widget for equality
invariantTabsAllMatch :: IO (Maybe TMWinInvariantErr)
invariantTabsAllMatch = do
let tmNote = tmNotebook $ tmWindowNotebook tmWin
focusList = tmNotebookTabs $ tmWindowNotebook tmWin
flList = tmNotebookTabTermContainer <$> toList focusList
noteList <- notebookToList tmNote
tabsMatch noteList flList
where
tabsMatch
:: forall a b
. (IsWidget a, IsWidget b)
=> [a]
-> [b]
-> IO (Maybe TMWinInvariantErr)
tabsMatch xs ys = foldr go (pure Nothing) (zip3 xs ys [0..])
where
go :: (a, b, Int) -> IO (Maybe TMWinInvariantErr) -> IO (Maybe TMWinInvariantErr)
go (x, y, i) acc = do
isEq <- widgetEq x y
if isEq
then acc
else pure . Just $ TabsDoNotMatch (TabAtIndexDifferent i)
-- | Gather up the invariants for 'TMState' and return them as a list.
--
-- If no invariants have been violated, then this function should return an
-- empty list.
invariantTMState' :: TMState' -> IO [TMStateInvariantErr]
invariantTMState' tmState = do
let tmWindows = tmStateWindows tmState
ifoldMap go tmWindows
where
go :: Int -> TMWindow -> IO [TMStateInvariantErr]
go idx tmWin = do
tmWinErrs <- invariantTMWindow tmWin
pure $ fmap (TMWinInvariantErr idx) tmWinErrs
-- | Check the invariants for 'TMState', and call 'fail' if we find that they
-- have been violated.
assertInvariantTMState :: TMState -> IO ()
assertInvariantTMState mvarTMState = do
tmState <- readMVar mvarTMState
assertValue <- invariantTMState' tmState
case assertValue of
[] x-> pure ()
errs@(_:_) -> do
putStrLn "In assertInvariantTMState, some invariants for TMState are being violated."
putStrLn "\nInvariants violated:"
print errs
putStrLn "\nTMState:"
pPrint tmState
putStrLn ""
fail "Invariants violated for TMState"
pPrintTMState :: TMState -> IO ()
pPrintTMState mvarTMState = do
tmState <- readMVar mvarTMState
pPrint tmState
traceShowMTMState :: TMState -> IO ()
traceShowMTMState mvarTMState = do
tmState <- readMVar mvarTMState
print tmState