Skip to content

Commit

Permalink
Improve formatting other error message types, simplify TreeFilter cod…
Browse files Browse the repository at this point in the history
…e a bit
  • Loading branch information
harris-chris committed Jan 3, 2024
1 parent dcbb80d commit a8c3d30
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 21 deletions.
12 changes: 9 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,17 @@ View [the full documentation](https://harris-chris.github.io/tree-surgeon/)

Tree surgeon is a tool for applying complex (or simple!) filters to directory trees. For example:
```
$ tree-surgeon show-diff -f 'endsWith ".md" (basename file)' -s ~/Documents
$ tree-surgeon tree-diff -f 'endsWith ".md" (basename file)' -s ~/Documents
```
will show a before & after of your `~/Downloads` folder, if all but the markdown (`*.md`) files were filtered out, then
```
$ tree-surgeon to-bash -f 'endsWith ".md" (basename file)' -s ~/Documents | elem parents myProjectFiles
$
$ tree-surgeon to-bash -f 'endsWith ".md" (basename file)' | elem "myProjectFiles" (parents file)' -s ~/Documents
```
will show the same files - but now also including any files that are holdings (at any level) of the `myProjectFiles` folder - as a bash array.

Run
```
$ tree-surgeon --help
```
to see a list of available commands.

11 changes: 6 additions & 5 deletions src/app/Exceptions.hs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ instance TextShow (FilterException a) where
fromText $
"Error: filter expression resolves to " <> expr
<> " which is type " <> showt exprType
<> " but must be of type boolean"
<> " but must be of type Boolean"
showb (DuplicateName _ name) =
fromText $
"Error: name " <> name
Expand Down Expand Up @@ -81,17 +81,18 @@ data OtherException =
instance TextShow OtherException where
showb (CanOnlyFilterDirectory fileName) =
fromText $
"You have asked me to filter the file " <> fileName
<> " but I can only filter directories"
"You have asked me to filter the file: " <> fileName
<> ", but I can only filter directories"
showb (Can'tParse) =
fromText $
"Unable to parse the filter string - did you forget to close a bracket or a list?"
showb (FailedToReadTree name) =
fromText $
"Could not read the following file: " <> name
"I could not read the requested source directory: " <> name
<> ", does it exist?"

instance Show OtherException where
show e = show $ showb e
show e = T.unpack $ toText $ showb e

instance Exception OtherException

Expand Down
6 changes: 5 additions & 1 deletion src/app/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@ main :: IO ()
main = withUtf8 $ tsFilter =<< execParser opts
where
header' = "Tree surgeon: A utility for filtering and manipulating file trees"
progDesc' = "Use --help on a specific command to see more information"
progDesc' =
"Use --help to see a list of commands, or "
<> " COMMAND --help on a specific command to see more information; eg"
<> " tree-surgeon tree --help"
<> " to see help information on the tree command"
fullDesc' = fullDesc <> header header' <> progDesc progDesc'
opts = info
(cliOptsParser <**> helper) fullDesc'
Expand Down
23 changes: 11 additions & 12 deletions src/app/TreeFilter.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,10 @@ toElements' _ _ (Failed name err) = Failed name err

applyFilter :: FileName -> BS.ByteString -> IO (Either [TSException X.Range] (DirTree FData))
applyFilter dirname filterStr = do
anchoredTree <- readDirectoryWith return dirname
let validated = validateTree $ dirTree anchoredTree
let filtered = filterTreeWith filterStr =<< validated
(_ :/ tree) <- readDirectoryWith return dirname
let fDataTree = toElements tree
let validatedTree = validateTree dirname fDataTree
let filtered = filterTreeWith filterStr =<< validatedTree
return filtered

applyFilterWith :: FileName -> (DirTree FData -> IO()) -> BS.ByteString -> IO ()
Expand Down Expand Up @@ -100,13 +101,11 @@ getExcluded typeConstraint origTree filteredTree =
arrayFiltered = toBashArray typeConstraint filteredTree
in filter (\z -> not $ elem z arrayFiltered) arrayOrig

filterTreeWith :: BS.ByteString -> DirTree a -> Either [TSException X.Range] (DirTree FData)
filterTreeWith filterStr tree = do
filterTreeWith :: BS.ByteString -> DirTree FData -> Either [TSException X.Range] (DirTree FData)
filterTreeWith filterStr validatedTree = do
simpleExp <- filterStrToSimpleExp filterStr
let tree' = toElements tree
validated <- validateTree tree'
let matcher = applyExpression simpleExp
filtered <- filterTreeWith' [] matcher validated
filtered <- filterTreeWith' [] matcher validatedTree
return $ fromJust filtered

applyExpression :: IsTrace trc => SimpleExp trc -> FData -> EitherF trc Bool
Expand All @@ -115,12 +114,12 @@ applyExpression expr fData = do
asBool <- convertToBool resolved
return asBool

validateTree :: DirTree a -> Either [TSException X.Range] (DirTree a)
validateTree (File name _) = Left $ [Other $ CanOnlyFilterDirectory $ T.pack name]
validateTree tree =
validateTree :: FileName -> DirTree a -> Either [TSException X.Range] (DirTree a)
validateTree dirName (File _ _) = Left $ [Other $ CanOnlyFilterDirectory $ T.pack dirName]
validateTree dirName tree =
case failures tree of
[] -> Right tree
xs -> Left $ (\fl -> Other $ FailedToReadTree (T.pack $ name fl)) <$> xs
xs -> Left $ (\fl -> Other $ FailedToReadTree (T.pack $ dirName)) <$> xs

filterTreeWith' ::
[T.Text] -> Matcher a -> DirTree FData -> Either [TSException a] (Maybe (DirTree FData))
Expand Down

0 comments on commit a8c3d30

Please sign in to comment.