All Projects → sdiehl → repline

sdiehl / repline

Licence: MIT license
Haskeline wrapper for GHCi-like REPL interfaces

Programming Languages

haskell
3896 projects
Nix
1067 projects

Projects that are alternatives of or similar to repline

Jay
😎 Supercharged JavaScript REPL
Stars: ✭ 970 (+889.8%)
Mutual labels:  repl, readline
PrettyPrompt
A cross-platform command line prompt library that provides syntax highlighting, autocompletion, history and multi-line input.
Stars: ✭ 45 (-54.08%)
Mutual labels:  repl, readline
Lua Resty Repl
Interactive console (REPL) for Openresty and luajit code
Stars: ✭ 165 (+68.37%)
Mutual labels:  repl, readline
Readline Sync
Synchronous Readline for interactively running to have a conversation with the user via a console(TTY).
Stars: ✭ 601 (+513.27%)
Mutual labels:  repl, readline
fancyline
Readline-esque library with fancy features
Stars: ✭ 72 (-26.53%)
Mutual labels:  repl, readline
vue-code-view
A Vue 2 component like Vue SFC REPL `@vue/repl` : u can edit, run and preview the code effect display in real time on the web page.
Stars: ✭ 67 (-31.63%)
Mutual labels:  repl
malluscript
A simple,gentle,humble scripting language for mallus, based on malayalam memes.
Stars: ✭ 112 (+14.29%)
Mutual labels:  repl
nim-noise
Nim implementation of linenoise command line editor
Stars: ✭ 45 (-54.08%)
Mutual labels:  repl
nodeScratchpad
Evaluate Nodejs Code Snippets From Menubar! 💻
Stars: ✭ 102 (+4.08%)
Mutual labels:  repl
replay-csharp
An editable C# REPL (Read Eval Print Loop) powered by Roslyn and .NET Core
Stars: ✭ 69 (-29.59%)
Mutual labels:  repl
aik
Frontend Playground
Stars: ✭ 43 (-56.12%)
Mutual labels:  repl
ShenScript
Shen for JavaScript
Stars: ✭ 40 (-59.18%)
Mutual labels:  repl
Replay.jl
Replay your REPL instructions
Stars: ✭ 24 (-75.51%)
Mutual labels:  repl
repl
Framework Agnostic REPL For Node.js. Used by AdonisJS
Stars: ✭ 76 (-22.45%)
Mutual labels:  repl
huginn
Programming language with no quirks, so simple every child can master it.
Stars: ✭ 41 (-58.16%)
Mutual labels:  repl
pytezos
🐍 Python SDK & CLI for Tezos | Michelson REPL and testing framework
Stars: ✭ 93 (-5.1%)
Mutual labels:  repl
cl-readline
Common Lisp bindings to the GNU Readline library
Stars: ✭ 34 (-65.31%)
Mutual labels:  readline
Clojure-Sublimed
Clojure support for Sublime Text 4
Stars: ✭ 268 (+173.47%)
Mutual labels:  repl
lambda-dti
Interpreter of the ITGL with dynamic type inference
Stars: ✭ 18 (-81.63%)
Mutual labels:  repl
swift-commandlinekit
Framework supporting the development of command-line tools in Swift on macOS and Linux. The framework supports managing command-line arguments, provides lightweight functions to deal with escape sequences, and defines an API for reading strings from the terminal.
Stars: ✭ 38 (-61.22%)
Mutual labels:  readline

Repline

Build Status Hackage

Slightly higher level wrapper for creating GHCi-like REPL monads that are composable with normal MTL transformers. Mostly exists because I got tired of implementing the same interface for simple shells over and over and decided to canonize the giant pile of hacks that I use to make Haskeline work.

See Documentation for more detailed usage.

Examples

Migration from 0.3.x

This release adds two parameters to the ReplOpts constructor and evalRepl function.

  • finaliser
  • multilineCommand

The finaliser function is a function run when the Repl monad is is exited.

-- | Decide whether to exit the REPL or not
data ExitDecision
  = Continue -- | Keep the REPL open
  | Exit     -- | Close the REPL and exit

For example:

final :: Repl ExitDecision
final = do
  liftIO $ putStrLn "Goodbye!"
  return Exit

The multilineCommand argument takes a command which invokes a multiline edit mode in which the user can paste/enter text across multiple lines terminating with a Ctrl-D / EOF. This can be used in conjunction with a customBanner function to indicate the entry mode.

customBanner :: MultiLine -> Repl String
customBanner SingleLine = pure ">>> "
customBanner MultiLine = pure "| "

See Multiline for a complete example.

Migration from 0.2.x

The underlying haskeline library that provides readline support had a breaking API change in 0.8.0.0 which removed the bespoke System.Console.Haskeline.MonadException module in favour of using the exceptions package. This is a much better design and I strongly encourage upgrading. To migrate simply add the following bounds to your Cabal file.

build-depends:
  repline   >= 0.3.0.0
  haskeline >= 0.8.0.0

You may also need to add the following to your stack.yaml file if using Stack.

resolver: lts-15.0
packages:
  - .
extra-deps:
  - haskeline-0.8.0.0
  - repline-0.3.0.0

Usage

type Repl a = HaskelineT IO a

-- Evaluation : handle each line user inputs
cmd :: String -> Repl ()
cmd input = liftIO $ print input

-- Tab Completion: return a completion for partial words entered
completer :: Monad m => WordCompleter m
completer n = do
  let names = ["kirk", "spock", "mccoy"]
  return $ filter (isPrefixOf n) names

-- Commands
help :: [String] -> Repl ()
help args = liftIO $ print $ "Help: " ++ show args

say :: [String] -> Repl ()
say args = do
  _ <- liftIO $ system $ "cowsay" ++ " " ++ (unwords args)
  return ()

options :: [(String, [String] -> Repl ())]
options = [
    ("help", help)  -- :help
  , ("say", say)    -- :say
  ]

ini :: Repl ()
ini = liftIO $ putStrLn "Welcome!"

repl :: IO ()
repl = evalRepl (pure ">>> ") cmd options Nothing (Word completer) ini

Trying it out:

$ stack repl Simple.hs
Prelude> main

Welcome!
>>> <TAB>
kirk spock mccoy

>>> k<TAB>
kirk

>>> spam
"spam"

>>> :say Hello Haskell
 _______________
< Hello Haskell >
 ---------------
        \   ^__^
         \  (oo)\_______
            (__)\       )\/\
                ||----w |
                ||     ||

Stateful Tab Completion

Quite often tab completion is dependent on the internal state of the Repl so we'd like to query state of the interpreter for tab completions based on actions performed themselves within the Repl, this is modeleted naturally as a monad transformer stack with StateT on top of HaskelineT.

type IState = Set.Set String
type Repl a = HaskelineT (StateT IState IO) a

-- Evaluation
cmd :: String -> Repl ()
cmd input = modify $ \s -> Set.insert input s

-- Completion
comp :: (Monad m, MonadState IState m) => WordCompleter m
comp n = do
  ns <- get
  return  $ filter (isPrefixOf n) (Set.toList ns)

-- Commands
help :: [String] -> Repl ()
help args = liftIO $ print $ "Help!" ++ show args

puts :: [String] -> Repl ()
puts args = modify $ \s -> Set.union s (Set.fromList args)

opts :: [(String, [String] -> Repl ())]
opts = [
    ("help", help) -- :help
  , ("puts", puts) -- :puts
  ]

ini :: Repl ()
ini = return ()

-- Tab completion inside of StateT
repl :: IO ()
repl = flip evalStateT Set.empty
     $ evalRepl (pure ">>> ") cmd opts Nothing (Word comp) ini

Prefix Completion

Just as GHCi will provide different tab completion for kind-level vs type-level symbols based on which prefix the user has entered, we can also set up a provide this as a first-level construct using a Prefix tab completer which takes care of the string matching behind the API.

type Repl a = HaskelineT IO a

-- Evaluation
cmd :: String -> Repl ()
cmd input = liftIO $ print input

-- Prefix tab completeter
defaultMatcher :: MonadIO m => [(String, CompletionFunc m)]
defaultMatcher = [
    (":file"    , fileCompleter)
  , (":holiday" , listCompleter ["christmas", "thanksgiving", "festivus"])
  ]

-- Default tab completer
byWord :: Monad m => WordCompleter m
byWord n = do
  let names = ["picard", "riker", "data", ":file", ":holiday"]
  return $ filter (isPrefixOf n) names

files :: [String] -> Repl ()
files args = liftIO $ do
  contents <- readFile (unwords args)
  putStrLn contents

holidays :: [String] -> Repl ()
holidays [] = liftIO $ putStrLn "Enter a holiday."
holidays xs = liftIO $ do
  putStrLn $ "Happy " ++ unwords xs ++ "!"

opts :: [(String, [String] -> Repl ())]
opts = [
    ("file", files)
  , ("holiday", holidays)
  ]

init :: Repl ()
init = return ()

repl :: IO ()
repl = evalRepl (pure ">>> ") cmd opts Nothing (Prefix (wordCompleter byWord) defaultMatcher) init

Trying it out:

$ stack repl examples/Prefix.hs
Prelude> main

>>> :file <TAB>
sample1.txt sample2.txt

>>> :file sample1.txt

>>> :holiday <TAB>
christmas thanksgiving festivus

License

Copyright (c) 2014-2020, Stephen Diehl Released under the MIT License

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].