Skip to content

Commit 1d9ae64

Browse files
committed
Select text color according to current background color
1 parent a69b372 commit 1d9ae64

File tree

8 files changed

+94
-42
lines changed

8 files changed

+94
-42
lines changed

stack.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ packages:
1010
extra-deps:
1111
- aeson-1.5.6.0
1212

13+
- ansi-terminal-0.11.4
14+
1315
- iso8601-duration-0.1.1.0
1416

1517
- portable-lines-0.1

stack.yaml.lock

Lines changed: 19 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,56 +5,63 @@
55

66
packages:
77
- completed:
8+
hackage: aeson-1.5.6.0@sha256:5003bb6fd260d2e2d5a51dee2bf5c8e8f29e4e0e0288fef805c22dcc80ecab06,6788
89
pantry-tree:
910
sha256: 1f61b87807bfb4362a978a492e2f17be9e3ebfe32f983ee49863ddde970b32ce
1011
size: 37981
11-
hackage: aeson-1.5.6.0@sha256:5003bb6fd260d2e2d5a51dee2bf5c8e8f29e4e0e0288fef805c22dcc80ecab06,6788
1212
original:
1313
hackage: aeson-1.5.6.0
1414
- completed:
15+
hackage: ansi-terminal-0.11.4@sha256:410737137c798e23339a08435a5511785ebf1db08700e37debbd7801cf73fc82,3584
16+
pantry-tree:
17+
sha256: 49bc6ef92292ee8b663a40cf5170af1efaaa4ed84155b65ad41eabf1faf9b18f
18+
size: 1462
19+
original:
20+
hackage: ansi-terminal-0.11.4
21+
- completed:
22+
hackage: iso8601-duration-0.1.1.0@sha256:0e9e7531e71b693c20115198186a0280aa3631dc29da226e84e55f0984bec1af,2210
1523
pantry-tree:
1624
sha256: 183be7ad7fded29b7dbf94bd57a58755abeab8f1d1d237ac47f95543a84a8ae7
1725
size: 569
18-
hackage: iso8601-duration-0.1.1.0@sha256:0e9e7531e71b693c20115198186a0280aa3631dc29da226e84e55f0984bec1af,2210
1926
original:
2027
hackage: iso8601-duration-0.1.1.0
2128
- completed:
29+
hackage: portable-lines-0.1@sha256:21c3b905888a4b43f957cd8e8cdf2af00942bb161aa19a6b20db18b661de0510,1552
2230
pantry-tree:
2331
sha256: 2eeb1d9aefe4fb172dc56baa75f09aaaac824799d926922b4673188293f9f95c
2432
size: 371
25-
hackage: portable-lines-0.1@sha256:21c3b905888a4b43f957cd8e8cdf2af00942bb161aa19a6b20db18b661de0510,1552
2633
original:
2734
hackage: portable-lines-0.1
2835
- completed:
29-
sha256: 42e6ebee9791e89b9490c3f92003e83a92401ed2635209b7d8a0f5da5d9b37f0
3036
name: ulid
31-
size: 10636
32-
url: https://github.com/ad-si/ulid/archive/443b5d74d0f2abdc3879e8338cc4497d68e640df.tar.gz
3337
pantry-tree:
3438
sha256: a6d5edf44e0c3dbe23454e11b6c7db52373f852fe5e2ade6d23b9842a13be883
3539
size: 1157
40+
sha256: 42e6ebee9791e89b9490c3f92003e83a92401ed2635209b7d8a0f5da5d9b37f0
41+
size: 10636
42+
url: https://github.com/ad-si/ulid/archive/443b5d74d0f2abdc3879e8338cc4497d68e640df.tar.gz
3643
version: 0.3.1.0
3744
original:
3845
url: https://github.com/ad-si/ulid/archive/443b5d74d0f2abdc3879e8338cc4497d68e640df.tar.gz
3946
- completed:
40-
sha256: a003b0d5906248468b886feea1536febc8b91946e634c4321b2f5c1c42744385
4147
name: simple-sql-parser
42-
size: 132157
43-
url: https://github.com/JakeWheat/simple-sql-parser/archive/00433a26e8303c9e61359f406da5a2dbf1293fc8.tar.gz
4448
pantry-tree:
4549
sha256: 8ea116a325adc8001f2f0c52ca5ece1e8d00f5ec44b7bc38de8907305fd1f37b
4650
size: 3477
51+
sha256: a003b0d5906248468b886feea1536febc8b91946e634c4321b2f5c1c42744385
52+
size: 132157
53+
url: https://github.com/JakeWheat/simple-sql-parser/archive/00433a26e8303c9e61359f406da5a2dbf1293fc8.tar.gz
4754
version: 0.5.0
4855
original:
4956
url: https://github.com/JakeWheat/simple-sql-parser/archive/00433a26e8303c9e61359f406da5a2dbf1293fc8.tar.gz
5057
- completed:
51-
sha256: 0642bfda8aaaa84477e030e26770a388e4facbce25f98b8781820c8b82f280e5
5258
name: protolude
53-
size: 33781
54-
url: https://github.com/protolude/protolude/archive/3e249724fd0ead27370c8c297b1ecd38f92cbd5b.tar.gz
5559
pantry-tree:
5660
sha256: 685ed0191298da9540e772ffa746eb078d4f4a77ed8012a9933d3c79644b1054
5761
size: 3146
62+
sha256: 0642bfda8aaaa84477e030e26770a388e4facbce25f98b8781820c8b82f280e5
63+
size: 33781
64+
url: https://github.com/protolude/protolude/archive/3e249724fd0ead27370c8c297b1ecd38f92cbd5b.tar.gz
5865
version: 0.3.1
5966
original:
6067
url: https://github.com/protolude/protolude/archive/3e249724fd0ead27370c8c297b1ecd38f92cbd5b.tar.gz

tasklite-core/app/Main.hs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import GHC.IO.Encoding (setLocaleEncoding, utf8)
2727
import GitHash
2828
import Options.Applicative
2929
import Options.Applicative.Help.Core (parserHelp)
30-
import Paths_tasklite_core ()
30+
import Paths_tasklite_core ( version ) -- Special module provided by Cabal
3131
import System.Directory
3232
( createDirectoryIfMissing
3333
, executable
@@ -46,7 +46,6 @@ import qualified Database.SQLite.Simple as SQLite
4646
import Config (Config(..), HookSet(..), HooksConfig(..), addHookFilesToConfig)
4747
import ImportExport
4848
import Migrations
49-
import Paths_tasklite_core (version) -- Special module provided by Cabal
5049
import Utils
5150

5251

tasklite-core/package.yaml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,14 @@ description: |
2121
2222
dependencies:
2323
- aeson
24+
- ansi-terminal
2425
- base >= 4.7 && < 5
2526
- beam-core
2627
- beam-migrate
2728
- beam-sqlite
2829
- bytestring
2930
- cassava
31+
- colour
3032
- directory
3133
- file-embed
3234
- filepath

tasklite-core/source/Config.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -252,7 +252,7 @@ defaultConfig = Config
252252
{ tableName = "tasks"
253253
, idStyle = color Green
254254
, priorityStyle = color Magenta
255-
, dateStyle = colorDull Black
255+
, dateStyle = color Black
256256
, bodyStyle = color White
257257
, bodyClosedStyle = color Black
258258
, closedStyle = colorDull Black

tasklite-core/source/Lib.hs

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,13 +1550,13 @@ formatTaskLine conf now taskUlidWidth task =
15501550
annotate (dateStyle conf) (pretty taskDate) :
15511551
(pretty $ case FullTask.review_utc task >>= parseUtc of
15521552
Nothing -> "" :: Text
1553-
Just date_ -> if date_ < now then "🔎" else "") :
1554-
(if dueIn mempty { durationHours = 24 } && isOpen
1555-
then "⚠️️ "
1556-
else "") <>
1557-
(if dueIn mempty && isOpen
1558-
then annotate (color Red) (reflow body)
1559-
else grayOutIfDone (reflow body)) :
1553+
Just date_ -> if date_ < now then "🔎 " else "")
1554+
<> (if dueIn mempty { durationHours = 24 } && isOpen
1555+
then "⚠️️ "
1556+
else "") <>
1557+
(if dueIn mempty && isOpen
1558+
then annotate (color Red) (reflow body)
1559+
else grayOutIfDone (reflow body)) :
15601560
annotate (dueStyle conf) (pretty dueUtcMaybe) :
15611561
annotate (closedStyle conf) (pretty closedUtcMaybe) :
15621562
hsep (tags <&> (formatTag conf)) :
@@ -1622,7 +1622,7 @@ headTasks conf now connection = do
16221622
\where closed_utc is null \
16231623
\order by priority desc, due_utc asc, ulid desc \
16241624
\limit " <> show (headCount conf)
1625-
pure $ formatTasks conf now tasks
1625+
formatTasksColor conf now tasks
16261626

16271627

16281628
newTasks :: Config -> DateTime -> Connection -> IO (Doc AnsiStyle)
@@ -1631,7 +1631,7 @@ newTasks conf now connection = do
16311631
"select * from `tasks_view` \
16321632
\where closed_utc is null \
16331633
\order by `ulid` desc limit " <> show (headCount conf)
1634-
pure $ formatTasks conf now tasks
1634+
formatTasksColor conf now tasks
16351635

16361636

16371637
listOldTasks :: Config -> DateTime -> Connection -> IO (Doc AnsiStyle)
@@ -1640,7 +1640,7 @@ listOldTasks conf now connection = do
16401640
"select * from `tasks_view` \
16411641
\where closed_utc is null \
16421642
\order by `ulid` asc limit " <> show (headCount conf)
1643-
pure $ formatTasks conf now tasks
1643+
formatTasksColor conf now tasks
16441644

16451645

16461646
openTasks :: Config -> DateTime -> Connection -> IO (Doc AnsiStyle)
@@ -1649,7 +1649,7 @@ openTasks conf now connection = do
16491649
"select * from `tasks_view` \
16501650
\where closed_utc is null \
16511651
\order by priority desc, due_utc asc, ulid desc"
1652-
pure $ formatTasks conf now tasks
1652+
formatTasksColor conf now tasks
16531653

16541654

16551655
modifiedTasks
@@ -1680,7 +1680,7 @@ modifiedTasks conf now connection listModifiedFlag = do
16801680
AllItems -> tasks
16811681
ModifiedItemsOnly -> filterModified tasks
16821682

1683-
pure $ formatTasks conf now filteredTasks
1683+
formatTasksColor conf now filteredTasks
16841684

16851685

16861686
overdueTasks :: Config -> DateTime -> Connection -> IO (Doc AnsiStyle)
@@ -1689,7 +1689,7 @@ overdueTasks conf now connection = do
16891689
"select * from `tasks_view` \
16901690
\where closed_utc is null and due_utc < datetime('now') \
16911691
\order by priority desc, due_utc asc, ulid desc"
1692-
pure $ formatTasks conf now tasks
1692+
formatTasksColor conf now tasks
16931693

16941694

16951695
doneTasks :: Config -> DateTime -> Connection -> IO (Doc AnsiStyle)
@@ -1698,7 +1698,7 @@ doneTasks conf now connection = do
16981698
"select * from tasks_view \
16991699
\where closed_utc is not null and state is 'Done' \
17001700
\order by closed_utc desc limit " <> show (headCount conf)
1701-
pure $ formatTasks conf now tasks
1701+
formatTasksColor conf now tasks
17021702

17031703

17041704
obsoleteTasks :: Config -> DateTime -> Connection -> IO (Doc AnsiStyle)
@@ -1707,7 +1707,7 @@ obsoleteTasks conf now connection = do
17071707
"select * from tasks_view \
17081708
\where closed_utc is not null and state is 'Obsolete' \
17091709
\order by ulid desc limit " <> show (headCount conf)
1710-
pure $ formatTasks conf now tasks
1710+
formatTasksColor conf now tasks
17111711

17121712

17131713
deletableTasks :: Config -> DateTime -> Connection -> IO (Doc AnsiStyle)
@@ -1716,7 +1716,7 @@ deletableTasks conf now connection = do
17161716
"select * from tasks_view \
17171717
\where closed_utc is not null and state is 'Deletable' \
17181718
\order by ulid desc limit " <> show (headCount conf)
1719-
pure $ formatTasks conf now tasks
1719+
formatTasksColor conf now tasks
17201720

17211721

17221722
listRepeating :: Config -> DateTime -> Connection -> IO (Doc AnsiStyle)
@@ -1726,7 +1726,7 @@ listRepeating conf now connection = do
17261726
\where repetition_duration is not null \
17271727
\order by repetition_duration desc"
17281728

1729-
pure $ formatTasks conf now tasks
1729+
formatTasksColor conf now tasks
17301730

17311731

17321732
listRecurring :: Config -> DateTime -> Connection -> IO (Doc AnsiStyle)
@@ -1736,7 +1736,7 @@ listRecurring conf now connection = do
17361736
\where recurrence_duration is not null \
17371737
\order by recurrence_duration desc"
17381738

1739-
pure $ formatTasks conf now tasks
1739+
formatTasksColor conf now tasks
17401740

17411741

17421742
listReady :: Config -> DateTime -> Connection -> IO (Doc AnsiStyle)
@@ -1749,7 +1749,7 @@ listReady conf now connection = do
17491749
\order by priority desc, due_utc asc, ulid desc \
17501750
\limit " <> show (headCount conf)
17511751

1752-
pure $ formatTasks conf now tasks
1752+
formatTasksColor conf now tasks
17531753

17541754

17551755
listWaiting :: Config -> DateTime -> Connection -> IO (Doc AnsiStyle)
@@ -1760,15 +1760,14 @@ listWaiting conf now connection = do
17601760
\and waiting_utc is not null \
17611761
\and (review_utc > datetime('now') or review_utc is null) \
17621762
\order by waiting_utc desc"
1763-
1764-
pure $ formatTasks conf now tasks
1763+
formatTasksColor conf now tasks
17651764

17661765

17671766
listAll :: Config -> DateTime -> Connection -> IO (Doc AnsiStyle)
17681767
listAll conf now connection = do
17691768
tasks <- query_ connection
17701769
"select * from tasks_view order by ulid asc"
1771-
pure $ formatTasks conf now tasks
1770+
formatTasksColor conf now tasks
17721771

17731772

17741773
listNoTag :: Config -> DateTime -> Connection -> IO (Doc AnsiStyle)
@@ -1777,7 +1776,7 @@ listNoTag conf now connection = do
17771776
"select * from tasks_view \
17781777
\where closed_utc is null and tags is null \
17791778
\order by priority desc, due_utc asc, ulid desc"
1780-
pure $ formatTasks conf now tasks
1779+
formatTasksColor conf now tasks
17811780

17821781

17831782
getWithTag :: Connection -> Maybe DerivedState -> [Text] -> IO [FullTask]
@@ -1817,14 +1816,14 @@ getWithTag connection stateMaybe tags = do
18171816
listWithTag :: Config -> DateTime -> Connection -> [Text] -> IO (Doc AnsiStyle)
18181817
listWithTag conf now connection tags = do
18191818
tasks <- getWithTag connection Nothing tags
1820-
pure $ formatTasks conf now tasks
1819+
formatTasksColor conf now tasks
18211820

18221821

18231822
queryTasks :: Config -> DateTime -> Connection -> Text -> IO (Doc AnsiStyle)
18241823
queryTasks conf now connection sqlQuery = do
18251824
tasks <- query_ connection $ Query $
18261825
"select * from `tasks_view` where " <> sqlQuery
1827-
pure $ formatTasks conf now tasks
1826+
formatTasksColor conf now tasks
18281827

18291828

18301829
runSql :: Config -> Text -> IO (Doc AnsiStyle)
@@ -1954,7 +1953,7 @@ runFilter conf now connection exps = do
19541953
then
19551954
dieWithError $ vsep (fmap ppInvalidFilter errors)
19561955
else
1957-
pure $ formatTasks conf now tasks
1956+
formatTasksColor conf now tasks
19581957

19591958
_ -> dieWithError filterHelp
19601959

@@ -2010,6 +2009,12 @@ formatTasks conf now tasks =
20102009
line
20112010

20122011

2012+
formatTasksColor :: Config -> DateTime -> [FullTask] -> IO (Doc AnsiStyle)
2013+
formatTasksColor conf now tasks = do
2014+
confNorm <- applyColorMode conf
2015+
pure $ formatTasks confNorm now tasks
2016+
2017+
20132018
getProgressBar :: Integer -> Double -> Doc AnsiStyle
20142019
getProgressBar maxWidthInChars progress =
20152020
let

tasklite-core/source/Utils.hs

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,21 @@ import Protolude as P
88

99
import Data.Text as T
1010
import Prettyprinter hiding ((<>))
11+
import Data.Colour.RGBSpace (RGB(..))
1112
import Data.Time (addUTCTime, UTCTime, ZonedTime, zonedTimeToUTC)
1213
import Data.Time.Clock.POSIX (posixSecondsToUTCTime, utcTimeToPOSIXSeconds)
1314
import Data.Hourglass
1415
import Prettyprinter.Render.Terminal
1516
import Data.ULID
1617
import Data.ULID.Random
1718
import Data.ULID.TimeStamp
19+
import System.Console.ANSI (
20+
ConsoleLayer(..),
21+
getLayerColor,
22+
)
1823
import System.Process
1924

25+
2026
import Base32
2127
import Config
2228

@@ -206,3 +212,26 @@ executeHooks stdinText hooks = do
206212
<&> T.pack
207213
& T.unlines
208214
& pretty
215+
216+
217+
218+
applyColorMode :: Config -> IO Config
219+
applyColorMode conf = do
220+
layerColorBgMb <- getLayerColor Background
221+
222+
let
223+
calcLuminance :: RGB Word16 -> Double
224+
calcLuminance (RGB {..}) =
225+
(0.3 * fromIntegral channelRed +
226+
0.6 * fromIntegral channelGreen +
227+
0.1 * fromIntegral channelBlue) / 65536
228+
229+
isLightMode = layerColorBgMb
230+
<&> calcLuminance
231+
& fromMaybe 0
232+
& (> 0.5)
233+
234+
pure $
235+
if isLightMode
236+
then conf { bodyStyle = colorDull Black }
237+
else conf

0 commit comments

Comments
 (0)