Skip to content

Commit

Permalink
The path found may not be a file
Browse files Browse the repository at this point in the history
  • Loading branch information
fujimura committed Mar 14, 2024
1 parent df63ecb commit 0b0b616
Showing 1 changed file with 22 additions and 16 deletions.
38 changes: 22 additions & 16 deletions src/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ import qualified Data.ByteString as BS
import qualified Data.Text as T
import qualified Data.Text.Encoding as T
import qualified Data.Text.IO as T
import System.Directory (doesFileExist)
import System.Environment (lookupEnv)

Check warning on line 12 in src/Lib.hs

View workflow job for this annotation

GitHub Actions / Run test on GHC latest

The import of ‘System.Environment’ is redundant

Check warning on line 12 in src/Lib.hs

View workflow job for this annotation

GitHub Actions / Build on GHC latest

The import of ‘System.Environment’ is redundant
import System.IO
( BufferMode (NoBuffering),
hClose,
Expand Down Expand Up @@ -41,26 +43,30 @@ substitute ::
FilePath -> -- File
IO ()
substitute re to file = do
b <- BS.readFile file
case T.decodeUtf8' b of
Left _ -> return ()
Right content -> do
let newContent :: T.Text = replaceAll to (content *=~ re)
T.writeFile file newContent
e <- doesFileExist file -- TODO: Test
when e $ do
b <- BS.readFile file
case T.decodeUtf8' b of
Left _ -> return ()
Right content -> do
let newContent :: T.Text = replaceAll to (content *=~ re)
T.writeFile file newContent

substituteInteractive ::
RE -> -- From
T.Text -> -- To
FilePath -> -- File
IO ()
substituteInteractive re to file = do
original <- T.readFile file
let changed :: T.Text = replaceAll to (original *=~ re)
withSystemTempFile ("git-gsub" ++ ".") $ \tmpFile hFile -> do
T.hPutStr hFile changed
hClose hFile
(_, diff, _) <- readProcessWithExitCode "git" ["diff", "--no-index", "--color", file, tmpFile] []
putStrLn diff
putStrLn "Apply this change?(y|Enter/n)"
answer <- getChar
when (answer `elem` "y\n") $ T.writeFile file changed
e <- doesFileExist file -- TODO: Test
when e $ do
original <- T.readFile file
let changed :: T.Text = replaceAll to (original *=~ re)
withSystemTempFile ("git-gsub" ++ ".") $ \tmpFile hFile -> do
T.hPutStr hFile changed
hClose hFile
(_, diff, _) <- readProcessWithExitCode "git" ["diff", "--no-index", "--color", file, tmpFile] []
putStrLn diff
putStrLn "Apply this change?(y|Enter/n)"
answer <- getChar
when (answer `elem` "y\n") $ T.writeFile file changed

0 comments on commit 0b0b616

Please sign in to comment.