summaryrefslogtreecommitdiff
path: root/src/Data
diff options
context:
space:
mode:
authorSam Truzjan <pxqr.sta@gmail.com>2013-11-06 22:03:51 +0400
committerSam Truzjan <pxqr.sta@gmail.com>2013-11-06 22:03:51 +0400
commit068751854cc6c111bf4bec14802fb2552c0a26bf (patch)
treed858150267d3902427ac61fc60d5b7ef8a36d642 /src/Data
parent5ffd1432dc947175787a6b616a673c9c3d49ffb8 (diff)
Add ppLayoutInfo function
Diffstat (limited to 'src/Data')
-rw-r--r--src/Data/Torrent/Layout.hs41
1 files changed, 36 insertions, 5 deletions
diff --git a/src/Data/Torrent/Layout.hs b/src/Data/Torrent/Layout.hs
index 4be108c2..84085a92 100644
--- a/src/Data/Torrent/Layout.hs
+++ b/src/Data/Torrent/Layout.hs
@@ -12,6 +12,9 @@
12{-# LANGUAGE StandaloneDeriving #-} 12{-# LANGUAGE StandaloneDeriving #-}
13{-# LANGUAGE GeneralizedNewtypeDeriving #-} 13{-# LANGUAGE GeneralizedNewtypeDeriving #-}
14{-# LANGUAGE DeriveDataTypeable #-} 14{-# LANGUAGE DeriveDataTypeable #-}
15{-# LANGUAGE DeriveFunctor #-}
16{-# LANGUAGE DeriveFoldable #-}
17{-# LANGUAGE DeriveTraversable #-}
15{-# LANGUAGE TemplateHaskell #-} 18{-# LANGUAGE TemplateHaskell #-}
16{-# OPTIONS -fno-warn-orphans #-} 19{-# OPTIONS -fno-warn-orphans #-}
17module Data.Torrent.Layout 20module Data.Torrent.Layout
@@ -21,6 +24,7 @@ module Data.Torrent.Layout
21 24
22 -- * Single file info 25 -- * Single file info
23 , FileInfo (..) 26 , FileInfo (..)
27 , ppFileInfo
24 28
25 -- ** Lens 29 -- ** Lens
26 , fileLength 30 , fileLength
@@ -29,6 +33,8 @@ module Data.Torrent.Layout
29 33
30 -- * File layout 34 -- * File layout
31 , LayoutInfo (..) 35 , LayoutInfo (..)
36 , ppLayoutInfo
37 , joinFilePath
32 38
33 -- ** Lens 39 -- ** Lens
34 , singleFile 40 , singleFile
@@ -65,13 +71,18 @@ import Data.BEncode
65import Data.BEncode.Types 71import Data.BEncode.Types
66import Data.ByteString as BS 72import Data.ByteString as BS
67import Data.ByteString.Char8 as BC 73import Data.ByteString.Char8 as BC
68import Data.Char 74import Data.Char as Char
75import Data.Foldable as F
69import Data.List as L 76import Data.List as L
77import Data.Text as T
78import Data.Text.Encoding as T
70import Data.Typeable 79import Data.Typeable
80import Text.PrettyPrint as PP
71import System.FilePath 81import System.FilePath
72import System.Posix.Types 82import System.Posix.Types
73 83
74import Data.Torrent.Block 84import Data.Torrent.Block
85import Data.Torrent.InfoHash
75 86
76 87
77{----------------------------------------------------------------------- 88{-----------------------------------------------------------------------
@@ -111,9 +122,11 @@ data FileInfo a = FileInfo {
111 -- 122 --
112 -- > ["dir1", "dir2", "file.ext"] 123 -- > ["dir1", "dir2", "file.ext"]
113 -- 124 --
114 } deriving (Show, Read, Eq, Typeable) 125 } deriving (Show, Read, Eq, Typeable
126 , Functor, Foldable
127 )
115 128
116$(deriveJSON (L.map toLower . L.dropWhile isLower) ''FileInfo) 129$(deriveJSON (L.map Char.toLower . L.dropWhile isLower) ''FileInfo)
117 130
118makeLensesFor 131makeLensesFor
119 [ ("fiLength", "fileLength") 132 [ ("fiLength", "fileLength")
@@ -162,6 +175,19 @@ instance BEncode (FileInfo ByteString) where
162 fromBEncode = fromDict getFileInfoSingle 175 fromBEncode = fromDict getFileInfoSingle
163 {-# INLINE fromBEncode #-} 176 {-# INLINE fromBEncode #-}
164 177
178-- | Format 'FileInfo' in human-readable form.
179ppFileInfo :: FileInfo ByteString -> Doc
180ppFileInfo FileInfo {..} =
181 "Path: " <> text (T.unpack (T.decodeUtf8 fiName))
182 $$ "Size: " <> text (show fiLength)
183 $$ maybe PP.empty ppMD5 fiMD5Sum
184 where
185 ppMD5 md5 = "MD5 : " <> text (show (InfoHash md5))
186
187-- | Join file path.
188joinFilePath :: FileInfo [ByteString] -> FileInfo ByteString
189joinFilePath = fmap (BS.intercalate "/")
190
165{----------------------------------------------------------------------- 191{-----------------------------------------------------------------------
166-- Original torrent file layout info 192-- Original torrent file layout info
167-----------------------------------------------------------------------} 193-----------------------------------------------------------------------}
@@ -186,7 +212,7 @@ data LayoutInfo
186 , liDirName :: !ByteString 212 , liDirName :: !ByteString
187 } deriving (Show, Read, Eq, Typeable) 213 } deriving (Show, Read, Eq, Typeable)
188 214
189$(deriveJSON (L.map toLower . L.dropWhile isLower) ''LayoutInfo) 215$(deriveJSON (L.map Char.toLower . L.dropWhile isLower) ''LayoutInfo)
190 216
191makeLensesFor 217makeLensesFor
192 [ ("liFile" , "singleFile" ) 218 [ ("liFile" , "singleFile" )
@@ -216,6 +242,11 @@ instance BEncode LayoutInfo where
216 toBEncode = toDict . (`putLayoutInfo` endDict) 242 toBEncode = toDict . (`putLayoutInfo` endDict)
217 fromBEncode = fromDict getLayoutInfo 243 fromBEncode = fromDict getLayoutInfo
218 244
245-- | Format 'LayoutInfo' in human readable form.
246ppLayoutInfo :: LayoutInfo -> Doc
247ppLayoutInfo SingleFile {..} = ppFileInfo liFile
248ppLayoutInfo MultiFile {..} = vcat $ L.map (ppFileInfo . joinFilePath) liFiles
249
219-- | Test if this is single file torrent. 250-- | Test if this is single file torrent.
220isSingleFile :: LayoutInfo -> Bool 251isSingleFile :: LayoutInfo -> Bool
221isSingleFile SingleFile {} = True 252isSingleFile SingleFile {} = True
@@ -237,7 +268,7 @@ suggestedName MultiFile {..} = liDirName
237-- | Find sum of sizes of the all torrent files. 268-- | Find sum of sizes of the all torrent files.
238contentLength :: LayoutInfo -> FileSize 269contentLength :: LayoutInfo -> FileSize
239contentLength SingleFile { liFile = FileInfo {..} } = fiLength 270contentLength SingleFile { liFile = FileInfo {..} } = fiLength
240contentLength MultiFile { liFiles = tfs } = sum (L.map fiLength tfs) 271contentLength MultiFile { liFiles = tfs } = L.sum (L.map fiLength tfs)
241 272
242-- | Get number of all files in torrent. 273-- | Get number of all files in torrent.
243fileCount :: LayoutInfo -> Int 274fileCount :: LayoutInfo -> Int