Skip to content

Commit 6eebffa

Browse files
committed
new option to use input file name as basis for output file name
1 parent 1fe3fd8 commit 6eebffa

File tree

5 files changed

+80
-5
lines changed

5 files changed

+80
-5
lines changed

knotools.cabal

+1
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ test-suite tests
4747
TestCommand
4848
TestData
4949
TestParser
50+
TestOutputFileName
5051
build-depends:
5152
base ^>=4.17.2.1
5253
, attoparsec

src/Command.hs

+13-1
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import Parser
1313
import System.FilePath
1414
import System.IO.Streams.Attoparsec.ByteString qualified as SP
1515

16+
class ToCommand a where
17+
command :: a -> [String]
18+
1619
wgetCmd :: Params -> FilePath -> [String]
1720
wgetCmd p f =
1821
[ "wget"
@@ -40,9 +43,18 @@ pdftkCmd p f =
4043
, p ^. input
4144
, "cat"
4245
, "output"
43-
, replaceExtension (p ^. output) "pdf"
46+
, replaceExtension (outFile p (p ^. output)) "pdf"
4447
]
4548

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+
4658
makeWH :: Num a => PageMedia a -> (a, a)
4759
makeWH p =
4860
( p ^. cropRect . topRight . x - p ^. cropRect . bottomLeft . x

src/Params.hs

+34-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ import Lens.Micro.Platform
88
data Params = Params
99
{ _input :: FilePath
1010
, _url :: String
11-
, _output:: FilePath
11+
, _output:: OutputFile
1212
}
1313
deriving (Show)
1414

15+
data OutputFile = OutputFile FilePath | AppendInput
16+
deriving (Show)
17+
1518
makeLenses ''Params
1619

1720
cmdLineParser :: IO Params
@@ -22,6 +25,33 @@ cmdLineParser = execParser opts
2225
parseParams :: Parser Params
2326
parseParams =
2427
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+
)

test/Main.hs

+2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@ module Main (main) where
33
import Test.Tasty
44
import TestParser
55
import TestCommand
6+
import TestOutputFileName
67

78
tests :: TestTree
89
tests =
910
testGroup
1011
"main"
1112
[ testParse
1213
, testCommand
14+
, testOutputFileName
1315
]
1416

1517
main :: IO ()

test/TestOutputFileName.hs

+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
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+
]

0 commit comments

Comments
 (0)