123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124(*————————————————————————————————————————————————————————————————————————————
Copyright (c) 2020–2021 Craig Ferguson <me@craigfe.io>
Distributed under the MIT license. See terms at the end of this file.
————————————————————————————————————————————————————————————————————————————*)open!Importletmalformed_strings=Format.kasprintfinvalid_arg"Terminal.Ansi: malformed UTF-8 string: %S"smoduleLength_counter=struct(* Counting length of UTF-8 strings while skipping ANSI escape sequences. See
https://en.wikipedia.org/wiki/ANSI_escape_code#Fe_Escape_sequences for
details. *)typet={mutableacc:int;mutablestate:[`Normal|`Parsing_ansi_sequence(* Read '\x1b', but not the subsequent byte. *)|`Ansi_parameter_bytes(* Inside a CSI parameter sequence *)]}letempty()={acc=0;state=`Normal}letis_initial_ansi_bytec=Char.equalc'\x1b'letis_final_ansi_bytec=letc=Char.codecinc>=0x40&&c<=0x7eletguess_printed_char_lengthc=matchUucp.Break.tty_width_hintcwith|-1->1(* Assume width of 1 if [Uucp] can't guess *)|n->nletaddtc=matchUchar.is_charcwith|false->t.acc<-t.acc+guess_printed_char_lengthc|true->(letc=Uchar.to_charcinmatcht.statewith|`Normal->ifis_initial_ansi_bytecthent.state<-`Parsing_ansi_sequenceelset.acc<-t.acc+1|`Parsing_ansi_sequence->ifChar.equalc'['(* Control sequence introducer *)thent.state<-`Ansi_parameter_byteselset.state<-`Normal|`Ansi_parameter_bytes->ifis_final_ansi_bytecthent.state<-`Normal)letcountt=t.accendletguess_printed_widths=letcount=Length_counter.empty()inUutf.String.fold_utf_8(fun()_->function|`Malformed_->malformed_strings|`Ucharc->Length_counter.addcountc)()s;Length_counter.countcountletuchar_sizeu=matchUchar.to_intuwith|uwhenu<0->assertfalse|uwhenu<=0x007F->1|uwhenu<=0x07FF->2|uwhenu<=0xFFFF->3|uwhenu<=0x10FFFF->4|_->assertfalseexceptionExitofintletstring_equal:string->string->bool=(=)lettruncate_to_widthwidths=ifwidth<0thenFormat.kasprintfinvalid_arg"Terminal.truncate_to_width: negative width %d requested"width;letcount=Length_counter.empty()intryUutf.String.fold_utf_8(fun()i->function|`Malformed_->malformed_strings|`Ucharc->ifLength_counter.countcount=widththen(* Check for display reset, and add it if it's there; truncating
this would cause the open colour to leak. *)letdisplay_reset="\027[0m"inifi+4<=String.lengths&&string_equal(String.subs~pos:i~len:4)display_resetthenraise(Exit(i+4))elseraise(Exiti)else(Length_counter.addcountc;letcount=Length_counter.countcountinifcount<=widththen()elseraise(Exiti)))()s;swithExitlen->String.subs~pos:0~lenletshow_cursor="\x1b[?25h"lethide_cursor="\x1b[?25l"leterase_display_suffix="\x1b[J"leterase_line="\x1b[K"letmove_upppf=function0->()|n->Format.fprintfppf"\x1b[%dA"nletmove_downppf=function0->()|n->Format.fprintfppf"\x1b[%dB"n(*————————————————————————————————————————————————————————————————————————————
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.
————————————————————————————————————————————————————————————————————————————*)