-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add terminal.unix
to re-expose size functions
#43
Merged
Merged
Changes from 2 commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,5 +7,5 @@ | |
fmt | ||
logs | ||
logs.fmt | ||
terminal | ||
terminal_ansi | ||
vector)) |
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
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
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
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
File renamed without changes.
File renamed without changes.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
(library | ||
(name terminal_ansi) | ||
(public_name terminal.ansi) | ||
(libraries stdlib-shims uutf uucp)) |
File renamed without changes.
File renamed without changes.
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
(*———————————————————————————————————————————————————————————————————————————— | ||
Copyright (c) 2020–2021 Craig Ferguson <me@craigfe.io> | ||
Distributed under the MIT license. See terms at the end of this file. | ||
————————————————————————————————————————————————————————————————————————————*) | ||
|
||
(** This library provides a small set of standard utility functions for | ||
interacting with terminals. *) | ||
|
||
module Color : sig | ||
type t | ||
(** The type of colours that can be rendered to a terminal. *) | ||
|
||
(** {1 4-bit ANSI colours} | ||
|
||
Colours built using {!ansi} will be rendered using the standard | ||
{{:https://en.wikipedia.org/wiki/ANSI_escape_code#3-bit_and_4-bit} 4-bit | ||
ANSI escape codes} for terminals. The actual colours displayed to the | ||
user depend on their terminal configuration / theme, ensuring that they | ||
look natural in context. *) | ||
|
||
type plain = | ||
[ `black | `blue | `cyan | `green | `magenta | `red | `white | `yellow ] | ||
|
||
val ansi : [ plain | `bright of plain ] -> t | ||
|
||
(** {1 24-bit RGB colours} | ||
|
||
Most modern terminals offer support for full 24-bit RGB colour (called | ||
"{{:https://en.wikipedia.org/wiki/ANSI_escape_code#24-bit} true colour}") | ||
in addition to the 16 original ANSI colours. These colours are rendered | ||
exactly as requested, offering greater flexibility at the risk of clashing | ||
with the user's theming. *) | ||
|
||
val rgb : int -> int -> int -> t | ||
(** [rgb r g b] is the RGB24 colour with the given red, green and blue colour | ||
components respectively. Raises [Invalid_argument] if any of the | ||
components are outside the range [\[0, 255\]]. *) | ||
|
||
val hex : string -> t | ||
(** [hex s] is the RGB24 colour given by the | ||
{{:https://en.wikipedia.org/wiki/Web_colors#Hex_triplet} hex triplet} [s], | ||
which must start with [#]. Examples: | ||
|
||
- [hex "#FF8C00"] = [rgb 0xFF 0x8C 0x00] | ||
- [hex "#fa0"] = [rgb 0xFF 0xAA 0x00] | ||
|
||
Raises [Invalid_argument] if the given string is not a [#]-prefixed hex | ||
triplet. *) | ||
|
||
val pp_dump : Format.formatter -> t -> unit | ||
(** Pretty-print a colour with an unspecified format. *) | ||
end | ||
|
||
module Style : sig | ||
type t | ||
(** The type of terminal {i styles}: values that can be printed to a terminal | ||
in order to change the way that it renders text. *) | ||
|
||
val code : t -> string | ||
(** Get the ANSI escape code for the given style. *) | ||
|
||
(** Constructing ANSI styles: *) | ||
|
||
val none : t | ||
val bold : t | ||
val faint : t | ||
val italic : t | ||
val underline : t | ||
val reverse : t | ||
val fg : Color.t -> t | ||
val bg : Color.t -> t | ||
end | ||
|
||
module Ansi : sig | ||
val show_cursor : string | ||
val hide_cursor : string | ||
val move_up : Format.formatter -> int -> unit | ||
val move_down : Format.formatter -> int -> unit | ||
val erase_line : string | ||
val erase_display_suffix : string | ||
end | ||
|
||
val guess_printed_width : string -> int | ||
(** [guess_printed_width s] returns an estimate of the number of terminal | ||
columns that the UTF-8 encoded string [s] would occupy if displayed in a | ||
terminal, after stripping any ANSI escape codes in the string. | ||
|
||
{b Note:} | ||
{i this function uses a heuristic ({!Uucp.tty_width_hint}) to guess the | ||
rendered length of supplied strings. This function is not guaranteed to | ||
be correct on all UTF-8 codepoints. See the [Uucp] documentation for | ||
details.} *) | ||
|
||
val truncate_to_width : int -> string -> string | ||
(** [truncate_to_width n s] is the longest prefix of UTF-8 encoded string [s] | ||
that will fit within [n] columns when displayed in a terminal (without | ||
including unbalanced ANSI control sequences after the [n]-th column). | ||
|
||
As with {!guess_printed_width}, the implementation relies on heuristics and | ||
so may not be accurate for all inputs (or for all terminal implementations).*) | ||
|
||
(*———————————————————————————————————————————————————————————————————————————— | ||
Copyright (c) 2020–2021 Craig Ferguson <me@craigfe.io> | ||
|
||
Permission to use, copy, modify, and/or distribute this software for any | ||
purpose with or without fee is hereby granted, provided that the above | ||
copyright notice and this permission notice appear in all copies. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL | ||
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING | ||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER | ||
DEALINGS IN THE SOFTWARE. | ||
————————————————————————————————————————————————————————————————————————————*) |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,9 @@ | ||
(library | ||
(name terminal) | ||
(public_name terminal) | ||
(libraries stdlib-shims uutf uucp)) | ||
(name terminal) | ||
(foreign_stubs | ||
(language c) | ||
(names terminal_stubs)) | ||
(libraries | ||
(re_export terminal_ansi) | ||
unix)) |
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
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not sure to really understand the
re_export
field. That means that ifunix
is found as a dependency, we integrateterminate_ansi
?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't understand
re_export
either and copied that trick fromprogress/dune
, but it doesn't work without it as e.g.Terminal.Color
is now a module alias toTerminal_ansi.Color
and withoutre_export
theTerminal_ansi
would be missing and cause compilation error (so a user would need to always depend on bothterminal terminal.ansi
rather than justterminal
)(The
re_export
doesn't have anything to do withunix
being available, a mirageos unikernel would depend onterminal.ansi
and ignore the unix-dependentterminal
lib, just like it would need to depend onprogress.engine
and wouldn't be able to use the unixyprogress
lib)