File tree 5 files changed +80
-5
lines changed
5 files changed +80
-5
lines changed Original file line number Diff line number Diff line change @@ -47,6 +47,7 @@ test-suite tests
47
47
TestCommand
48
48
TestData
49
49
TestParser
50
+ TestOutputFileName
50
51
build-depends :
51
52
base ^>= 4.17.2.1
52
53
, attoparsec
Original file line number Diff line number Diff line change @@ -13,6 +13,9 @@ import Parser
13
13
import System.FilePath
14
14
import System.IO.Streams.Attoparsec.ByteString qualified as SP
15
15
16
+ class ToCommand a where
17
+ command :: a -> [String ]
18
+
16
19
wgetCmd :: Params -> FilePath -> [String ]
17
20
wgetCmd p f =
18
21
[ " wget"
@@ -40,9 +43,18 @@ pdftkCmd p f =
40
43
, p ^. input
41
44
, " cat"
42
45
, " output"
43
- , replaceExtension (p ^. output) " pdf"
46
+ , replaceExtension (outFile p (p ^. output) ) " pdf"
44
47
]
45
48
49
+ -- todo: fixme. dont pass in params and an elem from params
50
+ -- maybe use derived lens
51
+ outFile :: Params -> OutputFile -> FilePath
52
+ outFile _ (OutputFile f) = f
53
+ outFile p AppendInput = newFileName (p^. input) " new"
54
+
55
+ newFileName :: FilePath -> String -> FilePath
56
+ newFileName f e = takeBaseName f <.> e <.> takeExtension f
57
+
46
58
makeWH :: Num a => PageMedia a -> (a , a )
47
59
makeWH p =
48
60
( p ^. cropRect . topRight . x - p ^. cropRect . bottomLeft . x
Original file line number Diff line number Diff line change @@ -8,10 +8,13 @@ import Lens.Micro.Platform
8
8
data Params = Params
9
9
{ _input :: FilePath
10
10
, _url :: String
11
- , _output :: FilePath
11
+ , _output :: OutputFile
12
12
}
13
13
deriving (Show )
14
14
15
+ data OutputFile = OutputFile FilePath | AppendInput
16
+ deriving (Show )
17
+
15
18
makeLenses ''Params
16
19
17
20
cmdLineParser :: IO Params
@@ -22,6 +25,33 @@ cmdLineParser = execParser opts
22
25
parseParams :: Parser Params
23
26
parseParams =
24
27
Params
25
- <$> strArgument (metavar " <INPUT>" <> help " pdf file for processing" )
26
- <*> strOption (metavar " <URL>" <> short ' u' <> long " url" <> help " url for new cover" )
27
- <*> strOption (metavar " <OUTPUT>" <> short ' o' <> long " output" <> help " output file for new pdf" )
28
+ <$> strArgument
29
+ ( metavar " <INPUT>"
30
+ <> help " pdf file for processing"
31
+ )
32
+ <*> strOption
33
+ ( metavar " <URL>"
34
+ <> short ' u'
35
+ <> long " url"
36
+ <> help " url for new cover"
37
+ )
38
+ <*> (fileOutput <|> defaultOutput)
39
+
40
+ fileOutput :: Parser OutputFile
41
+ fileOutput =
42
+ OutputFile
43
+ <$> strOption
44
+ ( metavar " <OUTPUT>"
45
+ <> short ' o'
46
+ <> long " output"
47
+ <> help " output file for new pdf"
48
+ )
49
+
50
+ defaultOutput :: Parser OutputFile
51
+ defaultOutput =
52
+ flag'
53
+ AppendInput
54
+ ( long " append"
55
+ <> short ' a'
56
+ <> help " use input file name as basis for output file name"
57
+ )
Original file line number Diff line number Diff line change @@ -3,13 +3,15 @@ module Main (main) where
3
3
import Test.Tasty
4
4
import TestParser
5
5
import TestCommand
6
+ import TestOutputFileName
6
7
7
8
tests :: TestTree
8
9
tests =
9
10
testGroup
10
11
" main"
11
12
[ testParse
12
13
, testCommand
14
+ , testOutputFileName
13
15
]
14
16
15
17
main :: IO ()
Original file line number Diff line number Diff line change
1
+ module TestOutputFileName (testOutputFileName ) where
2
+
3
+ import Test.Tasty
4
+ import Test.Tasty.HUnit
5
+ import Command
6
+
7
+ testOutputFileName :: TestTree
8
+ testOutputFileName = testGroup " output file name" [tests]
9
+
10
+ tests :: TestTree
11
+ tests=
12
+ testGroup
13
+ " filename"
14
+ [
15
+ let sut = " asdf.jpg"
16
+ got = newFileName sut " new"
17
+ wot = " asdf.new.jpg"
18
+ in
19
+ testCase " " $ wot @?= got
20
+ , let sut = " asdf"
21
+ got = newFileName sut " new"
22
+ wot = " asdf.new"
23
+ in
24
+ testCase " " $ wot @?= got
25
+ , let sut = " asdf.new.jpg"
26
+ got = newFileName sut " new"
27
+ wot = " asdf.new.new.jpg"
28
+ in
29
+ testCase " " $ wot @?= got
30
+ ]
You can’t perform that action at this time.
0 commit comments