Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 81e6885

Browse files
committed
Add sub-command notes to list all notes descending by creation UTC
1 parent 2aa1c75 commit 81e6885

File tree

5 files changed

+107
-15
lines changed

5 files changed

+107
-15
lines changed

tasklite-core/package.yaml

+1
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,7 @@ tests:
103103
- aeson
104104
- hourglass
105105
- hspec >= 2.11 && < 3.0
106+
- MissingH >= 1.4 && < 1.7
106107
- neat-interpolation >= 0.5 && < 0.6
107108
- sqlite-simple
108109
- tasklite-core

tasklite-core/source/Cli.hs

+6
Original file line numberDiff line numberDiff line change
@@ -171,6 +171,7 @@ import Lib (
171171
infoTask,
172172
listAll,
173173
listNoTag,
174+
listNotes,
174175
listOldTasks,
175176
listProjects,
176177
listReady,
@@ -314,6 +315,7 @@ data Command
314315
-- \| Views -- List all available views
315316
| Tags -- List all used tags
316317
| Projects -- List all active tags
318+
| Notes -- List all notes
317319
| Stats -- List statistics about tasks
318320
{- Unset -}
319321
| -- \| Active -- Started tasks
@@ -796,6 +798,9 @@ commandParser conf =
796798
<> command "projects" (toParserInfo (pure Projects)
797799
"List all active tags (a.k.a projects) and their progress")
798800

801+
<> command "notes" (toParserInfo (pure Notes)
802+
"List all notes descending by creation UTC")
803+
799804
<> command "stats" (toParserInfo (pure Stats)
800805
"Show statistics about tasks")
801806

@@ -1173,6 +1178,7 @@ executeCLiCommand conf now connection progName args = do
11731178
RunFilter expressions -> runFilter conf now connection expressions
11741179
Tags -> listTags conf connection
11751180
Projects -> listProjects conf connection
1181+
Notes -> listNotes conf connection
11761182
Stats -> getStats conf connection
11771183
ImportFile filePath -> importFile conf connection filePath
11781184
ImportJson -> importJson conf connection

tasklite-core/source/Lib.hs

+45
Original file line numberDiff line numberDiff line change
@@ -2915,6 +2915,51 @@ listProjects conf connection = do
29152915
pure $ formatTags conf tags
29162916

29172917

2918+
listNotes :: Config -> Connection -> IO (Doc AnsiStyle)
2919+
listNotes conf connection = do
2920+
(notes :: [TaskToNote]) <-
2921+
query_
2922+
connection
2923+
[sql|
2924+
SELECT ulid, task_ulid, note
2925+
FROM task_to_note
2926+
ORDER BY ulid DESC
2927+
|]
2928+
2929+
let
2930+
taskIdWidth = 7 -- TODO: Use dynamic width
2931+
noteWidth = getIdLength $ fromIntegral $ P.length notes
2932+
docHeader =
2933+
annotate
2934+
(idStyle conf <> bold <> underlined)
2935+
(fill taskIdWidth "Task ID")
2936+
<++> annotate
2937+
(idStyle conf <> bold <> underlined)
2938+
(fill noteWidth "ID")
2939+
<++> annotate (dateStyle conf <> bold <> underlined) "Created UTC"
2940+
<++> annotate (bold <> underlined) "Note"
2941+
<++> line
2942+
2943+
showNote note =
2944+
annotate
2945+
(idStyle conf)
2946+
(fill 7 $ pretty $ T.takeEnd taskIdWidth note.task_ulid)
2947+
<++> annotate
2948+
(idStyle conf)
2949+
(fill noteWidth $ pretty $ T.takeEnd noteWidth note.ulid)
2950+
<++> annotate
2951+
(dateStyle conf)
2952+
( note.ulid
2953+
& ulidTextToDateTime
2954+
<&> timePrint ISO8601_Date
2955+
& pretty
2956+
)
2957+
<++> pretty note.note
2958+
<> line
2959+
2960+
pure $ docHeader <> vsep (notes <&> showNote)
2961+
2962+
29182963
getStats :: Config -> Connection -> IO (Doc AnsiStyle)
29192964
getStats _ connection = do
29202965
[NumRows numOfTasksTotal] <-

tasklite-core/tasklite-core.cabal

+2-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,8 @@ test-suite tasklite-test
137137
UndecidableInstances
138138
ghc-options: -Wall -Wcompat -Wincomplete-record-updates -Wincomplete-uni-patterns -Wno-orphans -Wredundant-constraints -Wunused-packages
139139
build-depends:
140-
aeson
140+
MissingH >=1.4 && <1.7
141+
, aeson
141142
, base >=4.18 && <5
142143
, hourglass
143144
, hspec >=2.11 && <3.0

tasklite-core/test/LibSpec.hs

+53-14
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,21 @@
11
module LibSpec where
22

33
import Protolude (
4+
Bool (True),
45
Maybe (..),
56
Text,
67
pure,
78
show,
89
($),
10+
(&),
11+
(<),
912
(<>),
1013
)
1114
import Protolude qualified as P
1215

13-
import Config (defaultConfig)
16+
import Data.Hourglass (DateTime)
17+
import Data.List.Utils (subIndex)
18+
import Data.Text qualified as T
1419
import Test.Hspec (
1520
Spec,
1621
describe,
@@ -21,8 +26,7 @@ import Test.Hspec (
2126
shouldNotContain,
2227
)
2328

24-
import Data.Hourglass (DateTime)
25-
import Data.Text qualified as T
29+
import Config (defaultConfig)
2630
import ImportExport (PreEdit (ApplyPreEdit), editTaskByTask)
2731
import Lib (
2832
addTag,
@@ -31,27 +35,28 @@ import Lib (
3135
infoTask,
3236
insertRecord,
3337
insertTags,
38+
listNotes,
3439
newTasks,
3540
)
3641
import Task (Task (body, closed_utc, state, ulid), TaskState (Done), zeroTask)
37-
import TaskToNote (TaskToNote (TaskToNote))
42+
import TaskToNote (TaskToNote (TaskToNote, ulid))
3843
import TaskToNote qualified
3944
import TestUtils (withMemoryDb)
4045

4146

4247
task1 :: Task
4348
task1 =
4449
zeroTask
45-
{ ulid = "01hs68z7mdg4ktpxbv0yfafznq"
46-
, body = "New task 1"
50+
{ Task.ulid = "01hs68z7mdg4ktpxbv0yfafznq"
51+
, Task.body = "New task 1"
4752
}
4853

4954

5055
taskMultiLine :: Task
5156
taskMultiLine =
5257
zeroTask
53-
{ ulid = "01hx48cnjhp18mts3c44zk3gen"
54-
, body =
58+
{ Task.ulid = "01hx48cnjhp18mts3c44zk3gen"
59+
, Task.body =
5560
"New task\n\
5661
\with several lines\n\
5762
\and line breaks"
@@ -66,8 +71,8 @@ spec now = do
6671
let
6772
task2 =
6873
zeroTask
69-
{ ulid = "01hs690f9hkzk9z7zews9j2k1d"
70-
, body = "New task 2"
74+
{ Task.ulid = "01hs690f9hkzk9z7zews9j2k1d"
75+
, Task.body = "New task 2"
7176
}
7277

7378
count0 <- countTasks defaultConfig memConn P.mempty
@@ -93,10 +98,10 @@ spec now = do
9398
let
9499
task2 =
95100
zeroTask
96-
{ ulid = "01hs6zsf3c0vqx6egfnmbqtmvy"
97-
, body = "New task 2"
98-
, closed_utc = Just "2024-04-10T18:54:10Z"
99-
, state = Just Done
101+
{ Task.ulid = "01hs6zsf3c0vqx6egfnmbqtmvy"
102+
, Task.body = "New task 2"
103+
, Task.closed_utc = Just "2024-04-10T18:54:10Z"
104+
, Task.state = Just Done
100105
}
101106

102107
insertRecord "tasks" memConn task1
@@ -141,6 +146,40 @@ spec now = do
141146
\with several lines\n\
142147
\and line breaks"
143148

149+
it "lists all notes descending by creation UTC" $ do
150+
withMemoryDb defaultConfig $ \memConn -> do
151+
insertRecord "tasks" memConn task1
152+
let
153+
taskToNote1 =
154+
TaskToNote
155+
{ TaskToNote.ulid = "01hx4eyxxvs5b75ynxrztcz87f"
156+
, TaskToNote.task_ulid = task1.ulid
157+
, TaskToNote.note = "The first note"
158+
}
159+
note1Id = taskToNote1.ulid & T.takeEnd 3 & T.unpack
160+
taskToNote2 =
161+
TaskToNote
162+
{ TaskToNote.ulid = "01hx4f3f764sma7n8bahvwjeed"
163+
, TaskToNote.task_ulid = task1.ulid
164+
, TaskToNote.note = "The second note"
165+
}
166+
note2Id = taskToNote2.ulid & T.takeEnd 3 & T.unpack
167+
168+
insertRecord "task_to_note" memConn taskToNote1
169+
insertRecord "task_to_note" memConn taskToNote2
170+
171+
cliOutput <- listNotes defaultConfig memConn
172+
173+
show cliOutput `shouldContain` note1Id
174+
show cliOutput `shouldContain` note2Id
175+
176+
let
177+
posUlid1 = subIndex note1Id (show cliOutput)
178+
posUlid2 = subIndex note2Id (show cliOutput)
179+
180+
-- Newer notes should be listed first
181+
(posUlid2 < posUlid1) `shouldBe` True
182+
144183
it "lets you delete a note" $ do
145184
withMemoryDb defaultConfig $ \memConn -> do
146185
insertRecord "tasks" memConn task1

0 commit comments

Comments
 (0)