initial commit
This commit is contained in:
commit
70d3db13c7
17 changed files with 1109 additions and 0 deletions
39
app/FileActions.hs
Normal file
39
app/FileActions.hs
Normal file
|
|
@ -0,0 +1,39 @@
|
|||
{-# LANGUAGE ImportQualifiedPost #-}
|
||||
{-# LANGUAGE OverloadedStrings #-}
|
||||
{-# LANGUAGE ScopedTypeVariables #-}
|
||||
|
||||
module FileActions where
|
||||
|
||||
import Data.Text qualified as T
|
||||
import Data.Text.IO qualified as TIO
|
||||
import System.Directory
|
||||
import TodoListItem
|
||||
|
||||
tempFile = "tempFile"
|
||||
|
||||
-- TODO: add sanitisation using the todo item type ?
|
||||
-- TODO: sometimes the whitespace does not work as expected, find why and solve it
|
||||
add folder file item = do
|
||||
content <- TIO.readFile (folder <> file)
|
||||
let result = content <> item
|
||||
TIO.writeFile (folder <> tempFile) result
|
||||
renameFile (folder <> tempFile) (folder <> file)
|
||||
|
||||
delete folder file item = do
|
||||
content <- TIO.readFile (folder <> file)
|
||||
let result = T.unlines $ filter (not . item) (T.lines content)
|
||||
TIO.writeFile (folder <> tempFile) result
|
||||
renameFile (folder <> tempFile) (folder <> file)
|
||||
|
||||
check folder file item = do
|
||||
content <- TIO.readFile (folder <> file)
|
||||
let result = T.unlines $ map (swapIfMatch item) (T.lines content)
|
||||
TIO.writeFile (folder <> tempFile) result
|
||||
renameFile (folder <> tempFile) (folder <> file)
|
||||
|
||||
swapIfMatch name strEncodedItem = if name == strEncodedItem then swapCheck strEncodedItem else strEncodedItem
|
||||
|
||||
swapCheck text
|
||||
| T.index text 1 == 'x' = "[ ] " <> T.drop 4 text
|
||||
| T.index text 1 == ' ' = "[x] " <> T.drop 4 text
|
||||
| otherwise = error "incorrectly formated element"
|
||||
Loading…
Add table
Add a link
Reference in a new issue