Skip to content

Commit e897220

Browse files
committed
Inidication that a field is optional for cabal init prompts
1 parent f0d0594 commit e897220

File tree

4 files changed

+43
-32
lines changed

4 files changed

+43
-32
lines changed

cabal-install/src/Distribution/Client/Init/FlagExtractors.hs

+4-4
Original file line numberDiff line numberDiff line change
@@ -220,19 +220,19 @@ simpleProjectPrompt :: Interactive m => InitFlags -> m Bool
220220
simpleProjectPrompt flags = getSimpleProject flags $
221221
promptYesNo
222222
"Should I generate a simple project with sensible defaults"
223-
(Just True)
223+
(DefaultPrompt True)
224224

225225
initializeTestSuitePrompt :: Interactive m => InitFlags -> m Bool
226226
initializeTestSuitePrompt flags = getInitializeTestSuite flags $
227227
promptYesNo
228228
"Should I generate a test suite for the library"
229-
(Just True)
229+
(DefaultPrompt True)
230230

231231
packageTypePrompt :: Interactive m => InitFlags -> m PackageType
232232
packageTypePrompt flags = getPackageType flags $ do
233233
pt <- promptList "What does the package build"
234234
packageTypes
235-
(Just "Executable")
235+
(DefaultPrompt "Executable")
236236
Nothing
237237
False
238238

@@ -256,7 +256,7 @@ testMainPrompt :: Interactive m => m HsFilePath
256256
testMainPrompt = do
257257
fp <- promptList "What is the main module of the test suite?"
258258
[defaultMainIs', "Main.lhs"]
259-
(Just defaultMainIs')
259+
(DefaultPrompt defaultMainIs')
260260
Nothing
261261
True
262262

cabal-install/src/Distribution/Client/Init/Interactive/Command.hs

+16-16
Original file line numberDiff line numberDiff line change
@@ -264,7 +264,7 @@ cabalVersionPrompt :: Interactive m => InitFlags -> m CabalSpecVersion
264264
cabalVersionPrompt flags = getCabalVersion flags $ do
265265
v <- promptList "Please choose version of the Cabal specification to use"
266266
ppVersions
267-
(Just ppDefault)
267+
(DefaultPrompt ppDefault)
268268
(Just takeVersion)
269269
False
270270
-- take just the version numbers for convenience
@@ -301,12 +301,12 @@ packageNamePrompt srcDb flags = getPackageName flags $ do
301301
Flag b -> return $ filePathToPkgName b
302302
NoFlag -> currentDirPkgName
303303

304-
go $ Just defName
304+
go $ DefaultPrompt defName
305305
where
306306
go defName = prompt "Package name" defName >>= \n ->
307307
if isPkgRegistered n
308308
then do
309-
don'tUseName <- promptYesNo (promptOtherNameMsg n) (Just True)
309+
don'tUseName <- promptYesNo (promptOtherNameMsg n) (DefaultPrompt True)
310310
if don'tUseName
311311
then do
312312
putStrLn (inUseMsg n)
@@ -326,7 +326,7 @@ versionPrompt :: Interactive m => InitFlags -> m Version
326326
versionPrompt flags = getVersion flags go
327327
where
328328
go = do
329-
vv <- promptStr "Package version" (Just $ prettyShow defaultVersion)
329+
vv <- promptStr "Package version" (DefaultPrompt $ prettyShow defaultVersion)
330330
case simpleParsec vv of
331331
Nothing -> do
332332
putStrLn
@@ -339,7 +339,7 @@ licensePrompt :: Interactive m => InitFlags -> m SPDX.License
339339
licensePrompt flags = getLicense flags $ do
340340
l <- promptList "Please choose a license"
341341
licenses
342-
Nothing
342+
MandatoryPrompt
343343
Nothing
344344
True
345345

@@ -353,24 +353,24 @@ licensePrompt flags = getLicense flags $ do
353353

354354
authorPrompt :: Interactive m => InitFlags -> m String
355355
authorPrompt flags = getAuthor flags $
356-
promptStr "Author name" Nothing
356+
promptStr "Author name" OptionalPrompt
357357

358358
emailPrompt :: Interactive m => InitFlags -> m String
359359
emailPrompt flags = getEmail flags $
360-
promptStr "Maintainer email" Nothing
360+
promptStr "Maintainer email" OptionalPrompt
361361

362362
homepagePrompt :: Interactive m => InitFlags -> m String
363363
homepagePrompt flags = getHomepage flags $
364-
promptStr "Project homepage URL" Nothing
364+
promptStr "Project homepage URL" OptionalPrompt
365365

366366
synopsisPrompt :: Interactive m => InitFlags -> m String
367367
synopsisPrompt flags = getSynopsis flags $
368-
promptStr "Project synopsis" Nothing
368+
promptStr "Project synopsis" OptionalPrompt
369369

370370
categoryPrompt :: Interactive m => InitFlags -> m String
371371
categoryPrompt flags = getCategory flags $ promptList
372372
"Project category" defaultCategories
373-
(Just "") (Just matchNone) True
373+
(DefaultPrompt "") (Just matchNone) True
374374
where
375375
matchNone s
376376
| null s = "(none)"
@@ -383,7 +383,7 @@ mainFilePrompt flags = getMainFile flags go
383383
go = do
384384
fp <- promptList "What is the main module of the executable"
385385
[defaultMainIs', "Main.lhs"]
386-
(Just defaultMainIs')
386+
(DefaultPrompt defaultMainIs')
387387
Nothing
388388
True
389389

@@ -402,14 +402,14 @@ mainFilePrompt flags = getMainFile flags go
402402

403403
testDirsPrompt :: Interactive m => InitFlags -> m [String]
404404
testDirsPrompt flags = getTestDirs flags $ do
405-
dir <- promptStr "Test directory" (Just defaultTestDir)
405+
dir <- promptStr "Test directory" (DefaultPrompt defaultTestDir)
406406
return [dir]
407407

408408
languagePrompt :: Interactive m => InitFlags -> String -> m Language
409409
languagePrompt flags pkgType = getLanguage flags $ do
410410
lang <- promptList ("Choose a language for your " ++ pkgType)
411411
["Haskell2010", "Haskell98"]
412-
(Just "Haskell2010")
412+
(DefaultPrompt "Haskell2010")
413413
Nothing
414414
True
415415

@@ -428,7 +428,7 @@ noCommentsPrompt :: Interactive m => InitFlags -> m Bool
428428
noCommentsPrompt flags = getNoComments flags $ do
429429
doComments <- promptYesNo
430430
"Add informative comments to each field in the cabal file. (y/n)"
431-
(Just True)
431+
(DefaultPrompt True)
432432

433433
--
434434
-- if --no-comments is flagged, then we choose not to generate comments
@@ -443,7 +443,7 @@ appDirsPrompt :: Interactive m => InitFlags -> m [String]
443443
appDirsPrompt flags = getAppDirs flags $ do
444444
dir <- promptList promptMsg
445445
[defaultApplicationDir, "exe", "src-exe"]
446-
(Just defaultApplicationDir)
446+
(DefaultPrompt defaultApplicationDir)
447447
Nothing
448448
True
449449

@@ -458,7 +458,7 @@ srcDirsPrompt :: Interactive m => InitFlags -> m [String]
458458
srcDirsPrompt flags = getSrcDirs flags $ do
459459
dir <- promptList "Library source directory"
460460
[defaultSourceDir, "lib", "src-lib"]
461-
(Just defaultSourceDir)
461+
(DefaultPrompt defaultSourceDir)
462462
Nothing
463463
True
464464

cabal-install/src/Distribution/Client/Init/Prompt.hs

+13-12
Original file line numberDiff line numberDiff line change
@@ -29,15 +29,15 @@ import Distribution.Client.Init.Types
2929

3030
-- | Create a prompt with optional default value that returns a
3131
-- String.
32-
promptStr :: Interactive m => String -> Maybe String -> m String
32+
promptStr :: Interactive m => String -> DefaultPrompt String -> m String
3333
promptStr = promptDefault Right id
3434

3535
-- | Create a yes/no prompt with optional default value.
3636
promptYesNo
3737
:: Interactive m
3838
=> String
3939
-- ^ prompt message
40-
-> Maybe Bool
40+
-> DefaultPrompt Bool
4141
-- ^ optional default value
4242
-> m Bool
4343
promptYesNo =
@@ -53,16 +53,17 @@ promptYesNo =
5353

5454
-- | Create a prompt with optional default value that returns a value
5555
-- of some Text instance.
56-
prompt :: (Interactive m, Parsec t, Pretty t) => String -> Maybe t -> m t
56+
prompt :: (Interactive m, Parsec t, Pretty t) => String -> DefaultPrompt t -> m t
5757
prompt = promptDefault eitherParsec prettyShow
5858

5959
-- | Create a prompt from a prompt string and a String representation
6060
-- of an optional default value.
61-
mkDefPrompt :: String -> Maybe String -> String
61+
mkDefPrompt :: String -> DefaultPrompt String -> String
6262
mkDefPrompt msg def = msg ++ "?" ++ format def
6363
where
64-
format Nothing = " "
65-
format (Just s) = " [default: " ++ s ++ "] "
64+
format MandatoryPrompt = " "
65+
format OptionalPrompt = " [optional] "
66+
format (DefaultPrompt s) = " [default: " ++ s ++ "] "
6667

6768
-- | Create a prompt from a list of strings
6869
promptList
@@ -71,7 +72,7 @@ promptList
7172
-- ^ prompt
7273
-> [String]
7374
-- ^ choices
74-
-> Maybe String
75+
-> DefaultPrompt String
7576
-- ^ optional default value
7677
-> Maybe (String -> String)
7778
-- ^ modify the default value to present in-prompt
@@ -85,7 +86,7 @@ promptList msg choices def modDef hasOther = do
8586

8687
-- Output nicely formatted list of options
8788
for_ prettyChoices $ \(i,c) -> do
88-
let star = if Just c == def
89+
let star = if DefaultPrompt c == def
8990
then "*"
9091
else " "
9192

@@ -125,13 +126,13 @@ promptList msg choices def modDef hasOther = do
125126

126127
input <- getLine
127128
case def of
128-
Just d | null input -> return d
129+
DefaultPrompt d | null input -> return d
129130
_ -> case readMaybe input of
130131
Nothing -> invalidChoice input
131132
Just n
132133
| n > 0, n <= numChoices -> return $ choices !! (n-1)
133134
| n == numChoices + 1, hasOther ->
134-
promptStr "Please specify" Nothing
135+
promptStr "Please specify" OptionalPrompt
135136
| otherwise -> invalidChoice (show n)
136137

137138
-- | Create a prompt with an optional default value.
@@ -143,14 +144,14 @@ promptDefault
143144
-- ^ pretty-printer
144145
-> String
145146
-- ^ prompt message
146-
-> Maybe t
147+
-> (DefaultPrompt t)
147148
-- ^ optional default value
148149
-> m t
149150
promptDefault parse pprint msg def = do
150151
putStr $ mkDefPrompt msg (pprint <$> def)
151152
input <- getLine
152153
case def of
153-
Just d | null input -> return d
154+
DefaultPrompt d | null input -> return d
154155
_ -> case parse input of
155156
Right t -> return t
156157
Left err -> do

cabal-install/src/Distribution/Client/Init/Types.hs

+10
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ module Distribution.Client.Init.Types
4545
, ProjectSettings(..)
4646
-- * Formatters
4747
, FieldAnnotation(..)
48+
-- * Other conveniences
49+
, DefaultPrompt(..)
4850
) where
4951

5052

@@ -422,6 +424,14 @@ type IsLiterate = Bool
422424
--
423425
type IsSimple = Bool
424426

427+
-- | Defines whether or not a prompt will have a default value,
428+
-- is optional, or is mandatory.
429+
data DefaultPrompt t
430+
= DefaultPrompt t
431+
| OptionalPrompt
432+
| MandatoryPrompt
433+
deriving (Eq, Functor)
434+
425435
-- -------------------------------------------------------------------- --
426436
-- Field annotation for pretty formatters
427437

0 commit comments

Comments
 (0)