Skip to content
This repository was archived by the owner on Mar 22, 2026. It is now read-only.

Commit 64fe6b0

Browse files
committed
feat: prepare 0.0.1
0 parents  commit 64fe6b0

File tree

17 files changed

+410
-0
lines changed

17 files changed

+410
-0
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
_build

.ocamlformat

Whitespace-only changes.

CHANGES.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
# Changes
2+
3+
## 0.0.1
4+
5+
Initial release, including:
6+
7+
* High-level `Terminal` module for controlling a terminal
8+
* Async-input with UTF-8 support in the `Stdin` module
9+
* Terminal `Profile`s for determining what color palettes are available
10+
* A `Color` module for parsing and working with RGB/ANSI/ANSI256 colors
11+
* A collection of 60 escape sequence functions in `Escape_seq`
12+
* a lot of room for improvement!
13+

LICENSE.md

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (c) 2023, Leandro Ostera
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is
8+
furnished to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19+
SOFTWARE.

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# TTY
2+
3+
TTY is a pure OCaml library for directly interacting with the
4+
terminal, including escape sequences, color profiles, colors, and
5+
consuming stdin.
6+
7+
It is the main backend for [MintTea][minttea].
8+
9+
[minttea]: https://github.com/leostera/minttea
10+
11+
## Getting Started
12+
13+
```
14+
$ opam install tty
15+
```

dune-project

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
(lang dune 3.11)
2+
3+
(name tty)
4+
5+
(generate_opam_files true)
6+
7+
(source (github leostera/tty))
8+
9+
(authors "Leandro Ostera <leandro@abstractmachines.dev>")
10+
11+
(maintainers "Leandro Ostera <leandro@abstractmachines.dev>")
12+
13+
(license MIT)
14+
15+
(package
16+
(name tty)
17+
(synopsis "A library for interacting with teletype and terminal emulators")
18+
(description "TTY is a library for directly interacting with teletypes and terminal emulators, including escape sequences, colors, and consuming stdin")
19+
(depends
20+
(ocaml (>= "5.1"))
21+
(dune (>= "3.11"))
22+
(uutf (>= "1.0.3")))
23+
(tags (terminal ansi tty teletype utf8)))
24+

tty.opam

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# This file is generated by dune, edit dune-project instead
2+
opam-version: "2.0"
3+
synopsis: "A library for interacting with teletype and terminal emulators"
4+
description:
5+
"TTY is a library for directly interacting with teletypes and terminal emulators, including escape sequences, colors, and consuming stdin"
6+
maintainer: ["Leandro Ostera <leandro@abstractmachines.dev>"]
7+
authors: ["Leandro Ostera <leandro@abstractmachines.dev>"]
8+
license: "MIT"
9+
tags: ["terminal" "ansi" "tty" "teletype" "utf8"]
10+
homepage: "https://github.com/leostera/tty"
11+
bug-reports: "https://github.com/leostera/tty/issues"
12+
depends: [
13+
"ocaml" {>= "5.1"}
14+
"dune" {>= "3.11" & >= "3.11"}
15+
"uutf" {>= "1.0.3"}
16+
"odoc" {with-doc}
17+
]
18+
build: [
19+
["dune" "subst"] {dev}
20+
[
21+
"dune"
22+
"build"
23+
"-p"
24+
name
25+
"-j"
26+
jobs
27+
"@install"
28+
"@runtest" {with-test}
29+
"@doc" {with-doc}
30+
]
31+
]
32+
dev-repo: "git+https://github.com/leostera/tty.git"

tty/color.ml

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
type t = RGB of int * int * int | ANSI of int | ANSI256 of int | No_color
2+
3+
exception Invalid_color of string
4+
exception Invalid_color_param of string
5+
exception Invalid_color_num of string * int
6+
7+
let to_255 str =
8+
match int_of_string_opt ("0x" ^ str) with
9+
| None -> raise (Invalid_color_param str)
10+
| Some c -> c
11+
12+
let rgb r g b = RGB (to_255 r, to_255 g, to_255 b)
13+
14+
let rgb str =
15+
match String.to_seq str |> List.of_seq |> List.map (String.make 1) with
16+
| [ "#"; r1; r2; g1; g2; b1; b2 ] -> rgb (r1 ^ r2) (g1 ^ g2) (b1 ^ b2)
17+
| [ "#"; r1; g1; b1 ] -> rgb r1 g1 b1
18+
| _ -> raise (Invalid_color str)
19+
20+
let ansi i = ANSI i
21+
let ansi256 i = ANSI256 i
22+
let no_color = No_color
23+
24+
let make str =
25+
if String.starts_with ~prefix:"#" str then rgb str
26+
else
27+
match int_of_string_opt str with
28+
| None -> raise (Invalid_color str)
29+
| Some i when i < 16 -> ansi i
30+
| Some i -> ansi256 i
31+
32+
let to_escape_seq ~mode t =
33+
match t with
34+
| RGB (r, g, b) -> Format.sprintf "2;%d;%d;%d" r g b
35+
| ANSI c ->
36+
let bg_mod x = if mode = `bg then x + 10 else x in
37+
let c = if c < 8 then bg_mod c + 30 else bg_mod (c - 8) + 90 in
38+
Int.to_string c
39+
| ANSI256 c -> Format.sprintf "5;%d" c
40+
| No_color -> ""
41+
42+
let is_no_color t = t = No_color
43+
let is_rgb t = match t with RGB _ -> true | _ -> false
44+
let is_ansi t = match t with ANSI _ -> true | _ -> false
45+
let is_ansi256 t = match t with ANSI256 _ -> true | _ -> false

tty/color.mli

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
type t = private
2+
| RGB of int * int * int
3+
| ANSI of int
4+
| ANSI256 of int
5+
| No_color
6+
7+
val make : string -> t
8+
9+
exception Invalid_color of string
10+
exception Invalid_color_param of string
11+
exception Invalid_color_num of string * int
12+
13+
val no_color : t
14+
val is_no_color : t -> bool
15+
val is_rgb : t -> bool
16+
val is_ansi : t -> bool
17+
val is_ansi256 : t -> bool
18+
val to_escape_seq : mode:[> `bg | `fg ] -> t -> string

tty/dune

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
(library
2+
(public_name tty)
3+
(name tty)
4+
(libraries uutf unix))

0 commit comments

Comments
 (0)