Skip to content

Instantly share code, notes, and snippets.

@IvanRainbolt
Last active March 7, 2020 04:16
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save IvanRainbolt/6bddae9e59a52312e675afc7d5d76529 to your computer and use it in GitHub Desktop.
Save IvanRainbolt/6bddae9e59a52312e675afc7d5d76529 to your computer and use it in GitHub Desktop.
Very basic F# command line UI
//Very basic F# command line UI
//trying to get a global state
open System
let mutable x = "NoUser"
let printPrompt () =
printfn "Current State: %s" x
printf "command:>"
let getCommandLineInput _ =
printPrompt ()
System.Console.ReadLine ()
let writeError value =
printfn "ERROR - No Match\nYou typed: %s" value
let writeMatch value =
printfn "Matched: %s" value
let processCommandLineInput commandLineInput =
match commandLineInput with
| "a" -> writeMatch "a"; x <- "Ivan"
| "b" -> writeMatch "b"; x <- "Bob"
| "c" -> writeMatch "b"
| "exit" | "quit" -> Environment.Exit 55
| _ -> writeError commandLineInput
//change to output a 'command'?
//let progLoop () =
// let commandLineInput = Seq.initInfinite getCommandLineInput
// Seq.iter processCommandLineInput commandLineInput
let progLoop () =
getCommandLineInput
|> Seq.initInfinite
|> Seq.iter processCommandLineInput
[<EntryPoint>]
let main argv =
progLoop ()
0
// big time help from
// https://www.log4code.com/f-infinite-sequence-to-read-console-input/
// https://dev.to/integerman/creating-a-net-core-3-0-f-console-app-348h
// FoggyFinder -> https://forums.fsharp.org/t/basic-f-console-ui-program-loop/1216?u=ivanrainbolt
@gtbremer
Copy link

gtbremer commented Mar 7, 2020

You could try something like this:

//Very basic F# command line UI 
//trying to get a global state

open System

let printPrompt state = 
    printfn "Current State: %s" state
    printf "command:>"

let getCommandLineInput state =  
    state |> printPrompt
    let input = Console.ReadLine()
    state, input 

let writeError value =
    value |> printfn "ERROR - No Match\nYou typed: %s" 

let writeMatch value =
    value |> printfn "Matched: %s" 
    
let processCommandLineInput state commandLineInput =
    match commandLineInput with
    | "a"            -> writeMatch("a")
                        Some("Ivan")
    
    | "b"            -> writeMatch("b")
                        Some("Bob")
    
    | "c"            -> writeMatch("b")
                        Some(state)
    
    | "exit"|"quit"  -> None

    | otherwise      -> writeError(otherwise)
                        Some(state)    

let rec progLoop state =
    state
    |>  getCommandLineInput
    ||> processCommandLineInput
    |>  function
        | Some newState -> newState |> progLoop
        | None          -> printfn "See ya!"

[<EntryPoint>]
let main _argv = 
    "NoUser" |> progLoop  
    0

@gtbremer
Copy link

gtbremer commented Mar 7, 2020

Aaaand I just now saw the post on the fsharp community forums. Ahh, well...happy F#-ing! :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment