123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308open!CoremoduleT=structtypet=|Reset|Bold|Faint|Normal_weight|Italic|Fraktur|Not_emphasis|Underline|Double_ul|Not_underline|Blink|Fast_blink|Not_blink|Framed|Encircled|Not_framed|Superscript|Subscript|Not_script|Invert|Not_invert|Hide|Not_hide|Strike|Not_strike|Overline|Not_overline|Variable_width|Fixed_width|FgofColor.t|BgofColor.t|Ul_colorofColor.t|Fontofint|Ideogramofint|Otherofintlist[@quickcheck.do_not_generate][@@derivingcompare~localize,equal~localize,quickcheck,sexp]endincludeTincludeComparable.Make(T)letturn_off=function|Reset->None|FgDefault|BgDefault|Ul_colorDefault|Font0|Ideogram5->None|Fg_->FgDefault|>Some|Bg_->BgDefault|>Some|Ul_color_->Ul_colorDefault|>Some|Font_->Font0|>Some|Ideogram_->Ideogram5|>Some|Bold|Faint->SomeNormal_weight|Italic|Fraktur->SomeNot_emphasis|Underline|Double_ul->SomeNot_underline|Blink|Fast_blink->SomeNot_blink|Framed|Encircled->SomeNot_framed|Superscript|Subscript->SomeNot_script|Invert->SomeNot_invert|Hide->SomeNot_hide|Strike->SomeNot_strike|Overline->SomeNot_overline|Variable_width->SomeFixed_width|Normal_weight|Not_emphasis|Not_underline|Not_blink|Not_invert|Not_hide|Not_strike|Not_overline|Not_framed|Not_script|Fixed_width|Other_->None;;(** Whether the new attribute would completely override the effects of the old one. For
example, in the code
{v "\027[1;2m" v}
we have [Bold] followed by [Faint]. After this sequence, the text will be faint, and
this is indicated by [overrides ~new_attr:Faint ~old_attr:Bold] returning true.
[Reset] overrides everything; other attributes mostly come in pairs or groups of three
that mutually override. *)letoverrides~new_attr~old_attr=matchnew_attr,old_attrwith|Reset,_->true|(Bold|Faint|Normal_weight),(Bold|Faint|Normal_weight)->true|(Italic|Not_emphasis|Fraktur),(Italic|Not_emphasis|Fraktur)->true|(Underline|Double_ul|Not_underline),(Underline|Double_ul|Not_underline)->true|(Blink|Fast_blink|Not_blink),(Blink|Fast_blink|Not_blink)->true|(Invert|Not_invert),(Invert|Not_invert)->true|(Hide|Not_hide),(Hide|Not_hide)->true|(Strike|Not_strike),(Strike|Not_strike)->true|(Overline|Not_overline),(Overline|Not_overline)->true|(Framed|Encircled|Not_framed),(Framed|Encircled|Not_framed)->true|(Superscript|Subscript|Not_script),(Superscript|Subscript|Not_script)->true|(Variable_width|Fixed_width),(Variable_width|Fixed_width)->true|Fg_,Fg_->true|Bg_,Bg_->true|Ul_color_,Ul_color_->true|Font_,Font_->true|Ideogram_,Ideogram_->true|_->false;;letof_color_codecode=letbetweenn(low,high)=Int.betweenn~low~highinmatchcodewith|[5;c]whenbetweenc(0,7)->Some(Color.sgr8_exnc)|[5;c]whenbetweenc(8,15)->Some(Color.sgr8_exn~bright:true(c-8))|[5;c]whenbetweenc(16,231)->Some(Color.Rgb6(Color.Rgb6.of_code_exnc))|[5;c]whenbetweenc(232,255)->Some(Color.gray24_exnc)|[2;r;g;b]whenbetweenr(0,255)&&betweeng(0,255)&&betweenb(0,255)->Some(Color.rgb256_exn(r,g,b))|_->None;;letof_codescodes=letbetweenn(low,high)=Int.betweenn~low~highinmatchcodeswith|[0]->Reset|[1]->Bold|[2]->Faint|[3]->Italic|[4]->Underline|[5]->Blink|[6]->Fast_blink|[7]->Invert|[8]->Hide|[9]->Strike|[f]whenbetweenf(10,19)->Font(f-10)|[20]->Fraktur|[21]->Double_ul|[22]->Normal_weight|[23]->Not_emphasis|[24]->Not_underline|[25]->Not_blink|[26]->Variable_width|[27]->Not_invert|[28]->Not_hide|[29]->Not_strike|[c]whenbetweenc(30,37)->Fg(Color.sgr8_exn(c-30))|38::rest->(matchof_color_coderestwith|Somecolor->Fgcolor|None->Othercodes)|[39]->FgColor.Default|[c]whenbetweenc(40,47)->Bg(Color.sgr8_exn(c-40))|48::rest->(matchof_color_coderestwith|Somecolor->Bgcolor|None->Othercodes)|[49]->BgColor.Default|[50]->Fixed_width|[51]->Framed|[52]->Encircled|[53]->Overline|[54]->Not_framed|[55]->Not_overline|58::rest->(matchof_color_coderestwith|Somecolor->Ul_colorcolor|None->Othercodes)|[59]->Ul_colorColor.Default|[i]whenbetweeni(60,65)->Ideogram(i-60)|[73]->Superscript|[74]->Subscript|[75]->Not_script|[c]whenbetweenc(90,97)->Fg(Color.sgr8_exn~bright:true(c-90))|[c]whenbetweenc(100,107)->Bg(Color.sgr8_exn~bright:true(c-100))|_->Othercodes;;letto_code=function|Reset->[0]|Bold->[1]|Faint->[2]|Italic->[3]|Underline->[4]|Blink->[5]|Fast_blink->[6]|Invert->[7]|Hide->[8]|Strike->[9]|Fonti->[10+i]|Fraktur->[20]|Double_ul->[21]|Normal_weight->[22]|Not_emphasis->[23]|Not_underline->[24]|Not_blink->[25]|Variable_width->[26]|Not_invert->[27]|Not_hide->[28]|Not_strike->[29]|Fgc->Color.to_fg_codec|Bgc->Color.to_bg_codec|Fixed_width->[50]|Framed->[51]|Encircled->[52]|Overline->[53]|Not_framed->[54]|Not_overline->[55]|Ul_colorc->Color.to_ul_codec|Ideogrami->[60+i]|Superscript->[73]|Subscript->[74]|Not_script->[75]|Othercodes->codes;;letto_string_hum=function|Reset->"off"|Bold->"+bold"|Faint->"+faint"|Italic->"+italic"|Underline->"+uline"|Blink->"+blink"|Fast_blink->"+fastblink"|Invert->"+invert"|Hide->"+hide"|Strike->"+strike"|Double_ul->"+2uline"|Overline->"+overline"|Normal_weight->"-weight"|Not_emphasis->"-italic"(* Since [Fraktur] isn't widely supported, this is clearer *)|Not_underline->"-uline"|Not_blink->"-blink"|Not_invert->"-invert"|Not_hide->"-hide"|Not_strike->"-strike"|Not_overline->"-overline"|Fgc->"fg:"^Color.to_string_humc|Bgc->"bg:"^Color.to_string_humc|Ul_colorc->"ul:"^Color.to_string_humc|Othercodes->"ANSI-SGR:"^(List.mapcodes~f:Int.to_string|>String.concat~sep:";")|Framed->"+framed"|Encircled->"+encircled"|Superscript->"+superscript"|Subscript->"+subscript"|Variable_width->"+proportional_spacing"|Fraktur->"+fraktur"|Not_framed->"-framed"|Not_script->"-script"|Fixed_width->"-proportional_spacing"|Font0->"-font"|Fontf->"+font:"^Int.to_stringf|Ideogram5->"-ideogram"|Ideogrami->"+ideogram:"^Int.to_stringi;;letto_string=function|Reset->"0"|FgDefault->"39"|BgDefault->"49"|Fg(Standardc)->30+Color.Sgr8.to_codec|>Int.to_string|Bg(Standardc)->40+Color.Sgr8.to_codec|>Int.to_string|Bold->"1"|Faint->"2"|Italic->"3"|Underline->"4"|Blink->"5"|Fast_blink->"6"|Invert->"7"|Hide->"8"|Strike->"9"|Double_ul->"21"|Overline->"53"|Normal_weight->"22"|Not_emphasis->"23"|Not_underline->"24"|Not_blink->"25"|Not_invert->"27"|Not_hide->"28"|Not_strike->"29"|Not_overline->"55"|Fgc->Color.to_fg_codec|>List.map~f:Int.to_string|>String.concat~sep:";"|Bgc->Color.to_bg_codec|>List.map~f:Int.to_string|>String.concat~sep:";"|Ul_colorc->Color.to_ul_codec|>List.map~f:Int.to_string|>String.concat~sep:";"|Othercodes->List.mapcodes~f:Int.to_string|>String.concat~sep:";"|Framed->"51"|Encircled->"52"|Superscript->"73"|Subscript->"74"|Variable_width->"26"|Fraktur->"20"|Not_framed->"54"|Not_script->"75"|Fixed_width->"50"|Fontf->Int.to_string(f+10)|Ideogrami->Int.to_string(i+60);;(* For compatibility with legacy patdiff configs, which used the much-less-expressive
[Patdiff_kernel.Format.Style.t] type. *)lett_of_sexpsexp=matchsexpwith|Sexp.Atom"dim"->Faint|Atom"inverse"->Invert|Atom"emph"->Italic|_asa->t_of_sexpa;;