Add ByteString
This commit is contained in:
parent
c2a0a64a20
commit
c6745f5b35
18
app/Main.hs
18
app/Main.hs
@ -1,11 +1,13 @@
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
|
||||
module Main(main,parseTask, serializeTask, getPriority, comparePriority, TaskMap) where
|
||||
module Main(main, parseTask, serializeTask, getPriority, comparePriority, TaskMap) where
|
||||
|
||||
import System.IO
|
||||
import System.Directory
|
||||
import Data.Text (Text, pack, unpack, isInfixOf, lines, unlines, strip, splitOn)
|
||||
import Data.Text (Text, pack, unpack, isInfixOf, lines, unlines, strip)
|
||||
import qualified Data.Text.IO as TIO
|
||||
import qualified Data.ByteString as BS
|
||||
import qualified Data.ByteString.Char8 as BSC
|
||||
import Control.Monad (when)
|
||||
import Data.List (sortBy)
|
||||
import Data.Ord (comparing)
|
||||
@ -42,8 +44,8 @@ displayTasks = do
|
||||
exists <- doesFileExist todoFile
|
||||
if exists
|
||||
then do
|
||||
contents <- TIO.readFile todoFile
|
||||
let taskMap = parseTask contents
|
||||
contents <- BSC.readFile todoFile
|
||||
let taskMap = parseTask (pack (BSC.unpack contents))
|
||||
putStrLn "Tasks:"
|
||||
mapM_ (putStrLn . showTask) (Map.toList taskMap)
|
||||
else putStrLn "No tasks found."
|
||||
@ -57,12 +59,12 @@ addTask = do
|
||||
exists <- doesFileExist todoFile
|
||||
if exists
|
||||
then do
|
||||
contents <- TIO.readFile todoFile
|
||||
let taskMap = parseTask contents
|
||||
contents <- BSC.readFile todoFile
|
||||
let taskMap = parseTask (pack (BSC.unpack contents))
|
||||
newId = if Map.null taskMap then 1 else fst (Map.findMax taskMap) + 1
|
||||
newTaskMap = Map.insert newId (pack task) taskMap
|
||||
TIO.writeFile todoFile (serializeTask newTaskMap)
|
||||
else TIO.writeFile todoFile (pack task <> "\n")
|
||||
BSC.writeFile todoFile (BSC.pack (unpack (serializeTask newTaskMap)))
|
||||
else BSC.writeFile todoFile (BSC.pack (task <> "\n"))
|
||||
putStrLn "Task added."
|
||||
main
|
||||
|
||||
|
@ -24,6 +24,8 @@ dependencies:
|
||||
- text
|
||||
- directory
|
||||
- containers
|
||||
- bytestring
|
||||
|
||||
|
||||
ghc-options:
|
||||
- -Wall
|
||||
|
@ -35,6 +35,7 @@ library
|
||||
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints
|
||||
build-depends:
|
||||
base >=4.7 && <5
|
||||
, bytestring
|
||||
, containers
|
||||
, directory
|
||||
, text
|
||||
@ -51,6 +52,7 @@ executable todo-app
|
||||
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
|
||||
build-depends:
|
||||
base >=4.7 && <5
|
||||
, bytestring
|
||||
, containers
|
||||
, directory
|
||||
, text
|
||||
@ -69,6 +71,7 @@ test-suite todo-app-test
|
||||
ghc-options: -Wall -Wcompat -Widentities -Wincomplete-record-updates -Wincomplete-uni-patterns -Wmissing-export-lists -Wmissing-home-modules -Wpartial-fields -Wredundant-constraints -threaded -rtsopts -with-rtsopts=-N
|
||||
build-depends:
|
||||
base >=4.7 && <5
|
||||
, bytestring
|
||||
, containers
|
||||
, directory
|
||||
, hspec
|
||||
|
Loading…
Reference in New Issue
Block a user