~jackwines/fast-bronze

265fdd5ec21808773b7264d88626a4e929e52689 — Jack Wines 4 months ago 07dffe4
cleanup, now uses open for darwin and xdg-open for linux
4 files changed, 30 insertions(+), 66 deletions(-)

M cabal.project
M fast-bronze.cabal
M src/Catalog.hs
M src/Main.hs
M cabal.project => cabal.project +1 -7
@@ 1,11 1,5 @@
packages: *.cabal

-- source-repository-package
--     type: git
--     location: https://github.com/haskell-gi/haskell-gi.git
--     branch: master
--     tag: a3855b5c8e3d098e56e8eaf9b4bdcba6dbdbe3dc

executable-static:   true
jobs: $ncpus
allow-newer: all
-- allow-newer:  pkg:gi-gtk-declarative-app-simple

M fast-bronze.cabal => fast-bronze.cabal +1 -0
@@ 37,6 37,7 @@ executable fast-bronze
                       UndecidableInstances,
                       RecursiveDo,
                       RecordWildCards,
                       RankNTypes,
                       DuplicateRecordFields
  other-modules:       Catalog,
                       ListZipper

M src/Catalog.hs => src/Catalog.hs +21 -7
@@ 2,6 2,7 @@ module Catalog where
import qualified Data.Text as T
import qualified System.Directory as D
import qualified System.Posix as P
import qualified System.Info as In

data SearchDir = SearchDir {
  dirDepth ::Int,


@@ 39,11 40,10 @@ class Searchable s where
getCatalog :: IO [Cataloged]
getCatalog = searchCatalogItem SearchDir {
  dirDepth = 1,
  searchDir = "/Users/jackoe"
  searchDir = "~/"
  }


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


@@ 78,10 78,6 @@ getActions :: Cataloged -> [Action]
getActions cataloged =
  [
    Action {
      actionSearchString = "open",
      ioAction = OneParam $ \cataloged ->  P.executeFile "open" True [T.unpack . dirPath $ cataloged] Nothing
    },
    Action {
      actionSearchString = "move to trash",
      ioAction = OneParam $ \cataloged -> P.executeFile "mv" True [T.unpack . dirPath $ cataloged, "~/.Trash"] Nothing
    },


@@ 89,4 85,22 @@ getActions cataloged =
      actionSearchString = "move to",
      ioAction = TwoParam (\cataloged cataloged' -> P.executeFile "mv" True (map (T.unpack . dirPath) [cataloged, cataloged']) Nothing)
    }
  ] ++ case In.os of
  "darwin" -> darwinActions
  "linux" -> linuxActions

linuxActions :: [Action]
linuxActions = [
    Action {
      actionSearchString = "open",
      ioAction = OneParam $ \cataloged ->  P.executeFile "open" True [T.unpack . dirPath $ cataloged] Nothing
    }
  ]

darwinActions :: [Action]
darwinActions = [
    Action {
      actionSearchString = "xfg-open",
      ioAction = OneParam $ \cataloged ->  P.executeFile "open" True [T.unpack . dirPath $ cataloged] Nothing
    }
  ]

M src/Main.hs => src/Main.hs +7 -52
@@ 48,38 48,6 @@ data AppState = AppState {
  focus :: Focus,
  searchText :: T.Text
                         }
-- the function in question:
-- it's supposed to apply a function to firstCatalogs or
-- currActions based on what focused tells us
-- but it doesn't compile (ignore line numbers):

-- src/Main.hs:54:41: error:
--     • Couldn't match type ‘a’ with ‘Action’
--       ‘a’ is a rigid type variable bound by
--         the type signature for:
--           mapOverFocus :: forall a.
--                           (LZ.ListZipper a -> LZ.ListZipper a) -> AppState -> AppState
--         at src/Main.hs:52:1-86
--       Expected type: Maybe (LZ.ListZipper a)
--         Actual type: Maybe (LZ.ListZipper Action)
--     • In the second argument of ‘(<$>)’, namely ‘currActions s’
--       In the ‘currActions’ field of a record
--       In the expression: s {currActions = f <$> currActions s}
--     • Relevant bindings include
--         f :: LZ.ListZipper a -> LZ.ListZipper a
--           (bound at src/Main.hs:53:14)
--         mapOverFocus :: (LZ.ListZipper a -> LZ.ListZipper a)
--                         -> AppState -> AppState
--           (bound at src/Main.hs:53:1)
--    |
-- 54 |   Main.Action -> s {currActions = f <$> currActions s}
--    |                                         ^^^^^^^^^^^^^


-- mapOverFocus :: (forall a. LZ.ListZipper a -> LZ.ListZipper a) -> AppState -> AppState
-- mapOverFocus f s = case focus s of
--   Main.Action -> s {currActions = f <$> currActions s}
--   Catalog -> s {firstCatalogs = f <$> firstCatalogs s}

-- what should be highlighted? The catalog or the actions
data Focus = Action | Catalog | SecondCatalog deriving (Show, Eq)


@@ 296,25 264,17 @@ applySearchFilter s searchText'= ( -- looks like lisp lol
      Nothing -> newActions
      Just toFilter -> LZ.filter (fuzzyMatches searchText' . searchString) toFilter



mapOverFocus :: (forall a. Searchable a => LZ.ListZipper a -> LZ.ListZipper a) -> AppState -> AppState
mapOverFocus f s = case focus s of
  Main.Action -> s {currActions = f <$> currActions s}
  Catalog -> s {firstCatalogs = f <$> firstCatalogs s}
  SecondCatalog -> s {secondCatalogs = f <$> secondCatalogs s}

selectedZipperLeft :: AppState  -> AppState
selectedZipperLeft s = case focus s of
  Main.Action -> s {currActions = fmap LZ.moveLeft (currActions s)}
  Catalog     -> s {firstCatalogs = fmap LZ.moveLeft (firstCatalogs s)}
  SecondCatalog  -> s {secondCatalogs = fmap LZ.moveLeft (secondCatalogs s)}
selectedZipperLeft s = mapOverFocus LZ.moveLeft s

selectedZipperRight :: AppState -> AppState
selectedZipperRight s = case focus s of
  Main.Action -> s {currActions = fmap LZ.moveRight (currActions s)}
  Catalog     -> s {firstCatalogs = fmap LZ.moveRight (firstCatalogs s)}
  SecondCatalog -> s {secondCatalogs = fmap LZ.moveLeft (secondCatalogs s)}
-- TODO: fails to compile, don't know why
-- mapFocus :: Searchable a => AppState -> (LZ.ListZipper a -> LZ.ListZipper a) -> AppState
-- mapFocus s f = case focus s of
--   Catalog -> s {firstCatalogs = firstCatalogs s}
--   Main.Action -> s {currActions = currActions s}
selectedZipperRight s = mapOverFocus LZ.moveRight s

replaceNothingWith :: Maybe (LZ.ListZipper Cataloged) -> Maybe (LZ.ListZipper Action) -> AppState -> AppState
replaceNothingWith catalogZipper actionZipper s = case focus s of


@@ 384,11 344,6 @@ searchCatalog s = LZ.focus <$> firstCatalogs s
searchAction :: AppState -> Maybe Action
searchAction s = LZ.focus <$> currActions s


flipFocus :: Focus -> Focus
flipFocus Main.Action = Catalog
flipFocus Catalog = Main.Action

searchableMatches :: Searchable s => T.Text -> [s] -> [s]
searchableMatches s = filter (fuzzyMatches s . searchString)