From d979ae5ae179e63ac67cc97c10e715f7d8b39065 Mon Sep 17 00:00:00 2001 From: Daisuke Fujimura Date: Sun, 24 Mar 2024 22:35:55 +0900 Subject: [PATCH] Drop interactive mode We can use Git to stage result interactively --- src/Cli.hs | 1 - src/Lib.hs | 27 +++------------------------ src/Types.hs | 3 +-- test/Git/GsubSpec.hs | 5 ----- 4 files changed, 4 insertions(+), 32 deletions(-) diff --git a/src/Cli.hs b/src/Cli.hs index 3b3c88c..c8209c2 100644 --- a/src/Cli.hs +++ b/src/Cli.hs @@ -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") diff --git a/src/Lib.hs b/src/Lib.hs index 8587a0d..8cdc63e 100644 --- a/src/Lib.hs +++ b/src/Lib.hs @@ -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 @@ -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) \ No newline at end of file diff --git a/src/Types.hs b/src/Types.hs index 1b79603..d540507 100644 --- a/src/Types.hs +++ b/src/Types.hs @@ -3,6 +3,5 @@ module Types where data Options = Options { from :: FilePath, to :: FilePath, - path :: FilePath, - interactive :: Bool + path :: FilePath } diff --git a/test/Git/GsubSpec.hs b/test/Git/GsubSpec.hs index ee91804..2c5b2bd 100644 --- a/test/Git/GsubSpec.hs +++ b/test/Git/GsubSpec.hs @@ -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 ()))