123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154(*————————————————————————————————————————————————————————————————————————————
Copyright (c) 2020–2021 Craig Ferguson <me@craigfe.io>
Distributed under the MIT license. See terms at the end of this file.
————————————————————————————————————————————————————————————————————————————*)open!Importexternalunsafe_blit_string:string->int->bytes->int->int->unit="caml_blit_string"[@@noalloc](** Polyfill for pre-4.09.0 *)typet={mutablebuffer:bytes;mutableposition:int;mutablelength:int;mutablelast:string(** Cache latest delivered contents to avoid unnecessary re-rendering *);mutablelast_len:int(** Avoids some string comparisons on [last] *);ppf:Format.formatterLazy.t}(** Invariants:
- [0 <= position <= length]
- [length = Bytes.length buffer] *)letresizetmore=letold_pos=t.positionandold_len=t.lengthinletnew_len=letres=refold_leninwhileold_pos+more>!resdores:=2*!resdone;!resinletnew_buffer=Bytes.createnew_leninBytes.blit~src:t.buffer~src_pos:0~dst:new_buffer~dst_pos:0~len:t.position;t.buffer<-new_buffer;t.length<-new_lenletadvancetlen=letnew_position=t.position+leninifnew_position>t.lengththenresizetlen;(* Fmt.pr "[%d -> %d]" t.position new_position; *)t.position<-new_positionletlift_write~len~write=Staged.inj(funtx->letposition=t.positioninadvancetlen;writex~into:t.buffer~pos:position)letadd_charbc=letpos=b.positioninifpos>=b.lengththenresizeb1;Bytes.unsafe_setb.bufferposc;b.position<-pos+1letadd_substringts~off~len=ifoff<0||len<0||off>String.lengths-lentheninvalid_arg"Line_buffer.add_substring";letposition=t.positioninadvancetlen;unsafe_blit_stringsofft.bufferpositionlenletadd_stringbs=letlen=String.lengthsinletnew_position=b.position+leninifnew_position>b.lengththenresizeblen;unsafe_blit_strings0b.bufferb.positionlen;b.position<-new_positionletadd_line_buffer~dst~src=letposition=dst.positioninletlen=src.positioninadvancedstlen;Bytes.unsafe_blit~src:src.buffer~src_pos:0~dst:dst.buffer~dst_pos:position~lenletcreate~size=letbuffer=Bytes.createsizeinletrecppf=lazy(letppf=Format.make_formatter(funsofflen->add_substringts~off~len)(fun()->())inFmt.set_style_rendererppf`Ansi_tty;ppf)andt={buffer;position=0;length=size;ppf;last="";last_len=0}intletwith_ppftf=letppf=Lazy.forcet.ppfinleta=fppfinFormat.pp_print_flushppf();aletresett=t.position<-0letcontentst=letlast=t.lastinletlast_len=t.last_leninletcurrent_len=t.positionin(* NOTE: Without an efficient substring equality function, we have no choice
but to copy here even if the buffer is clean... *)letcurrent=Bytes.sub_stringt.buffer~pos:0~len:current_leninresett;matchInt.equallast_lencurrent_len&&String.equallastcurrentwith|true->`Cleant.last|false->t.last<-current;t.last_len<-current_len;`Dirtycurrenttypemark=intletcurrent_positiont=t.positionmoduleSpan=structtypet={pos:int;len:int}letppppft=Fmt.pfppf"{ pos = %d; len = %d }"t.post.lenletempty={pos=0;len=0}letbetween_marksab={pos=a;len=b-a}endletskipt(span:Span.t)=(* XXX: this can cause spurious failures when zooming the terminal, so for the
moment we don't validate positions whatsoever. *)(* if t.position <> span.pos then
* Fmt.failwith "Misaligned span %a inside line buffer at position %d" Span.pp
* span t.position; *)advancetspan.len(*————————————————————————————————————————————————————————————————————————————
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.
————————————————————————————————————————————————————————————————————————————*)