Browse Source

Added the NoItems constructor as a possible solution to empty menus

tags/v0.2.2.0
Peter J. Jones 4 years ago
parent
commit
be7ad73611
3 changed files with 23 additions and 11 deletions
  1. 5
    0
      CHANGES
  2. 1
    1
      byline.cabal
  3. 17
    10
      src/System/Console/Byline/Menu.hs

+ 5
- 0
CHANGES View File

@@ -3,6 +3,11 @@
#+title: Version History
#+startup: showall

* 0.2.0.0 (Pending)

- Added the NoItems constructor for Choice to deal with menus that
are asked to display an empty list.

* 0.1.0.0 (May 19, 2015)

- Initial release.

+ 1
- 1
byline.cabal View File

@@ -1,6 +1,6 @@
--------------------------------------------------------------------------------
name: byline
version: 0.1.0.0
version: 0.2.0.0
synopsis: Library for creating command-line interfaces (colors, menus, etc.)
homepage: http://github.com/pjones/byline
bug-reports: http://github.com/pjones/byline/issues

+ 17
- 10
src/System/Console/Byline/Menu.hs View File

@@ -64,7 +64,8 @@ data Menu a = Menu
--------------------------------------------------------------------------------
-- | A type representing the choice made by a user while working with
-- a menu.
data Choice a = Match a -- ^ User picked a menu item.
data Choice a = NoItems -- ^ Menu has no items to choose from.
| Match a -- ^ User picked a menu item.
| Other Text -- ^ User entered text that doesn't match an item.
deriving Show

@@ -187,16 +188,22 @@ askWithMenu :: (MonadIO m)
-> Byline m (Choice a)
askWithMenu m prompt = do
currCompFunc <- Reader.asks compFunc >>= liftIO . readIORef
let firstItem = Text.strip $ renderText Plain (menuItemPrefix m 1)

-- Use the default completion function for menus, but not if another
-- completion function is already active.
withCompletionFunc (fromMaybe (defaultCompFunc m) currCompFunc) $ do
prefixes <- displayMenu
answer <- ask prompt (Just firstItem)
return (menuMatcher m m prefixes answer)
if null (menuItems m)
then return NoItems
else go currCompFunc

where
-- Use the default completion function for menus, but not if another
-- completion function is already active.
go comp = withCompletionFunc (fromMaybe (defaultCompFunc m) comp) $ do
prefixes <- displayMenu
answer <- ask prompt (Just firstItem)
return (menuMatcher m m prefixes answer)

-- The default menu item.
firstItem = Text.strip $ renderText Plain (menuItemPrefix m 1)

-- Print the entire menu.
displayMenu = do
case menuBanner m of
@@ -239,5 +246,5 @@ askWithMenuRepeatedly m prompt errprompt = go m
answer <- askWithMenu config prompt

case answer of
Match _ -> return answer
_ -> go (config {menuBeforePrompt = Just errprompt})
Other _ -> go (config {menuBeforePrompt = Just errprompt})
_ -> return answer

Loading…
Cancel
Save