~jackwines/fast-bronze

1004e2f53431688390aab6e9b741c6d2835d63aa — Jack Wines 4 months ago d90bbf2
you can now press left as well as right to navigate directories
2 files changed, 27 insertions(+), 25 deletions(-)

M src/Catalog.hs
M src/Main.hs
M src/Catalog.hs => src/Catalog.hs +7 -8
@@ 9,8 9,7 @@ data SearchDir = SearchDir {
                 } deriving (Show)
data Action = Action {
  actionSearchString :: T.Text,
  action :: Cataloged -> IO (),
  inputs :: [InputType]
  action :: Cataloged -> IO ()
  }

data Cataloged = Cataloged {


@@ 41,6 40,8 @@ getCatalog = searchCatalogItem SearchDir {
  searchDir = "/Users/jackoe"
  }


-- recursively crawl a director to the desired depth
searchCatalogItem :: SearchDir -> IO [Cataloged]
searchCatalogItem (SearchDir depth dir) = do
  isDir <- P.isDirectory <$> P.getFileStatus dir


@@ 66,21 67,19 @@ dirToCatalog dir isDir =

-- we only match the first one because the action is always the second thing
-- that's selected, regardless of how many inputs the action takes
validAction :: Action -> Cataloged -> Bool
validAction action = (==) (head . inputs $ action) . inputType
-- validAction :: Action -> Cataloged -> Bool
-- validAction action = (==) (head . inputs $ action) . inputType

getActions :: Cataloged -> [Action]
getActions cataloged =
  [
    Action {
      actionSearchString = "open",
      action = \cataloged ->  P.executeFile "open" True [T.unpack . dirPath $ cataloged] Nothing,
      inputs = [Other]
      action = \cataloged ->  P.executeFile "open" True [T.unpack . dirPath $ cataloged] Nothing
    },
    Action {
      actionSearchString = "move to trash",
      action = \cataloged -> P.executeFile "mv" True [T.unpack . dirPath $ cataloged, "~/.Trash"] Nothing,
      inputs = [Other]
      action = \cataloged -> P.executeFile "mv" True [T.unpack . dirPath $ cataloged, "~/.Trash"] Nothing
      }
    -- Action {
    --   actionSearchString = "move to",

M src/Main.hs => src/Main.hs +20 -17
@@ 103,7 103,7 @@ view' s =
          }
        ]
      where
        otherOptions = map searchListWidget . reverse . maybe [] LZ.after $ case focus s of
        otherOptions = map searchListWidget . maybe [] LZ.after $ case focus s of
          Main.Action ->  LZ.map searchString <$> currActions s
          Catalog -> LZ.map searchString <$> currCatalogs s



@@ 142,23 142,26 @@ update' s Down = Transition (selectedZipperLeft  s) (return Nothing)
-- move right, into the current directory
update' s Main.Right = Transition s $
  case (currCatalogs s, focus s) of
    (Just zipper, Catalog) -> do
      let currPath :: String = T.unpack . dirPath . LZ.focus $ zipper
    (Just (LZ.ListZipper _ curr _ ), Catalog) -> do
      let currPath :: String = T.unpack . dirPath $ curr
      isDir <- P.isDirectory <$> P.getFileStatus currPath
      if not isDir then return Nothing else do
        childPaths <- map (\x -> getFocusFilePath zipper ++ '/':x) . filterHiddenFiles <$> (D.listDirectory .  getFocusFilePath $ zipper)
        mapM (putStrLn . show) childPaths
        childPaths <- filterHiddenFiles <$> (listAbsoluteDir . T.unpack . dirPath $ curr)
        childIsDirs <- mapM (fmap P.isDirectory . P.getFileStatus) childPaths
        return . Just . ReplaceCatalog $ zipWith dirToCatalog childPaths childIsDirs
    _ -> return Nothing
update' s (ReplaceCatalog c) = Transition (s {currCatalogs = LZ.fromList c, searchText = ""}) (do
                                                                                                  print c
                                                                                                  return Nothing)





update' s Main.Left = Transition s $
  case (currCatalogs s, focus s) of
    (Just (LZ.ListZipper _ curr _ ), Catalog) -> do
      let currPath = getParentDir . T.unpack . dirPath $ curr
      if currPath == "/" || currPath == "" then return Nothing else do
        let currPathParent = getParentDir currPath
        childPaths <- filter (/= currPathParent) . filterHiddenFiles <$> listAbsoluteDir currPathParent
        childIsDirs <- mapM (fmap P.isDirectory . P.getFileStatus) childPaths
        currPathParentAsCatalog <- dirToCatalog currPathParent . P.isDirectory <$> P.getFileStatus currPathParent
        return . Just . ReplaceCatalog $ currPathParentAsCatalog : zipWith dirToCatalog childPaths childIsDirs -- we place it at the start so it's the selected item
    _ -> return Nothing
update' s (ReplaceCatalog c) = Transition (s {currCatalogs = LZ.fromList c, searchText = ""}) (return Nothing)
update' s Enter =
  Transition s (do
    fromMaybe (return ()) $ (fromMaybe (const $ return ()) (action <$> (searchAction s))) <$> (searchCatalog s)


@@ 167,6 170,10 @@ update' s (KeyPressed c) = Transition (applySearchFilter s searchText') (return 
  where
    searchText' = T.snoc (searchText s) c

listAbsoluteDir dir = map (\xs -> dir ++ '/' : xs) <$> D.listDirectory dir

getParentDir = reverse . tail . dropWhile (/= '/') .  reverse

resetCurrZipper s = (case focus s of
  Catalog -> s {currCatalogs = Nothing, currActions = Nothing}
  Main.Action -> s {currActions = Nothing})


@@ 220,9 227,6 @@ replaceNothingWith catalogZipper actionZipper s = case focus s of
filterHiddenFiles :: [FilePath] -> [FilePath]
filterHiddenFiles = filter (not . L.isInfixOf "/.")

getFocusFilePath :: LZ.ListZipper Cataloged -> FilePath
getFocusFilePath = T.unpack . dirPath . LZ.focus

main :: IO ()
main = do
  void $ Gtk.init Nothing


@@ 253,7 257,6 @@ main = do
handleKeyPress ::  EK.EventKey -> Window -> IO (Bool, Event)
handleKeyPress key window = do
  keyPressed <- EK.getEventKeyKeyval key
  print (fromIntegral keyPressed)
  return (True , case fromIntegral keyPressed of
             65288 -> Delete
             65289 -> Tab