aboutsummaryrefslogtreecommitdiffstats
path: root/app/Issue.hs
diff options
context:
space:
mode:
authorLibravatar Alexander Foremny <aforemny@posteo.de>2023-11-28 12:46:59 +0100
committerLibravatar Alexander Foremny <aforemny@posteo.de>2023-11-28 12:46:59 +0100
commitc138fb9910c661f7efd00cc7dceb6fc68dc790a9 (patch)
treeef05d171092007d556c4894524f7224b5caa3b2d /app/Issue.hs
parent457cc50cf9cbf8906695758acadee4bf39a4eed8 (diff)
move `replaceText` to `Issue`
Diffstat (limited to 'app/Issue.hs')
-rw-r--r--app/Issue.hs23
1 files changed, 23 insertions, 0 deletions
diff --git a/app/Issue.hs b/app/Issue.hs
index 9d5a102..411e910 100644
--- a/app/Issue.hs
+++ b/app/Issue.hs
@@ -3,12 +3,14 @@ module Issue
Provenance (..),
id,
internalTags,
+ replaceText,
)
where
import Data.Binary (Binary)
import Data.List (find)
import Data.Text qualified as T
+import Data.Text.IO qualified as T
import Data.Time.Clock (UTCTime (utctDay))
import GHC.Generics (Generic)
import GHC.Records (HasField (..))
@@ -55,3 +57,24 @@ toSpinalCase :: T.Text -> T.Text
toSpinalCase = T.replace " " "-" . T.filter keep . T.toLower
where
keep = (`elem` (concat [[' ', '-'], ['a' .. 'z'], ['0' .. '9']]))
+
+-- TODO `replaceFile` hardcodes comment
+--
+-- @difficulty easy
+replaceText :: Issue -> T.Text -> IO ()
+replaceText issue s' = T.writeFile issue.file . replace (indent (comment s')) =<< T.readFile issue.file
+ where
+ comment = T.intercalate "\n" . map T.strip . map ("-- " <>) . T.lines
+ indent = T.intercalate "\n" . mapButFirst (T.replicate (issue.start.column - 1) " " <>) . T.lines
+ replace s t = before <> s <> after
+ where
+ t' = T.lines t
+ before = T.intercalate "\n" (mapLast (T.take (issue.start.column - 1)) (take issue.start.row t'))
+ after = T.unlines (mapFirst (T.drop issue.end.column) (drop (issue.end.row - 1) t'))
+ mapFirst _ [] = []
+ mapFirst f (x : xs) = f x : xs
+ mapLast _ [] = []
+ mapLast f [x] = [f x]
+ mapLast f (x : xs) = x : mapLast f xs
+ mapButFirst _ [] = []
+ mapButFirst f (x : xs) = x : map f xs