Skip to content

Commit 9b60ead

Browse files
committed
ff-brick: Add sections
1 parent 3cb010a commit 9b60ead

File tree

2 files changed

+63
-21
lines changed

2 files changed

+63
-21
lines changed

ff-brick/Main.hs

Lines changed: 62 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ import Brick (
3838
(<=>),
3939
)
4040
import Brick qualified
41-
import Brick.Widgets.Border (border)
41+
import Brick.Widgets.Border (border, hBorderWithLabel)
4242
import Brick.Widgets.Border.Style (borderStyleFromChar, unicode)
4343
import Brick.Widgets.List (
4444
List,
@@ -53,9 +53,10 @@ import Brick.Widgets.List (
5353
import Control.Monad (void)
5454
import Data.Function ((&))
5555
import Data.Generics.Labels ()
56-
import Data.Maybe (isJust)
56+
import Data.Map.Strict qualified as Map
5757
import Data.Text (Text)
5858
import Data.Text qualified as Text
59+
import Data.Vector (Vector)
5960
import Data.Vector qualified as Vector
6061
import GHC.Generics (Generic)
6162
import Graphics.Vty (
@@ -82,25 +83,44 @@ import Lens.Micro.Mtl (preuse, use, (.=))
8283
import RON.Storage.FS (runStorage)
8384
import RON.Storage.FS qualified as StorageFS
8485

85-
import FF (fromRgaM, getDataDir, loadAllNotes, noDataDirectoryMessage)
86+
import FF (
87+
defaultNoteFilter,
88+
fromRgaM,
89+
getDataDir,
90+
getUtcToday,
91+
loadAllNotes,
92+
noDataDirectoryMessage,
93+
viewTaskSamples,
94+
)
8695
import FF.Config (loadConfig)
96+
import FF.Config qualified
8797
import FF.Types (
8898
Entity (Entity),
89-
EntityDoc,
99+
EntityView,
100+
ModeMap,
90101
Note (Note),
91-
NoteStatus (TaskStatus),
92-
Status (Active),
102+
NoteSample,
103+
Sample (Sample),
104+
TaskMode,
105+
View (NoteView),
93106
)
94107
import FF.Types qualified
108+
import FF.UI (sampleLabel)
95109

96-
-- | Brick widget names
110+
-- | Widget names
97111
data WN
98112
= NoteList
99113
| OpenNoteViewport
100114
deriving (Eq, Ord, Show)
101115

102116
data Model = Model
103-
{ visibleNotes :: List WN (EntityDoc Note)
117+
{ visibleNotes ::
118+
List
119+
WN
120+
( Either
121+
TaskMode -- section title
122+
(EntityView Note) -- item
123+
)
104124
, isNoteOpen :: Bool
105125
}
106126
deriving (Generic)
@@ -119,18 +139,29 @@ main = do
119139
dataDirM <- getDataDir cfg
120140
handleM <- traverse StorageFS.newHandle dataDirM
121141
handle <- handleM `orElse` fail noDataDirectoryMessage
122-
allNotes <- runStorage handle loadAllNotes
123-
let filteredNotes = filter (isNoteActive . (.entityVal)) allNotes
142+
today <- getUtcToday
143+
let limit = Nothing
144+
samples <-
145+
runStorage handle do
146+
allNotes <- loadAllNotes
147+
viewTaskSamples
148+
defaultNoteFilter
149+
cfg.ui
150+
limit
151+
today
152+
allNotes
124153
let initialModel =
125154
Model
126155
{ visibleNotes =
127-
list NoteList (Vector.fromList filteredNotes) listItemHeight
156+
list NoteList (notesToModel samples) listItemHeight
128157
, isNoteOpen = False
129158
}
130159
void $ defaultMain app initialModel
131160

132-
isNoteActive :: Note -> Bool
133-
isNoteActive Note{note_status} = note_status == Just (TaskStatus Active)
161+
notesToModel :: ModeMap NoteSample -> Vector (Either TaskMode (EntityView Note))
162+
notesToModel = Vector.fromList . foldMap notesToModel' . Map.assocs
163+
where
164+
notesToModel' (taskMode, Sample{items}) = Left taskMode : map Right items
134165

135166
listItemHeight :: Int
136167
listItemHeight = 1
@@ -183,9 +214,9 @@ appDraw Model{visibleNotes, isNoteOpen} = [mainWidget <=> keysHelpLine]
183214

184215
openNoteContent =
185216
case listSelectedElement visibleNotes of
186-
Nothing -> ""
187-
Just (_, Entity _ note) ->
217+
Just (_, Right (Entity _ NoteView{note})) ->
188218
Text.pack $ clean $ fromRgaM note.note_text
219+
_ -> ""
189220

190221
keysHelpLine =
191222
hBox
@@ -199,10 +230,15 @@ appDraw Model{visibleNotes, isNoteOpen} = [mainWidget <=> keysHelpLine]
199230
[ withAttr highlightAttr (txt "^q")
200231
, txt " "
201232
, withAttr highlightAttr (txt "Esc")
202-
, txt " exit "
203-
, withAttr highlightAttr (txt "Enter")
204-
, txt " open"
233+
, txt " exit"
205234
]
235+
++ case listSelectedElement visibleNotes of
236+
Just (_, Right Entity{}) ->
237+
[ txt " "
238+
, withAttr highlightAttr (txt "Enter")
239+
, txt " open"
240+
]
241+
_ -> []
206242

207243
-- Clean string for Brick
208244
clean :: String -> String
@@ -232,7 +268,10 @@ appHandleVtyEvent event = do
232268
-- open selected note
233269
selectedNoteM <-
234270
preuse $ #visibleNotes . listSelectedElementL
235-
#isNoteOpen .= isJust selectedNoteM
271+
#isNoteOpen
272+
.= case selectedNoteM of
273+
Just (Right Entity{}) -> True
274+
_ -> False
236275
e -> zoom #visibleNotes $ handleListEvent e
237276

238277
handleViewportEvent :: Event -> EventM ()
@@ -249,8 +288,10 @@ handleViewportEvent = \case
249288
where
250289
vps = viewportScroll OpenNoteViewport
251290

252-
renderListItem :: Bool -> EntityDoc Note -> Widget
253-
renderListItem _isSelected (Entity _ note) = txt $ noteTitle note
291+
renderListItem :: Bool -> Either TaskMode (EntityView Note) -> Widget
292+
renderListItem _isSelected = \case
293+
Left taskMode -> hBorderWithLabel $ txt $ sampleLabel taskMode
294+
Right (Entity _ NoteView{note}) -> txt $ noteTitle note
254295

255296
noteTitle :: Note -> Text
256297
noteTitle Note{note_text} =

ff-brick/ff-brick.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ executable ff-brick
88
build-depends:
99
base,
1010
brick,
11+
containers,
1112
generic-lens,
1213
microlens-mtl,
1314
ron-storage,

0 commit comments

Comments
 (0)