From 425c2867708bbb43dec1f8d9094c5f8fbaa78038 Mon Sep 17 00:00:00 2001 From: cfhammill Date: Tue, 27 Mar 2018 13:33:53 -0400 Subject: [PATCH 1/3] Add lsdepth, findtree, cmin, and cmax As discussed in #298 --- src/Turtle/Prelude.hs | 57 ++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 56 insertions(+), 1 deletion(-) diff --git a/src/Turtle/Prelude.hs b/src/Turtle/Prelude.hs index 50f3f9c..f1b6a05 100644 --- a/src/Turtle/Prelude.hs +++ b/src/Turtle/Prelude.hs @@ -170,6 +170,7 @@ module Turtle.Prelude ( , ls , lsif , lstree + , lsdepth , cat , grep , grepText @@ -183,6 +184,7 @@ module Turtle.Prelude ( , inplaceSuffix , inplaceEntire , find + , findtree , yes , nl , paste @@ -269,6 +271,8 @@ module Turtle.Prelude ( , PosixCompat.isDirectory , PosixCompat.isSymbolicLink , PosixCompat.isSocket + , cmin + , cmax -- * Headers , WithHeader(..) @@ -305,7 +309,7 @@ import Data.Ord (comparing) import qualified Data.Set as Set import Data.Text (Text, pack, unpack) import Data.Time (NominalDiffTime, UTCTime, getCurrentTime) -import Data.Time.Clock.POSIX (POSIXTime) +import Data.Time.Clock.POSIX (POSIXTime, posixSecondsToUTCTime) import Data.Traversable import qualified Data.Text as Text import qualified Data.Text.IO as Text @@ -1005,6 +1009,29 @@ lstree path = do then return child <|> lstree child else return child + +{- | Stream the recursive descendents of a given directory + between a given minimum and maximum depth +-} +lsdepth :: Int -> Int -> FilePath -> Shell FilePath +lsdepth mn mx path = + lsdepthHelper 1 mn mx path + where + lsdepthHelper :: Int -> Int -> Int -> FilePath -> Shell FilePath + lsdepthHelper depth l u p = + if depth > u + then empty + else do + child <- ls p + isDir <- testdir child + if isDir + then if depth >= l + then return child <|> lsdepthHelper (depth + 1) l u child + else lsdepthHelper (depth + 1) l u child + else if depth >= l + then return child + else empty + {-| Stream all recursive descendents of the given directory This skips any directories that fail the supplied predicate @@ -1635,6 +1662,34 @@ find pattern dir = do file_stat <- lstat file return (not (PosixCompat.isSymbolicLink file_stat)) +-- | Filter a shell of FilePaths according to a given pattern +findtree :: Pattern a -> Shell FilePath -> Shell FilePath +findtree pat files = do + path <- files + Right txt <- return (Filesystem.toText path) + _:_ <- return (match pat txt) + return path + +{- | Check if a file was last modified after a given + timestamp +-} +cmin :: MonadIO io => UTCTime -> FilePath -> io Bool +cmin t file = + (> t) . + posixSecondsToUTCTime . + modificationTime <$> + lstat file + +{- | Check if a file was last modified after a given + timestamp +-} +cmax :: MonadIO io => UTCTime -> FilePath -> io Bool +cmax t file = + (< t) . + posixSecondsToUTCTime . + modificationTime <$> + lstat file + -- | A Stream of @\"y\"@s yes :: Shell Line yes = fmap (\_ -> "y") endless From a165f144e248450aa46f2d0e6da8a604543dc02b Mon Sep 17 00:00:00 2001 From: cfhammill Date: Wed, 28 Mar 2018 20:49:51 -0400 Subject: [PATCH 2/3] Adopt Gabe's suggestion to be more point-full --- src/Turtle/Prelude.hs | 16 +++++++--------- 1 file changed, 7 insertions(+), 9 deletions(-) diff --git a/src/Turtle/Prelude.hs b/src/Turtle/Prelude.hs index f1b6a05..ecc55ad 100644 --- a/src/Turtle/Prelude.hs +++ b/src/Turtle/Prelude.hs @@ -1675,20 +1675,18 @@ findtree pat files = do -} cmin :: MonadIO io => UTCTime -> FilePath -> io Bool cmin t file = - (> t) . - posixSecondsToUTCTime . - modificationTime <$> - lstat file + adapt <$> lstat file + where + adapt x = posixSecondsToUTCTime (modificationTime x) > t -{- | Check if a file was last modified after a given +{- | Check if a file was last modified before a given timestamp -} cmax :: MonadIO io => UTCTime -> FilePath -> io Bool cmax t file = - (< t) . - posixSecondsToUTCTime . - modificationTime <$> - lstat file + adapt <$> lstat file + where + adapt x = posixSecondsToUTCTime (modificationTime x) < t -- | A Stream of @\"y\"@s yes :: Shell Line From bf983a7feff773c6f1324c7c6501f379db0c38cb Mon Sep 17 00:00:00 2001 From: cfhammill Date: Thu, 12 Apr 2018 20:17:50 -0400 Subject: [PATCH 3/3] Convert cmin and cmax to do notation --- src/Turtle/Prelude.hs | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/Turtle/Prelude.hs b/src/Turtle/Prelude.hs index ecc55ad..ccf8a40 100644 --- a/src/Turtle/Prelude.hs +++ b/src/Turtle/Prelude.hs @@ -1674,8 +1674,9 @@ findtree pat files = do timestamp -} cmin :: MonadIO io => UTCTime -> FilePath -> io Bool -cmin t file = - adapt <$> lstat file +cmin t file = do + status <- lstat file + return (adapt status) where adapt x = posixSecondsToUTCTime (modificationTime x) > t @@ -1683,8 +1684,9 @@ cmin t file = timestamp -} cmax :: MonadIO io => UTCTime -> FilePath -> io Bool -cmax t file = - adapt <$> lstat file +cmax t file = do + status <- lstat file + return (adapt status) where adapt x = posixSecondsToUTCTime (modificationTime x) < t