You can search for identifiers within the package.
in-package search v0.2.0
Source file matrix_terminal.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523(* Types *)typemouse_mode=[`Off|`X10|`Normal|`Button|`Any|`Sgr_normal|`Sgr_button|`Sgr_any]typecursor_position={x:int;y:int;visible:bool}moduleCaps=Capstypecapabilities=Caps.ttypecursor_state={mutablex:int;mutabley:int;mutablevisible:bool;mutablestyle:Ansi.cursor_style;mutableblinking:bool;mutablecolor:float*float*float*float;}typet={output:string->unit;tty:bool;mutablecaps:Caps.t;mutablemouse_mode:mouse_mode;mutablebracketed_paste_enabled:bool;mutablefocus_enabled:bool;mutablekitty_keyboard_enabled:bool;mutablekitty_keyboard_flags:int;mutablemodify_other_keys_enabled:bool;mutablecolor_scheme_reports_enabled:bool;mutableresize_reports_enabled:bool;mutablemeta_sends_escape_enabled:bool;mutablegrapheme_mode_enabled:bool;(* [*_armed] is set before the enable sequence is written, and [*_enabled]
only after. When the output callback raises during an enable, the
sequence may have reached the terminal although the enable never
completed. [reset_state] writes its disable sequences by armed, so a
protocol enabled in part is still unwound, while the [*_enabled]
accessors report the state confirmed. *)mutablemouse_armed:bool;mutablebracketed_paste_armed:bool;mutablefocus_armed:bool;mutablekitty_keyboard_armed:bool;mutablemodify_other_keys_armed:bool;mutablecolor_scheme_reports_armed:bool;mutableresize_reports_armed:bool;mutablemeta_sends_escape_armed:bool;mutablegrapheme_mode_armed:bool;mutablealt_screen_armed:bool;mutablescroll_region_armed:bool;cursor:cursor_state;mutablealt_screen:bool;mutablescroll_region:(int*int)option;mutablepixel_resolution:(int*int)option;(* Appearance the handle actually emitted. [reset_state] restores only what
was touched: popping a title or resetting cursor colour/style the app
never set would clobber user- or shell-configured terminal state. The
title is pushed on the terminal's title stack while [title_sent]. *)mutabletitle_sent:bool;mutablecursor_style_sent:bool;mutablecursor_color_sent:bool;}(* Pre-computed escape sequences *)letalternate_on="\027[?1049h"letalternate_off="\027[?1049l"letfocus_on="\027[?1004h"letfocus_off="\027[?1004l"letpaste_on="\027[?2004h"letpaste_off="\027[?2004l"letcolor_scheme_reports_on="\027[?2031h"letcolor_scheme_reports_off="\027[?2031l"letresize_reports_on="\027[?2048h"letresize_reports_off="\027[?2048l"letmeta_sends_escape_on="\027[?1036h"letmeta_sends_escape_off="\027[?1036l"letkitty_kb_pushflags=Ansi.(to_string(csi_u_push~flags))letkitty_kb_pop="\027[<u"letmodify_other_keys_on_seq="\027[>4;1m"letmodify_other_keys_off_seq="\027[>4;0m"letcursor_show="\027[?25h"letcursor_hide="\027[?25l"letunicode_on="\027[?2027h"letunicode_off="\027[?2027l"letreset_sgr="\027[0m"letcursor_default="\027[0 q"lettitle_push="\027[22;0t"lettitle_pop="\027[23;0t"letreset_cursor_color_seq=Ansi.to_string(funwriter->Ansi.reset_cursor_color_fallbackwriter;Ansi.reset_cursor_colorwriter)letmouse_x10="\027[?9h"letmouse_tracking="\027[?1000h"letmouse_button="\027[?1002h"letmouse_motion="\027[?1003h"letdisable_all_mouse_seq="\027[?9l\027[?1000l\027[?1002l\027[?1003l\027[?1015l\027[?1006l"letsgr_normal_seq="\027[?1006h\027[?1000h"letsgr_button_seq="\027[?1006h\027[?1000h\027[?1002h"letsgr_any_seq="\027[?1006h\027[?1000h\027[?1002h\027[?1003h"(* Helpers *)letclamp_color_componentx=letx=ifFloat.is_nanxthen0.elsexinint_of_float(Float.round(Float.max0.(Float.min1.x)*.255.))(* Core output *)letsendtseq=ift.ttythent.outputseqletttyt=t.tty(* Constructor *)letmake~output?(tty=true)caps={output;tty;caps;mouse_mode=`Off;bracketed_paste_enabled=false;focus_enabled=false;kitty_keyboard_enabled=false;kitty_keyboard_flags=0b00101;modify_other_keys_enabled=false;color_scheme_reports_enabled=false;resize_reports_enabled=false;meta_sends_escape_enabled=false;grapheme_mode_enabled=false;mouse_armed=false;bracketed_paste_armed=false;focus_armed=false;kitty_keyboard_armed=false;modify_other_keys_armed=false;color_scheme_reports_armed=false;resize_reports_armed=false;meta_sends_escape_armed=false;grapheme_mode_armed=false;alt_screen_armed=false;scroll_region_armed=false;cursor={x=1;y=1;visible=true;style=`Block;blinking=false;color=(1.,1.,1.,1.);};alt_screen=false;scroll_region=None;pixel_resolution=None;title_sent=false;cursor_style_sent=false;cursor_color_sent=false;}(* Capability access *)letcapabilitiest=t.capsletset_capabilitiestcaps=t.caps<-capsletpixel_resolutiont=t.pixel_resolutionletset_pixel_resolutiontres=t.pixel_resolution<-res(* Mouse mode *)letdisable_all_mouset=sendtdisable_all_mouse_seqletset_mouse_modetmode=ift.mouse_mode=modethen()else(disable_all_mouset;ifmode<>`Offthent.mouse_armed<-true;(matchmodewith|`Off->()|`X10->sendtmouse_x10|`Normal->sendtmouse_tracking|`Button->sendtmouse_button|`Any->sendtmouse_motion|`Sgr_normal->sendtsgr_normal_seq|`Sgr_button->sendtsgr_button_seq|`Sgr_any->sendtsgr_any_seq);t.mouse_mode<-mode;ifmode=`Offthent.mouse_armed<-false)letmouse_modet=t.mouse_mode(* Bracketed paste *)letenable_bracketed_pastetenable=ift.bracketed_paste_enabled=enablethen()elseifenablethen(t.bracketed_paste_armed<-true;sendtpaste_on;t.bracketed_paste_enabled<-true)else(sendtpaste_off;t.bracketed_paste_enabled<-false;t.bracketed_paste_armed<-false)letbracketed_paste_enabledt=t.bracketed_paste_enabled(* Focus reporting *)letenable_focus_reportingtenable=ift.focus_enabled=enablethen()elseifenablethen(t.focus_armed<-true;sendtfocus_on;t.focus_enabled<-true)else(sendtfocus_off;t.focus_enabled<-false;t.focus_armed<-false)letfocus_reporting_enabledt=t.focus_enabled(* Kitty keyboard *)letenable_kitty_keyboard?(flags=0b00101)tenable=ifenablethen(if(nott.kitty_keyboard_enabled)||t.kitty_keyboard_flags<>flagsthen((* Pop the previous entry before pushing replacement flags so the
terminal's keyboard stack never holds more than one entry from this
handle; the single pop in disable and [reset_state] then fully
unwinds it (the same dance as [restore_modes]). *)ift.kitty_keyboard_enabledthensendtkitty_kb_pop;t.kitty_keyboard_armed<-true;sendt(kitty_kb_pushflags);t.kitty_keyboard_enabled<-true;t.kitty_keyboard_flags<-flags))elseift.kitty_keyboard_enabledthen(sendtkitty_kb_pop;t.kitty_keyboard_enabled<-false;t.kitty_keyboard_armed<-false)letkitty_keyboard_enabledt=t.kitty_keyboard_enabled(* Modify other keys *)letenable_modify_other_keystenable=ift.modify_other_keys_enabled=enablethen()elseifenablethen(t.modify_other_keys_armed<-true;sendtmodify_other_keys_on_seq;t.modify_other_keys_enabled<-true)else(sendtmodify_other_keys_off_seq;t.modify_other_keys_enabled<-false;t.modify_other_keys_armed<-false)letmodify_other_keys_enabledt=t.modify_other_keys_enabled(* Color scheme reports, resize reports and metaSendsEscape: a mode is armed
before its enable sequence is written, as above. *)letenable_color_scheme_reportstenable=ift.color_scheme_reports_enabled<>enablethenifenablethen(t.color_scheme_reports_armed<-true;sendtcolor_scheme_reports_on;t.color_scheme_reports_enabled<-true)else(sendtcolor_scheme_reports_off;t.color_scheme_reports_enabled<-false;t.color_scheme_reports_armed<-false)letenable_resize_reportstenable=ift.resize_reports_enabled<>enablethenifenablethen(t.resize_reports_armed<-true;sendtresize_reports_on;t.resize_reports_enabled<-true)else(sendtresize_reports_off;t.resize_reports_enabled<-false;t.resize_reports_armed<-false)letenable_meta_sends_escapetenable=ift.meta_sends_escape_enabled<>enablethenifenablethen(t.meta_sends_escape_armed<-true;sendtmeta_sends_escape_on;t.meta_sends_escape_enabled<-true)else(sendtmeta_sends_escape_off;t.meta_sends_escape_enabled<-false;t.meta_sends_escape_armed<-false)(* Unicode width, the alternate screen and the scrolling region: armed
before the change is written, as above. *)letenable_grapheme_modetenable=ift.grapheme_mode_enabled<>enablethenifenablethen(t.grapheme_mode_armed<-true;sendtunicode_on;t.grapheme_mode_enabled<-true)else(sendtunicode_off;t.grapheme_mode_enabled<-false;t.grapheme_mode_armed<-false)letenter_alternate_screent=ifnott.alt_screenthen(t.alt_screen_armed<-true;sendtalternate_on;t.alt_screen<-true)letleave_alternate_screent=ift.alt_screenthen(sendtalternate_off;t.alt_screen<-false;t.alt_screen_armed<-false)letalt_screent=t.alt_screenletset_scroll_regiont~top~bottom=ift.scroll_region<>Some(top,bottom)then(letseq=Ansi.(to_string(set_scrolling_region~top~bottom))int.scroll_region_armed<-true;sendtseq;t.scroll_region<-Some(top,bottom))letclear_scroll_regiont=ift.scroll_region<>Nonethen(sendtAnsi.(to_stringreset_scrolling_region);t.scroll_region<-None;t.scroll_region_armed<-false)letscroll_regiont=t.scroll_region(* Cursor *)letset_cursor_visibletvisible=ift.cursor.visible<>visiblethen(sendt(ifvisiblethencursor_showelsecursor_hide);t.cursor.visible<-visible)letcursor_visiblet=t.cursor.visibleletcursor_positiont={x=t.cursor.x;y=t.cursor.y;visible=t.cursor.visible}letmove_cursor?(visible=true)t~row~col=letrow=max1rowinletcol=max1colinifvisible<>t.cursor.visiblethenset_cursor_visibletvisible;t.cursor.x<-col;t.cursor.y<-row;sendtAnsi.(to_string(cursor_position~row~col))letcursor_style_statet=(t.cursor.style,t.cursor.blinking)letcursor_colort=t.cursor.colorletwrite_cursor_stylet~shapewriter=t.cursor_style_sent<-true;Ansi.cursor_style~shapewriterletwrite_cursor_colortcolorwriter=matchcolorwith|Some(r,g,b)->t.cursor_color_sent<-true;Ansi.cursor_color~r~g~bwriter|None->(* A queued reset may never reach the terminal. Keep restoration armed
until [reset_cursor_color] confirms the output succeeded. *)Ansi.Writer.write_stringwriterreset_cursor_color_seq(* Each writes what it sets alone. The terminal keeps the style and the color
while the cursor is hidden and shows them with it. *)letset_cursor_styletstyle~blinking=t.cursor.style<-style;t.cursor.blinking<-blinking;letshape=Ansi.cursor_shape_of_style~style~blinkinginsendt(Ansi.to_string(funwriter->write_cursor_stylet~shapewriter))letset_cursor_colort~r~g~b~a=t.cursor.color<-(r,g,b,a);letcolor=Some(clamp_color_componentr,clamp_color_componentg,clamp_color_componentb)insendt(Ansi.to_string(funwriter->write_cursor_colortcolorwriter))letreset_cursor_colort=t.cursor.color<-(1.,1.,1.,1.);sendtreset_cursor_color_seq;t.cursor_color_sent<-false(* The first title pushes the one it replaces on xterm's title stack
(XTWINOPS, "CSI 22 ; 0 t": "Save xterm icon and window title on stack"),
and [reset_state] pops it ("CSI 23 ; 0 t"), so the shell's title comes
back. *)letset_titlettitle=ifnott.title_sentthen(t.title_sent<-true;sendttitle_push);sendtAnsi.(to_string(set_title~title))letcopy_to_clipboardttext=letseq=Ansi.set_clipboard~textinmatcht.caps.multiplexerwith|Some`Tmux->sendt(Ansi.to_string(Ansi.tmux_passthroughseq))|Some`Screen->(* Screen forwards at most 252 payload bytes per DCS envelope. OSC 52
uses BEL so its terminator cannot close the enclosing DCS early. *)letseq=Ansi.to_stringseqinletlen=String.lengthseqinletbuf=Buffer.create(len+(4*((len+251)/252)))inletoff=ref0inwhile!off<lendoletcount=min252(len-!off)inBuffer.add_stringbuf"\027P";Buffer.add_substringbufseq!offcount;Buffer.add_stringbuf"\027\\";off:=!off+countdone;sendt(Buffer.contentsbuf)|None->sendt(Ansi.to_stringseq)letnotifyt~title~body=letseq=Ansi.notifyt.caps.notification~title~bodyinletseq=ift.caps.multiplexer=Some`TmuxthenAnsi.tmux_passthroughseqelseseqinsendt(Ansi.to_stringseq)letbellt=sendtAnsi.(to_stringbell)letquery_color_schemet=sendtAnsi.(to_string(queryColor_scheme_report))letquery_pixel_resolutiont=sendtAnsi.(to_string(queryPixel_size))(* Mode restoration *)letrestore_modes?(skip_focus=false)t=(matcht.mouse_modewith|`Off->()|`X10->sendtmouse_x10|`Normal->sendtmouse_tracking|`Button->sendtmouse_button|`Any->sendtmouse_motion|`Sgr_normal->sendtsgr_normal_seq|`Sgr_button->sendtsgr_button_seq|`Sgr_any->sendtsgr_any_seq);ift.focus_enabled&¬skip_focusthensendtfocus_on;ift.bracketed_paste_enabledthensendtpaste_on;ift.kitty_keyboard_enabledthen(sendtkitty_kb_pop;sendt(kitty_kb_pusht.kitty_keyboard_flags));ift.modify_other_keys_enabledthensendtmodify_other_keys_on_seq;ift.color_scheme_reports_enabledthensendtcolor_scheme_reports_on;ift.resize_reports_enabledthensendtresize_reports_on;ift.meta_sends_escape_enabledthensendtmeta_sends_escape_on(* Reset and close *)letreset_statet=sendtcursor_show;sendtreset_sgr;ift.cursor_color_sentthenreset_cursor_colort;ift.cursor_style_sentthen(sendtcursor_default;t.cursor_style_sent<-false);ift.kitty_keyboard_armedthensendtkitty_kb_pop;t.kitty_keyboard_enabled<-false;t.kitty_keyboard_armed<-false;t.kitty_keyboard_flags<-0b00101;ift.modify_other_keys_armedthensendtmodify_other_keys_off_seq;t.modify_other_keys_enabled<-false;t.modify_other_keys_armed<-false;ift.mouse_armed||t.mouse_mode<>`Offthendisable_all_mouset;t.mouse_mode<-`Off;t.mouse_armed<-false;ift.bracketed_paste_armedthensendtpaste_off;t.bracketed_paste_enabled<-false;t.bracketed_paste_armed<-false;ift.focus_armedthensendtfocus_off;t.focus_enabled<-false;t.focus_armed<-false;ift.color_scheme_reports_armedthensendtcolor_scheme_reports_off;t.color_scheme_reports_enabled<-false;t.color_scheme_reports_armed<-false;ift.resize_reports_armedthensendtresize_reports_off;t.resize_reports_enabled<-false;t.resize_reports_armed<-false;ift.meta_sends_escape_armedthensendtmeta_sends_escape_off;t.meta_sends_escape_enabled<-false;t.meta_sends_escape_armed<-false;ift.grapheme_mode_armedthensendtunicode_off;t.grapheme_mode_enabled<-false;t.grapheme_mode_armed<-false;ift.scroll_region_armedthensendtAnsi.(to_stringreset_scrolling_region);t.scroll_region<-None;t.scroll_region_armed<-false;ift.alt_screen_armedthensendtalternate_off;t.alt_screen<-false;t.alt_screen_armed<-false;(* A terminal without the stack keeps the empty title. *)ift.title_sentthen(sendtAnsi.(to_string(set_title~title:""));sendttitle_pop;t.title_sent<-false);sendtcursor_show;t.cursor.visible<-true;t.cursor.style<-`Block;t.cursor.blinking<-false;t.cursor.color<-(1.,1.,1.,1.)letcloset=reset_statet