Skip to content

Add Tail module #66

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 72 additions & 0 deletions src/Streamly/Coreutils/Tail.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
-- |
-- Module : Streamly.Coreutils.Tail
-- Copyright : (c) 2022 Composewell Technologies
-- License : Apache-2.0
-- Maintainer : [email protected]
-- Stability : experimental
-- Portability : GHC
--
-- Output the last part of files.

module Streamly.Coreutils.Tail

( tail

-- * Options
, Tail
, follow
, tailN
)
where

import Control.Monad.IO.Class (MonadIO(liftIO))
import Data.Function ((&))
import Streamly.Prelude (SerialT)
import System.IO (IOMode(..), openFile)

import qualified Streamly.FileSystem.Handle as File
import qualified Streamly.Internal.Data.Array as Array
import qualified Streamly.Internal.Data.Stream.IsStream as Stream
import qualified Streamly.Internal.Data.Fold as FL
import qualified Streamly.Internal.Unicode.Stream as Unicode

import Prelude hiding (tail)
import Streamly.Coreutils.Common (Switch(..))

newtype Tail = Tail{tailFollow :: Switch}

defaultConfig :: Tail
defaultConfig = Tail Off

follow :: Switch -> Tail -> Tail
follow opt cfg = cfg {tailFollow = opt}

tailForEver :: Int -> FilePath -> SerialT IO [Char]
tailForEver n fp = do
fh <- liftIO $ openFile fp ReadMode
Stream.concatMap Stream.fromList
$ Stream.repeatM
$ Stream.toList
$ Stream.unfold File.read fh
& Unicode.decodeUtf8
& Unicode.lines FL.toList
& Stream.fold (Array.writeLastN n)
& Stream.map Array.toStream
& Stream.concat
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand what this is doing. Reading the fh in an infinite loop without waiting, that's bad.


tailN :: (Tail -> Tail) -> Int -> FilePath -> SerialT IO [Char]
tailN f n fp = do
fh <- liftIO $ openFile fp ReadMode
let opt = f defaultConfig
case tailFollow opt of
On -> tailForEver n fp
Off ->
Stream.unfold File.read fh
& Unicode.decodeUtf8
& Unicode.lines FL.toList
& Stream.fold (Array.writeLastN n)
& Stream.map Array.toStream
& Stream.concat

tail :: (Tail -> Tail) -> FilePath -> SerialT IO [Char]
tail f = tailN f 10
2 changes: 2 additions & 0 deletions streamly-coreutils.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -92,13 +92,15 @@ library
, unix >= 2.7.0 && < 2.8
, directory >= 1.2.2 && < 1.4
, data-default-class >= 0.1 && < 0.2
, monad-control >= 1.0.3 && < 1.0.4
hs-source-dirs: src
exposed-modules:
Streamly.Coreutils
, Streamly.Coreutils.Common
, Streamly.Coreutils.Cp
, Streamly.Coreutils.FileTest
, Streamly.Coreutils.ShellWords
, Streamly.Coreutils.Tail
, Streamly.Coreutils.Uniq
, Streamly.Coreutils.Which

Expand Down