Skip to content

Commit

Permalink
Drop interactive mode
Browse files Browse the repository at this point in the history
We can use Git to stage result interactively
  • Loading branch information
fujimura committed Mar 24, 2024
1 parent ddef18f commit d979ae5
Show file tree
Hide file tree
Showing 4 changed files with 4 additions and 32 deletions.
1 change: 0 additions & 1 deletion src/Cli.hs
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,3 @@ parseOptions =
<$> argument str (help "from")
<*> argument str (help "to")
<*> argument str (help "path" <> value "./")
<*> switch (long "interactive" <> short 'i' <> help "Run interactively")
27 changes: 3 additions & 24 deletions src/Lib.hs
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,12 @@ import Text.Regex.TDFA (matchTest, (=~))
import Types

run :: Options -> IO ()
run Options {from, to, path, interactive} = do
run Options {from, to, path} = do
hSetBuffering stdin NoBuffering
targets <- getTargetFiles path
re <- compileRegex from
let to' = T.encodeUtf8 . T.pack $ to
if interactive
then mapM_ (substituteInteractive re to') targets
else mapConcurrently_ (substitute re to') targets
mapConcurrently_ (substitute re to') targets

getTargetFiles :: FilePath -> IO [FilePath]
getTargetFiles path = do
Expand All @@ -56,23 +54,4 @@ substitute re to file = do
content <- BS.readFile file
when (matchTest (reRegex re) content) $ do
let newContent = replaceAll to (content *=~ re)
seq (BS.length newContent) (BS.writeFile file newContent)

substituteInteractive ::
RE -> -- From
ByteString -> -- To
FilePath -> -- File
IO ()
substituteInteractive re to file = do
e <- doesFileExist file -- TODO: Test
when e $ do
original <- BS.readFile file
let changed :: ByteString = replaceAll to (original *=~ re)
withSystemTempFile ("git-gsub" ++ ".") $ \tmpFile hFile -> do
BS.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") $ BS.writeFile file changed
seq (BS.length newContent) (BS.writeFile file newContent)
3 changes: 1 addition & 2 deletions src/Types.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,5 @@ module Types where
data Options = Options
{ from :: FilePath,
to :: FilePath,
path :: FilePath,
interactive :: Bool
path :: FilePath
}
5 changes: 0 additions & 5 deletions test/Git/GsubSpec.hs
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,6 @@ spec = around_ (hSilence [stdout] . inTempRepo) $ do
actual' <- readFile "foo.rb"
actual' `shouldNotContain` "def bar"

it "should substitute interactively" $ do
runWithStdin "y" $ Cli.run ["-i", "foo", "bar"]
actual <- readFile "foo.rb"
actual `shouldContain` "def bar"

it "should show version" $ do
let run args = fst <$> (capture $ Cli.run args `catch` (\ExitSuccess -> return ()))

Expand Down

0 comments on commit d979ae5

Please sign in to comment.