Created
January 25, 2024 22:30
-
-
Save jkone27/5f20a167407deca6801b6e3426a94e36 to your computer and use it in GitHub Desktop.
F# fsx fsi workshop gist for creating as simple CLI game / stub in .NET with spectreconsole and more
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
//#r "nuget: Spectre.Console" | |
// https://mdbouk.com/how-to-create-beautiful-console-applications-with-spectre-console/ | |
// https://www.asciiart.eu/animals/bears | |
// | |
#r "nuget: EluciusFTW.SpectreCoff" | |
#r "nuget: Spectre.Console.ImageSharp, 0.48.0" | |
#r "nuget: FSharp.Data" | |
#r "nuget: AlienFruit.FluentConsole.AsciiArt" | |
#r "nuget: FsHttp" | |
open AlienFruit.FluentConsole | |
open AlienFruit.FluentConsole.AsciiArt | |
open SpectreCoff | |
open FSharp.Data | |
open System | |
open FsHttp | |
module Img = | |
open Spectre.Console | |
open SixLabors.ImageSharp | |
open SixLabors.ImageSharp.Processing | |
let blackBaground (image: CanvasImage) = | |
// Set a sampler that will be used when scaling the image. | |
//image.BilinearResampler() |> ignore | |
// Mutate the image using ImageSharp | |
image.Mutate(fun ctx -> ctx.BackgroundColor(Color.Transparent) |> ignore) | |
let showImage imgUrl = | |
let str = http { GET imgUrl } |> Request.send |> Response.toStream | |
let image = | |
let i = new CanvasImage(data = str) | |
i.MaxWidth <- 64 | |
i |> blackBaground | |
AnsiConsole.Write(image) | |
module Luck = | |
let rnd = new Random() | |
let luckyMove luckyOutcomes = | |
let i = rnd.Next() % (luckyOutcomes |> List.length) | |
luckyOutcomes[i] | |
type AnsiConsole = Spectre.Console.AnsiConsole | |
type CanvasImage = Spectre.Console.CanvasImage | |
type EmojiProvider = JsonProvider<"https://raw.githubusercontent.com/github/gemoji/master/db/emoji.json"> | |
let sleepSeconds seconds = | |
System.Threading.Thread.Sleep(seconds * 1000) | |
let getEmoji s = | |
let result = EmojiProvider.GetSamples() | |
let emojiOpt = result |> Seq.tryFind (fun e -> e.Aliases.Strings |> Seq.contains s) | |
emojiOpt |> Option.map (fun e -> e.Emoji) | |
type Color = Spectre.Console.Color | |
// nice default to left align all | |
defaultAlignment <- Left | |
"Hello Sexyland World" |> figlet |> toConsole | |
let getMenu () = | |
Many | |
[ MarkupC(Color.Green, "Hello friends,") // Use any available color | |
BlankLine | |
Pumped "Welcome to Code, Connect, Reboot!" // Use the Pumped convenience style | |
BL // short for BlankLine | |
C "Please bring ... " // short for Calm | |
BI | |
[ // short for BulletItems | |
C "laptop :laptop," | |
P "some games," // short for Pumped | |
E "and some creepy stories!" ] // short for Edgy | |
C "See you " | |
P "later ... " | |
NL // Mixing list separators to indicate the line is also possible | |
Emoji "alien_monster" ] | |
|> toConsole | |
let r: string = SpectreCoff.Prompt.ask "what [red]game[/] do you want to play?" | |
customFiglet Left Color.Red r |> toConsole | |
P "is my favourite game, LOL!" |> toConsole | |
C "Great, now pick a character..." |> toConsole | |
type CharacterType = | |
| Hero of OutputPayload | |
| Wolf of OutputPayload | |
| Witch of OutputPayload | |
static member Parse(str) = | |
match str with | |
| "hero" -> Hero(Emoji "Superhero") | |
| "wolf" -> Wolf(Emoji "Wolf") | |
| "witch" -> Witch(Emoji "Mage") | |
| _ -> failwith "unknown" | |
member this.Print() = | |
match this with | |
| Hero(emoji) | |
| Wolf(emoji) | |
| Witch(emoji) -> emoji |> toConsole | |
Emoji "fire" |> toConsole | |
let character = | |
chooseFrom [ "hero"; "witch"; "wolf" ] "who are you going to be?" | |
|> CharacterType.Parse | |
C $"Great, your story begins {character}..." |> toConsole | |
character.Print() | |
P "Let the story begin." |> toConsole | |
sleepSeconds 1 | |
P "Let the story begin.." |> toConsole | |
sleepSeconds 1 | |
P "Let the story begin..." |> toConsole | |
sleepSeconds 1 | |
// TODO: show spinner | |
// let _ = | |
// start (fun ctx -> ) | |
let assets = | |
[ "shark", "https://img.freepik.com/free-vector/cute-shark-swimming-cartoon-icon-illustration_138676-2245.jpg" | |
"bear", "https://img.freepik.com/premium-photo/brown-bear-face-with-black-background_800563-12964.jpg" ] | |
|> dict | |
let showFish () = Img.showImage assets["fish"] | |
let showMonster () = | |
// using FConsole lib | |
let i = FConsole.GetInstance() | |
//i.Draw | |
i.DrawDemo(DemoPicture.DarthVader) |> ignore | |
() | |
let luckResult = Luck.luckyMove [ showFish; showMonster ] | |
// see if we are lucky or not | |
luckResult () |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment