All Projects → nicolaiw → FsMenu

nicolaiw / FsMenu

Licence: other
A very small DSL to create an interactive cli.

Programming Languages

F#
602 projects
shell
77523 projects
Batchfile
5799 projects

FsMenu

A very small DSL to create an interactive cli.

Build status

Master Branch

Build status

Nuget

Inc.

Usage

The DSL:

// Creates a new menu
let Menu = Sub

// Render sub menu
let (+>) name entry : (string * MenuEntry) = (name,entry)

// Execute action and exit
let (=>) s f = (s, Action (fun () -> f(); Exit))

// Execute action and render the previous menu
let (<+) s f = (s, Action (fun () -> f(); NavigateBack))

// Execute action and render the menu where you come frome
let (<+=) s f = (s, Action (fun () -> f(); Stay))

Example:

let testFunc() = 
    printfn "selected Sub Sub 3"
    printf "handle some input: "
    let input = Console.ReadLine()
    // Do some stuff with input
    ()

printfn "Use Keys: UP-Arrow DOWN-Arrow ENTER BACK-SPACE\n"

let mutable yesOrNo = ""

let test =
    Menu [
        "Item 1" => (fun () -> printf "selected Item 1")
        "Item 2" +>
           Menu [ 
               "Sub 1" +>
                       Menu [
                           "Sub Sub 1" => (fun () -> printf "selected Sub Sub 1")
                           "Sub Sub 2" +>
                                        Menu [
                                                "yes" <+ (fun () -> yesOrNo <- "--yes")
                                                "no " <+ (fun () -> yesOrNo <- "--no") ]
                           "Sub Sub 3" <+ testFunc]
               "Sub 2" => (fun () -> printf "selected Sub 2")
               "Sub 3" => (fun () -> printf "exec some command with param %s" yesOrNo)]] 
               

render test "<--"

Instead of (fun () -> printf ... you could pass any unit -> unit function.

Will turn into

Want some color ? :)

renderWithColoredEmphaziser test "<--" Color.Green

Here it is

It is also possible to emphazise the entry

renderWithColoredEntry test Color.Cyan

Or Both, the emphaziser and the entry

renderWithColoredLine test "<--" Color.Green

Another example

Here

Build

  • On Windows run build.cmd
  • On Linux run build.sh

TODO ( contributes are very welcome :D )

  • Add support for multiselect list and single select list
  • Create NuGet Package using Paket
  • Clean build.fsx
  • Refactoring: The goal is too use the render funcion like render test <| withColoredEmphaziser "<--" Color.Green or something like that
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].