Last active
April 17, 2020 04:28
-
-
Save dullbananas/0de5d3af02abf09b575eda86ea6462e9 to your computer and use it in GitHub Desktop.
elm concept: writing a shell with elm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- if elm was imperative | |
import Console | |
import Proc | |
type alias Model = | |
{ prompt : String | |
} | |
initModel : Model | |
initModel = | |
{ prompt = "\n$ " | |
} | |
getCmd : String -> List String | |
getCmd prompt = | |
Console.input prompt | |
|> String.split " " | |
main : Model -> Int | |
main model = | |
case getCmd of | |
[ "setprompt", newPrompt ] -> | |
main { model | prompt = newPrompt } | |
[ "exit" ] -> | |
0 | |
args -> | |
Proc.run ( \_ -> main model ) args | |
main initModel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- work in progress | |
port module Shell exposing ( main ) | |
import Gp | |
import Gp.Cmd | |
import Gp.Sub | |
import Gp.Proc | |
import Gp.Console | |
port portOut : Gp.PortOut | |
port portIn : Gp.PortIn | |
main : Gp.Program Model Msg | |
main = | |
Gp.program | |
{ init = init | |
, update = update | |
, subscriptions = subscriptions | |
, portIn = portIn | |
, portOut = portOut | |
} | |
type alias Model = | |
{ prompt : String | |
} | |
type Msg | |
= ProcFinished Gp.Proc.FinishedProc | |
| GetCmd | |
| TypedCmd String | |
init : Gp.Flags -> ( Model, Gp.Cmd Msg ) | |
init _ = | |
( { prompt = "\n$ " } | |
, Gp.trigger GetCmd | |
) | |
update : Msg -> Model -> ( Model, Gp.Cmd Msg ) | |
update msg model = | |
case msg of | |
GetCmd -> | |
( model | |
, Gp.Conosle.input TypedCmd model.prompt | |
) | |
TypedCmd command -> | |
case String.split " " command of | |
[ "setprompt", newPrompt ] -> | |
( { model | prompt = newPrompt } | |
, Gp.Cmd.trigger GetCmd | |
) | |
[ "exit" ] -> | |
( model | |
, Gp.exit 0 | |
) | |
args -> | |
( model | |
, Gp.Proc.run ProcFinished args | |
) | |
ProcFinished { exitCode } -> | |
( model | |
, Gp.Cmd.sequence | |
[ Gp.Console.print <| "Command exited with code " ++ exitCode | |
, Gp.Cmd.trigger GetCmd | |
] | |
) | |
subscriptions : Model -> Gp.Sub Msg | |
subscriptions _ = | |
Gp.Sub.none |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment