Updated documentation

This commit is contained in:
Gabriel Gonzalez 2015-01-19 18:41:20 -08:00
parent b35121f283
commit cf411e4796

View file

@ -7,45 +7,48 @@
-- Example one-liners: -- Example one-liners:
-- --
-- >>> :set -XOverloadedStrings -- >>> :set -XOverloadedStrings
-- >>>
-- >>> -- `list` displays all values in a `Shell` stream
-- >>> cd "/usr" -- >>> cd "/usr"
-- >>> pwd -- >>> pwd
-- FilePath "/usr" -- FilePath "/usr"
-- >>> list (ls ".") -- >>> -- `list` displays all values in a `Shell` stream
-- FilePath "./lib" -- >>> list (limit 3 (ls "lib"))
-- FilePath "./src" -- FilePath "lib/gnome-screensaver"
-- FilePath "./sbin" -- FilePath "lib/libplist.so.1.1.8"
-- FilePath "./include", -- FilePath "lib/tracker"
-- FilePath "./share"
-- FilePath "./games"
-- FilePath "./local"
-- FilePath "./bin"
-- >>> list (find "Browser.py" "lib") -- >>> list (find "Browser.py" "lib")
-- FilePath "lib/python3.2/idlelib/ObjectBrowser.py" -- FilePath "lib/python3.2/idlelib/ObjectBrowser.py"
-- FilePath "lib/python3.2/idlelib/PathBrowser.py" -- FilePath "lib/python3.2/idlelib/PathBrowser.py"
-- FilePath "lib/python3.2/idlelib/RemoteObjectBrowser.py" -- FilePath "lib/python3.2/idlelib/RemoteObjectBrowser.py"
-- FilePath "lib/python3.2/idlelib/ClassBrowser.py" -- FilePath "lib/python3.2/idlelib/ClassBrowser.py"
-- >>> -- >>> -- Use `fold` to reduce the output of a `Shell` stream
-- >>> -- Use `fold` to fold the output of a `Shell` stream
-- >>> import qualified Control.Foldl as Fold -- >>> import qualified Control.Foldl as Fold
-- >>> fold (ls ".") Fold.null -- >>> fold (ls "lib") Fold.length
-- False -- 846
-- >>> fold (lstree ".") Fold.length
-- 301966
-- >>> fold (find "Browser.py" "lib") Fold.head -- >>> fold (find "Browser.py" "lib") Fold.head
-- Just (FilePath "lib/python3.2/idlelib/ObjectBrowser.py") -- FilePath "lib/python3.2/idlelib/ObjectBrowser.py"
-- >>>
-- >>> -- `sh` runs a `Shell` only for its effects, discarding any output -- >>> -- `sh` runs a `Shell` only for its effects, discarding any output
-- >>> cd "/tmp" -- >>> cd "/tmp"
-- >>> sh (fileout "foo.txt" ("123" <|> "456" <|> "789")) -- >>> sh (fileout "foo.txt" ("123" <|> "456" <|> "ABC"))
-- >>> realpath "foo.txt"
-- FilePath "/tmp/foo.txt"
-- >>> sh (stdout (filein "foo.txt")) -- >>> sh (stdout (filein "foo.txt"))
-- 123 -- 123
-- 456 -- 456
-- 789 -- ABC
-- >>> sh (stdout (grep ("1" <|> "8") (filein "foo.txt"))) -- >> -- Commands like `grep`, `sed` and `find` accept arbitrary `Pattern`s
-- >>> sh (stdout (grep ("1" <|> "B") (filein "foo.txt")))
-- 123 -- 123
-- 789 -- ABC
-- >>> let exclaim = fmap (<> "!") (plus digit)
-- >>> sh (stdout (sed exclaim (filein "foo.txt")))
-- 123!
-- 456!
-- ABC
-- >>> testfile "foo.txt"
-- True
-- >>> rm "foo.txt"
-- >>> testfile "foo.txt"
-- False
-- --
-- You can also build up more sophisticated `Shell` programs using @do@ -- You can also build up more sophisticated `Shell` programs using @do@
-- notation: -- notation:
@ -57,17 +60,16 @@
-- > main = sh example -- > main = sh example
-- > -- >
-- > example = do -- > example = do
-- > -- Read in lines containing "bar" from "files1.txt" and "files2.txt" -- > -- Read in file names from "files1.txt" and "files2.txt"
-- > -- and interpret them as files -- > fileStr <- filein "files1.txt" <|> filein "files2.txt"
-- > fileStr <- grep "bar" (filein "files1.txt" <|> filein "files2.txt")
-- > let file = fromText fileStr -- > let file = fromText fileStr
-- > -- >
-- > -- Stream the file to standard output if it exists -- > -- Stream each file to standard output only if the file exists
-- > exists <- liftIO (testfile file) -- > True <- liftIO (testfile file)
-- > stdout (if exists then filein file else empty) -- > stdout (filein file)
-- --
-- The above program will stream in constant space, bringing no more than two -- See "Turtle.Tutorial" for an extended tutorial explaining how to use this
-- lines into memory at any time. -- library in greater detail.
module Turtle.Prelude ( module Turtle.Prelude (
-- * IO -- * IO
@ -383,7 +385,11 @@ grep pattern s = do
_:_ <- return (inside pattern txt) _:_ <- return (inside pattern txt)
return txt return txt
-- | Replace all occurrences of a `Pattern` with its `Text` result {-| Replace all occurrences of a `Pattern` with its `Text` result
Warning: Do not use a `Pattern` that matches the empty string, since it will
match an infinite number of times
-}
sed :: Pattern Text -> Shell Text -> Shell Text sed :: Pattern Text -> Shell Text -> Shell Text
sed pattern s = do sed pattern s = do
let pattern' = fmap Text.concat (many (pattern <|> selfless (plus anyChar))) let pattern' = fmap Text.concat (many (pattern <|> selfless (plus anyChar)))