Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop interactive mode #8

Merged
merged 1 commit into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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")
25 changes: 2 additions & 23 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 @@ -57,22 +55,3 @@ substitute re to file = do
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
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
Loading