diff options
| author | Akshay Nair <phenax5@gmail.com> | 2025-10-03 21:57:20 +0530 |
|---|---|---|
| committer | Akshay Nair <phenax5@gmail.com> | 2025-10-03 21:57:20 +0530 |
| commit | b05be850349dbb813d2af6f3ee7a2fc3bf98b8ef (patch) | |
| tree | dcfb1264b3305ef143f3cb642529b1bb81907197 /lib/Daffm/Action | |
| parent | 931cf30e66238432fbf653adda7bd08f05a18e87 (diff) | |
| download | daffm-b05be850349dbb813d2af6f3ee7a2fc3bf98b8ef.tar.gz daffm-b05be850349dbb813d2af6f3ee7a2fc3bf98b8ef.zip | |
Refactor to Text
Diffstat (limited to 'lib/Daffm/Action')
| -rw-r--r-- | lib/Daffm/Action/Cmdline.hs | 27 | ||||
| -rw-r--r-- | lib/Daffm/Action/Core.hs | 13 |
2 files changed, 20 insertions, 20 deletions
diff --git a/lib/Daffm/Action/Cmdline.hs b/lib/Daffm/Action/Cmdline.hs index e7376c3..12f7a9d 100644 --- a/lib/Daffm/Action/Cmdline.hs +++ b/lib/Daffm/Action/Cmdline.hs @@ -8,7 +8,6 @@ import Control.Monad.State (get, gets, modify) import Daffm.Action.Core (reloadDir) import Daffm.Types (AppEvent, AppState (..), FileInfo (..), FocusTarget (..)) import Data.Char (isSpace) -import Data.List (dropWhileEnd) import qualified Data.Set as Set import qualified Data.Text as Text import qualified Data.Text.Zipper as Z @@ -21,9 +20,9 @@ leaveCmdline = clearCmdline >> modify (\st -> st {stateFocusTarget = FocusMain}) enterCmdline :: AppEvent () enterCmdline = modify (\st -> st {stateFocusTarget = FocusCmdline}) -setCmdlineText :: String -> AppEvent () +setCmdlineText :: Text.Text -> AppEvent () setCmdlineText text = - applyCmdlineEdit (const $ Z.stringZipper [text] (Just 1)) + applyCmdlineEdit (const $ Z.textZipper [text] (Just 1)) clearCmdline :: AppEvent () clearCmdline = applyCmdlineEdit Z.clearZipper @@ -34,17 +33,17 @@ runCmdline = do evaluateCommand cmd leaveCmdline where - trimCmd = dropWhile isSpace . dropWhileEnd isSpace . unlines + trimCmd = Text.dropWhile isSpace . Text.dropWhileEnd isSpace . Text.unlines -evaluateCommand :: String -> AppEvent () -evaluateCommand ('!' : '!' : cmd) = do - cmd' <- Text.unpack <$> cmdSubstitutions (Text.pack cmd) +evaluateCommand :: Text.Text -> AppEvent () +evaluateCommand (Text.splitAt 2 -> ("!!", cmd)) = do + cmd' <- Text.unpack <$> cmdSubstitutions cmd suspendAndResume' $ do callCommand cmd' putStrLn "Press any key to continue" >> void getChar reloadDir -evaluateCommand ('!' : cmd) = do - cmd' <- Text.unpack <$> cmdSubstitutions (Text.pack cmd) +evaluateCommand (Text.splitAt 1 -> ("!", cmd)) = do + cmd' <- Text.unpack <$> cmdSubstitutions cmd suspendAndResume' $ callCommand cmd' reloadDir evaluateCommand "delete" = do @@ -54,7 +53,7 @@ evaluateCommand "delete" = do then maybe [] ((: []) . filePath . snd) $ L.listSelectedElement stateFiles else Set.elems stateFileSelections unless (null files) $ do - suspendAndResume' $ callProcess "rm" ("-rfi" : files) + suspendAndResume' $ callProcess "rm" ("-rfi" : map Text.unpack files) reloadDir evaluateCommand _cmd = pure () @@ -63,16 +62,16 @@ cmdSubstitutions cmd = do (AppState {stateFiles, stateCwd, stateFileSelections}) <- get let file = maybe "" (filePath . snd) . L.listSelectedElement $ stateFiles let escape = (\s -> "'" <> s <> "'") . Text.replace "'" "\\'" - let selections = map Text.pack $ Set.elems stateFileSelections + let selections = Set.elems stateFileSelections -- TODO: Escaping % let subst = - Text.replace "%" (Text.pack file) - . Text.replace "%d" (Text.pack stateCwd) + Text.replace "%" file + . Text.replace "%d" stateCwd . Text.replace "%s" (Text.unwords $ map escape selections) . Text.replace "%S" (Text.dropWhileEnd (== '\n') $ Text.unlines selections) pure . subst $ cmd -applyCmdlineEdit :: (Zipper.TextZipper String -> Zipper.TextZipper String) -> AppEvent () +applyCmdlineEdit :: (Zipper.TextZipper Text.Text -> Zipper.TextZipper Text.Text) -> AppEvent () applyCmdlineEdit zipper = do editor <- gets stateCmdlineEditor let editor' = Editor.applyEdit zipper editor diff --git a/lib/Daffm/Action/Core.hs b/lib/Daffm/Action/Core.hs index d18bda9..337801e 100644 --- a/lib/Daffm/Action/Core.hs +++ b/lib/Daffm/Action/Core.hs @@ -8,8 +8,9 @@ import Brick (suspendAndResume') import qualified Brick.Widgets.List as L import Control.Monad.State (MonadIO (liftIO), MonadState, get, gets, modify, put) import Daffm.State -import Daffm.Types (AppEvent, AppState (..), FileInfo (..), FileType (..)) +import Daffm.Types (AppEvent, AppState (..), FileInfo (..), FilePathText, FileType (..)) import qualified Data.Set as Set +import qualified Data.Text as Text import System.Directory (getHomeDirectory) import System.FilePath (takeDirectory) import System.Process (callProcess) @@ -17,7 +18,7 @@ import System.Process (callProcess) modifyM :: (MonadState s m) => (s -> m s) -> m () modifyM f = get >>= f >>= put -loadDir :: FilePath -> FilePath -> AppEvent () +loadDir :: FilePathText -> FilePathText -> AppEvent () loadDir dir parentDir = do modifyM (liftIO . (>>= filterInvalidSelections) . loadDirToState dir parentDir) @@ -29,12 +30,12 @@ reloadDir = do goBackToParentDir :: AppEvent () goBackToParentDir = do dir <- gets stateParentDir - loadDir dir (takeDirectory dir) + loadDir dir (Text.pack . takeDirectory $ Text.unpack dir) goHome :: AppEvent () goHome = do dir <- liftIO getHomeDirectory - loadDir dir (takeDirectory dir) + loadDir (Text.pack dir) (Text.pack $ takeDirectory dir) openSelectedFile :: AppEvent () openSelectedFile = do @@ -47,8 +48,8 @@ openFile (FileInfo {filePath, fileType = Directory}) = do gets stateCwd >>= loadDir filePath openFile (FileInfo {filePath, fileType}) = do suspendAndResume' $ do - putStrLn $ "Opening " <> show fileType <> ": " <> filePath - callProcess "nvim" [filePath] + putStrLn $ "Opening " <> show fileType <> ": " <> Text.unpack filePath + callProcess "nvim" [Text.unpack filePath] currentFile :: AppEvent (Maybe FileInfo) currentFile = do |
