~jackwines/fast-bronze

ref: 93636f0910a6e327d6530e2d212388c2e5597240 fast-bronze/src/Catalog.hs -rw-r--r-- 3.1 KiB
93636f09 — Jack Wines more CI fixes 1 year, 1 month ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
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,
  searchDir :: FilePath
                 } deriving (Show)
data Action = Action {
  actionSearchString :: T.Text,
  ioAction :: IOAction
  }

data IOAction = OneParam (Cataloged -> IO ()) | TwoParam (Cataloged -> Cataloged -> IO ())

data Cataloged = Cataloged {
  inputType :: InputType,
  dirPath ::  T.Text
  } deriving (Show)

data InputType = Folder | Text | Other deriving (Show, Eq)

instance Searchable Action where
  searchString = actionSearchString

instance Searchable Cataloged where
  searchString = T.toLower . T.pack . reverse . takeWhile (/= '/') . reverse . T.unpack . dirPath

instance Show Action where
  show = T.unpack . searchString

instance Searchable T.Text where
  searchString = id

class Searchable s where
  searchString :: s -> T.Text

getCatalog :: IO [Cataloged]
getCatalog = do
  absHomeDir <- D.getHomeDirectory
  searchCatalogItem SearchDir {
  dirDepth = 1,
  searchDir = absHomeDir
  }

-- recursively crawl a directory to the desired depth
searchCatalogItem :: SearchDir -> IO [Cataloged]
searchCatalogItem (SearchDir depth dir) = do
  isDir <- P.isDirectory <$> P.getFileStatus dir
  dir' <- D.makeAbsolute dir
  let currCatalog = dirToCatalog dir' isDir
  case (depth, isDir) of
    (0, _) -> return [currCatalog]
    (_, False) -> return [currCatalog]
    (_, True) -> do
      childPaths <- D.listDirectory dir
      childCatalogs :: [Cataloged] <- mconcat <$> mapM recSearchCatalog childPaths
      return $ currCatalog : childCatalogs
  where
    recSearchCatalog :: FilePath -> IO [Cataloged]
    recSearchCatalog x = searchCatalogItem . SearchDir (pred depth) $ (dir ++ "/" ++ x)



dirToCatalog :: FilePath -> Bool -> Cataloged
dirToCatalog dir isDir =
  Cataloged {
    inputType = if isDir then Folder else Other,
    dirPath = T.pack dir
         }

-- 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

getActions :: Cataloged -> [Action]
getActions cataloged =
  [
    Action {
      actionSearchString = "move to trash",
      ioAction = OneParam $ \cataloged -> P.executeFile "mv" True [T.unpack . dirPath $ cataloged, "~/.Trash"] Nothing
    },
    Action {
      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
    }
  ]