Skip to content

Instantly share code, notes, and snippets.

@Eckankar
Created October 21, 2014 12:24
Show Gist options
  • Select an option

  • Save Eckankar/79af0037d3989f5eec63 to your computer and use it in GitHub Desktop.

Select an option

Save Eckankar/79af0037d3989f5eec63 to your computer and use it in GitHub Desktop.
signature Term =
sig
type color
val black : color
val red : color
val green : color
val yellow : color
val blue : color
val magenta : color
val cyan : color
val white : color
(* slå cursor til og fra *)
val cursor : bool -> string
(* skift forgrundsfarven *)
val fgColor : color -> string
(* skift baggrundsfarven *)
val bgColor : color -> string
(* slå tekstfarver fra *)
val resetText : string
(* slå bold til/fra *)
val bold : bool -> string
(* ryd hele skærmen *)
val reset : string
(* pos (x, y) flytter cursoren til (x, y) *)
val pos : int * int -> string
end
structure Term :> Term =
struct
type color = int
val black = 0
val red = 1
val green = 2
val yellow = 3
val blue = 4
val magenta = 5
val cyan = 6
val white = 7
val CSI = "\027["
fun cursor b = CSI ^ "?25" ^ (if b then "h" else "l")
fun fgColor n = CSI ^ "3" ^ Int.toString n ^ "m"
fun bgColor n = CSI ^ "4" ^ Int.toString n ^ "m"
val resetText = CSI ^ "0m"
fun bold b = CSI ^ (if b then "1" else "21") ^ "m"
fun pos (x, y) = CSI ^ Int.toString x ^ ";" ^ Int.toString y ^ "H"
val reset = CSI ^ "2J"
end
local
infixr $
fun f $ x = f x
structure T = Term
(* gentag strengen s n gange. *)
fun repeat s n = String.concat (List.tabulate (n, fn _ => s))
(* tegn en boks, startende i (x, y), som er w bred og h høj. *)
fun drawBox (x, y) (w, h) col =
String.concat $ List.tabulate (h,
fn i => T.pos (x+i, y) ^ T.bgColor col ^ repeat " " w ^ T.resetText
)
(* tegn en søjle af højde h, startende i (x, y) *)
fun drawColumn (x, y) h col =
String.concat $ List.tabulate (h,
fn i => T.pos (x+i, y) ^ T.bgColor col ^ " " ^ T.resetText
)
(* sov i n ms *)
fun sleep n =
let
val start = Time.now ()
fun loop () = if Time.toMilliseconds (Time.- (Time.now (), start)) < n
then loop ()
else ()
in loop () end
val width = 80
val height = 30
val iw = width - 2
fun anim n =
let
val _ = print $ String.concat [
drawColumn (6, 6 + (n mod iw)) (height-2) T.red,
drawColumn (6, 6 + ((n-1) mod iw)) (height-2) T.black
]
val _ = sleep 20
in anim (n+1) end
in
val _ = print $ String.concat [
T.cursor false,
T.reset,
drawBox (5, 5) (width, height) T.white,
drawBox (6, 6) (width-2, height-2) T.black
]
val _ = anim 0
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment