packages feed

simple-prompt (empty) → 0.1.0

raw patch · 5 files changed

+135/−0 lines, 5 filesdep +basedep +extra

Dependencies added: base, extra

Files

+ ChangeLog.md view
@@ -0,0 +1,4 @@+# Revision history for simple-prompt++## 0.1.0 (2023-04-02)+- initial release with prompt, prompt_, yesno functions
+ LICENSE view
@@ -0,0 +1,30 @@+Copyright (c) 2023, Jens Petersen++All rights reserved.++Redistribution and use in source and binary forms, with or without+modification, are permitted provided that the following conditions are met:++    * Redistributions of source code must retain the above copyright+      notice, this list of conditions and the following disclaimer.++    * Redistributions in binary form must reproduce the above+      copyright notice, this list of conditions and the following+      disclaimer in the documentation and/or other materials provided+      with the distribution.++    * Neither the name of Jens Petersen nor the names of other+      contributors may be used to endorse or promote products derived+      from this software without specific prior written permission.++THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS+"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT+LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR+A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT+OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,+SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT+LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,+DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY+THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT+(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE+OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ README.md view
@@ -0,0 +1,10 @@+# simple-prompt++A little library for commandline text prompts for user input.++- `prompt`: return a String+- `prompt_` ignore input+- `yesno` expects y/n answer++Currently assumes unix since it reads from /dev/tty for fresh stdin,+but it could probably easily be extended to work on Windows.
+ simple-prompt.cabal view
@@ -0,0 +1,52 @@+name:                simple-prompt+version:             0.1.0+synopsis:            Simple commandline text prompt functions+description:+        The library provides prompt functions for reading user input:+        prompt returns the input given, prompt_ ignores the input,+        and yesno asks for confirmation.+license:             BSD3+license-file:        LICENSE+author:              Jens Petersen <juhpetersen@gmail.com>+maintainer:          Jens Petersen <juhpetersen@gmail.com>+copyright:           2023  Jens Petersen <juhpetersen@gmail.com>+category:            System+homepage:            https://github.com/juhp/simple-prompt+bug-reports:         https://github.com/juhp/simple-prompt/issues+build-type:          Simple+extra-doc-files:     README.md+                     ChangeLog.md+cabal-version:       2.0+tested-with:         GHC == 8.6.5+                     || == 8.8.4+                     || == 8.10.7+                     || == 9.0.2+                     || == 9.2.7+                     || == 9.4.4++source-repository head+  type:                git+  location:            https://github.com/juhp/simple-prompt.git++library+  build-depends:       base < 5,+                       extra++  default-language:    Haskell2010+  exposed-modules:     SimplePrompt+  hs-source-dirs:      src++  ghc-options:         -Wall+  if impl(ghc >= 8.0)+    ghc-options:       -Wcompat+                       -Widentities+                       -Wincomplete-uni-patterns+                       -Wincomplete-record-updates+                       -Wredundant-constraints+  if impl(ghc >= 8.2)+    ghc-options:       -fhide-source-paths+  if impl(ghc >= 8.4)+    ghc-options:       -Wmissing-export-lists+                       -Wpartial-fields+  if impl(ghc >= 8.10)+    ghc-options:       -Wunused-packages
+ src/SimplePrompt.hs view
@@ -0,0 +1,39 @@+module SimplePrompt (+  prompt,+  prompt_,+  yesno+  ) where++import Control.Monad (void)+import Data.Bool (bool)+import Data.Char (isPrint)+import Data.List.Extra (lower, trim)++import System.IO++prompt :: String -> IO String+prompt s = do+  putStr $ s ++ ": "+  tty <- openFile "/dev/tty" ReadMode+  inp <- hGetLine tty+  if all isPrint inp+    then return inp+    else do+    putStrLn $+      "input rejected because of unprintable character(s): '" +++      show inp ++ "'"+    prompt s++prompt_ :: String -> IO ()+prompt_ = void <$> prompt++yesno :: Maybe Bool -> String -> IO Bool+yesno mdefault desc = do+  inp <- prompt $ desc ++ "? " ++ maybe "[y/n]" (bool "[y/N]" "[Y/n]") mdefault+  case trim (lower inp) of+    "y" -> return True+    "yes" -> return True+    "n" -> return False+    "no" -> return False+    "" -> maybe (yesno Nothing desc) return mdefault+    _ ->  yesno mdefault desc