diff options
author | Alexander Foremny <aforemny@posteo.de> | 2023-11-28 12:46:59 +0100 |
---|---|---|
committer | Alexander Foremny <aforemny@posteo.de> | 2023-11-28 12:46:59 +0100 |
commit | c138fb9910c661f7efd00cc7dceb6fc68dc790a9 (patch) | |
tree | ef05d171092007d556c4894524f7224b5caa3b2d | |
parent | 457cc50cf9cbf8906695758acadee4bf39a4eed8 (diff) |
move `replaceText` to `Issue`
-rw-r--r-- | app/Issue.hs | 23 | ||||
-rw-r--r-- | app/Main.hs | 25 |
2 files changed, 24 insertions, 24 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 diff --git a/app/Main.hs b/app/Main.hs index 2e3cae1..e1b4e18 100644 --- a/app/Main.hs +++ b/app/Main.hs @@ -673,7 +673,7 @@ main = do hFlush h hClose h sh_ (proc "${EDITOR-vi} -- %" fp) - replaceText issue =<< T.readFile fp + I.replaceText issue =<< T.readFile fp else do -- TODO Make `show` page-able -- @@ -768,29 +768,6 @@ prettyTags = ) M.empty --- TODO Move `replaceText` to `Issue` - --- 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 - putDoc :: Color -> Bool -> Maybe Int -> P.Doc P.AnsiStyle -> IO () putDoc colorize noPager width doc = do isTty <- (== 1) <$> c_isatty 1 |