diff options
author | Sam Truzjan <pxqr.sta@gmail.com> | 2013-11-06 22:03:51 +0400 |
---|---|---|
committer | Sam Truzjan <pxqr.sta@gmail.com> | 2013-11-06 22:03:51 +0400 |
commit | 068751854cc6c111bf4bec14802fb2552c0a26bf (patch) | |
tree | d858150267d3902427ac61fc60d5b7ef8a36d642 /src/Data | |
parent | 5ffd1432dc947175787a6b616a673c9c3d49ffb8 (diff) |
Add ppLayoutInfo function
Diffstat (limited to 'src/Data')
-rw-r--r-- | src/Data/Torrent/Layout.hs | 41 |
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 #-} |
17 | module Data.Torrent.Layout | 20 | module 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 | |||
65 | import Data.BEncode.Types | 71 | import Data.BEncode.Types |
66 | import Data.ByteString as BS | 72 | import Data.ByteString as BS |
67 | import Data.ByteString.Char8 as BC | 73 | import Data.ByteString.Char8 as BC |
68 | import Data.Char | 74 | import Data.Char as Char |
75 | import Data.Foldable as F | ||
69 | import Data.List as L | 76 | import Data.List as L |
77 | import Data.Text as T | ||
78 | import Data.Text.Encoding as T | ||
70 | import Data.Typeable | 79 | import Data.Typeable |
80 | import Text.PrettyPrint as PP | ||
71 | import System.FilePath | 81 | import System.FilePath |
72 | import System.Posix.Types | 82 | import System.Posix.Types |
73 | 83 | ||
74 | import Data.Torrent.Block | 84 | import Data.Torrent.Block |
85 | import 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 | ||
118 | makeLensesFor | 131 | makeLensesFor |
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. | ||
179 | ppFileInfo :: FileInfo ByteString -> Doc | ||
180 | ppFileInfo 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. | ||
188 | joinFilePath :: FileInfo [ByteString] -> FileInfo ByteString | ||
189 | joinFilePath = 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 | ||
191 | makeLensesFor | 217 | makeLensesFor |
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. | ||
246 | ppLayoutInfo :: LayoutInfo -> Doc | ||
247 | ppLayoutInfo SingleFile {..} = ppFileInfo liFile | ||
248 | ppLayoutInfo MultiFile {..} = vcat $ L.map (ppFileInfo . joinFilePath) liFiles | ||
249 | |||
219 | -- | Test if this is single file torrent. | 250 | -- | Test if this is single file torrent. |
220 | isSingleFile :: LayoutInfo -> Bool | 251 | isSingleFile :: LayoutInfo -> Bool |
221 | isSingleFile SingleFile {} = True | 252 | isSingleFile 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. |
238 | contentLength :: LayoutInfo -> FileSize | 269 | contentLength :: LayoutInfo -> FileSize |
239 | contentLength SingleFile { liFile = FileInfo {..} } = fiLength | 270 | contentLength SingleFile { liFile = FileInfo {..} } = fiLength |
240 | contentLength MultiFile { liFiles = tfs } = sum (L.map fiLength tfs) | 271 | contentLength 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. |
243 | fileCount :: LayoutInfo -> Int | 274 | fileCount :: LayoutInfo -> Int |