Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file ocolor_sgr.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245openOcolor_types(** Until the last moment, it is more convenient to work with list of integers.
It is easier to apply several styles at once, since a sequence can be the
concatenation of several simple sequences. And, at the last moment, we
translate it as the string sequence as it will be passed to the terminal.
*)typeseq=intlist(** Translate a list of integer to the corresponding escape sequence string.
This is the list of integer separated by {!Ocolor_config.separator} (by
default ";"), surrounded by the appropriate prefix and suffix (resp.
["\x1b\["] and ["m"])
*)letsgr_of_seq(l:seq):string=letesc="\x1b"in(* Escape character *)letcsi="["in(* Control Sequence Introducer *)letsgr="m"in(* Select Graphic Rendition *)letsep=Ocolor_config.get_separator()inletseq=List.mapstring_of_intl|>String.concatsepinesc^csi^seq^sgrlet code_of_color3(c:color4):int=letint_of_bool:bool->int=functiontrue->1|false->0inint_of_boolc.r4lsl0+int_of_bool c.g4lsl1+int_of_bool c.b4lsl2letfg_code_of_color4 (c:color4):int=code_of_color3c+ifc.intensity4then90else30letcode_of_color8(c:color8):int=matchcwith|Standard c->code_of_color3c+ifc.intensity4then8else0|Cube6{r6;g6;b6}->ifr6<0|| r6>5thenraise(Invalid_argument"Red parameter of a color8 should be in [0; 5].");ifg6<0||g6>5thenraise(Invalid_argument"Green parameter of a color8 should be in [0; 5].");ifb6<0||b6>5thenraise(Invalid_argument"Blue parameter of a color8 should be in [0; 5].");16+36*r6+6*g6+b6|Grayscalec->ifc<0||c>23thenraise(Invalid_argument"Grayscale parameter of a color8 should be in [0; 23].");16+36*6+cletbg_code_of_color4(c:color4):int=10+fg_code_of_color4c(** {2 Parametric style sequences} *)(** {3 Foreground color} *)(** {4 Integer sequences} *)letdefault_fg_seq:seq=[39](** Legal values for parameters are \[0; 255\]. *)letfg_rgb_seq(r:int)(g:int)(b:int):seq =ifr<0||r>255thenraise(Invalid_argument"Red parameter should be in [0; 255].");ifg<0||g>255thenraise(Invalid_argument"Green parameter should be in [0; 255].");ifb<0||b>255thenraise(Invalid_argument"Blue parameter should be in [0; 255].");[38;2;r;g;b]letfg_color4_seq(c:color4):seq=[fg_code_of_color4 c]letfg_color8_seq(c:color8):seq=[38;5;code_of_color8c]letfg_color24_seq(c:color24):seq=fg_rgb_seqc.r24c.g24 c.b24letfg_color_seq(c:color):seq=let openOcolor_converterinletopenOcolor_configinmatchc,get_color_capability ()with|C4c,(Color4|Color8|Color24)->fg_color4_seqc|C8c,Color4->c|>color4_of_color8|>fg_color4_seq|C8c,(Color8|Color24)->fg_color8_seqc|C24c,Color4->c|>color4_of_color24|>fg_color4_seq|C24c,Color8->c|>color8_of_color24|>fg_color8_seq|C24c,Color24->fg_color24_seqc(** {4 Escape sequences} *)letdefault_fg_sgr:string=sgr_of_seqdefault_fg_seq(** Legal values for parameters are \[0; 255\]. *)letfg_rgb_sgr(r:int)(g:int)(b:int):string=ifr<0||r>255thenraise(Invalid_argument"Red parameter should be in [0; 255].");ifg<0||g>255thenraise(Invalid_argument"Green parameter should be in [0; 255].");ifb<0||b>255thenraise(Invalid_argument"Blue parameter should be in [0; 255].");fg_rgb_seqrgb|>sgr_of_seqletfg_color4_sgr(c:color4):string=c|>fg_color4_seq|>sgr_of_seqletfg_color8_sgr(c:color8):string=c|>fg_color8_seq|>sgr_of_seqletfg_color24_sgr(c:color24):string=c|>fg_color24_seq |> sgr_of_seq(** {3 Background color} *)(** {4 Integer sequences} *)letdefault_bg_seq:seq=[49](** Legal values for parameters are \[0; 255\]. *)letbg_rgb_seq(r:int)(g:int)(b:int):seq =ifr<0||r>255thenraise(Invalid_argument"Red parameter should be in [0; 255].");ifg<0||g>255thenraise(Invalid_argument"Green parameter should be in [0; 255].");ifb<0||b>255thenraise(Invalid_argument"Blue parameter should be in [0; 255].");[48;2;r;g;b]letbg_color4_seq(c:color4):seq=[bg_code_of_color4 c]letbg_color8_seq(c:color8):seq=[48;5;code_of_color8c]letbg_color24_seq(c:color24):seq=bg_rgb_seqc.r24c.g24 c.b24(** {4 Escape sequences} *)letdefault_bg_sgr:string=sgr_of_seqdefault_bg_seq(** Legal values for parameters are \[0; 255\]. *)letbg_rgb_sgr(r:int)(g:int)(b:int):string=ifr<0||r>255thenraise(Invalid_argument"Red parameter should be in [0; 255].");ifg<0||g>255thenraise(Invalid_argument"Green parameter should be in [0; 255].");ifb<0||b>255thenraise(Invalid_argument"Blue parameter should be in [0; 255].");bg_rgb_seqrgb|>sgr_of_seqletbg_color4_sgr(c:color4):string=c|>bg_color4_seq|>sgr_of_seqletbg_color8_sgr(c:color8):string=c|>bg_color8_seq|>sgr_of_seqletbg_color24_sgr(c:color24):string=c|>bg_color24_seq |> sgr_of_seqletbg_color_seq(c:color):seq=let openOcolor_converterinletopenOcolor_configinmatchc,get_color_capability ()with|C4c,(Color4|Color8|Color24)->bg_color4_seqc|C8c,Color4->c|>color4_of_color8|>bg_color4_seq|C8c,(Color8|Color24)->bg_color8_seqc|C24c,Color4->c|>color4_of_color24|>bg_color4_seq|C24c,Color8->c|>color8_of_color24|>bg_color8_seq|C24c,Color24->bg_color24_seqc(** {3 Font} *)(** {4 Integer sequences} *)(** Legal values for parameter are \[1; 9\] *)letfont_seq(i:int):seq=ifi<1||i>9then raise(Invalid_argument"Font parameter should be in [1; 9].");[10+i]letdefault_font_seq:seq=[10](** {4 Escape sequences} *)(** Legal values for parameters are \[1; 9\] *)letfont_sgr(i:int):string=i|>font_seq|>sgr_of_seqletdefault_font_sgr:string=sgr_of_seqdefault_font_seq(** {2 Non parametric style sequences} *)(** {3 SGR sequences of common styles as [int list]} *)letreset_seq:seq=[0]letbold_seq:seq=[1]letfaint_seq:seq=[2]letitalic_seq:seq=[3]letunderlined_seq:seq=[4]let blink_seq:seq=[5]letreverse_video_seq:seq=[7]letconceal_seq:seq=[8]letcrossed_out_seq:seq=[9]let fraktur_seq:seq=[20]letdouble_underlined_seq:seq=[21]letfaint_bold_off_seq :seq=[22]letitalic_fraktur_off_seq:seq=[23]letunderlined_off_seq :seq=[24]letblink_off_seq:seq=[25]letreverse_video_off_seq:seq=[27]letconcel_off_seq :seq=[28]letcrossed_out_off_seq:seq=[29]letframed_seq :seq=[51]letencircled_seq:seq=[52]letoverlined_seq:seq=[53]letframed_encircled_off_seq:seq=[54]letoverlined_off_seq :seq=[55](** {3 String SGR sequences of common styles} *)letreset_sgr:string=sgr_of_seqreset_seqletbold_sgr:string =sgr_of_seqbold_seqletdouble_underlined_sgr:string=sgr_of_seq double_underlined_seqletfaint_sgr:string=sgr_of_seqfaint_seqletfaint_bold_off_sgr:string=sgr_of_seq faint_bold_off_seqletitalic_sgr:string=sgr_of_seqitalic_seqletitalic_fraktur_off_sgr:string=sgr_of_seqitalic_fraktur_off_seqletfraktur_sgr:string=sgr_of_seqfraktur_seqletunderlined_sgr:string=sgr_of_seq underlined_seqletunderlined_off_sgr:string=sgr_of_seq underlined_off_seqletblink_sgr:string=sgr_of_seqblink_seqletblink_off_sgr:string=sgr_of_seq blink_off_seqletreverse_video_sgr:string=sgr_of_seq reverse_video_seqletreverse_video_off_sgr:string=sgr_of_seq reverse_video_off_seqletconceal_sgr:string=sgr_of_seqconceal_seqletconcel_off_sgr:string=sgr_of_seq concel_off_seqletcrossed_out_sgr:string=sgr_of_seq crossed_out_seqletcrossed_out_off_sgr:string=sgr_of_seq crossed_out_off_seqletframed_sgr:string=sgr_of_seqframed_seqletframed_encircled_off_sgr:string=sgr_of_seqframed_encircled_off_seqletencircled_sgr:string=sgr_of_seq encircled_seqletoverlined_sgr:string=sgr_of_seq overlined_seqletoverlined_off_sgr:string=sgr_of_seq overlined_off_seq(** {2 Integer and escape sequence for arbitrary style} *)letseq_of_style(s:style):seq=matchswith|Reset ->reset_seq|Bold->bold_seq|DoubleUnderlined->double_underlined_seq|Faint->faint_seq|Faint_bold_off->faint_bold_off_seq|Italic->italic_seq|Italic_fraktur_off->italic_fraktur_off_seq|Fraktur->fraktur_seq|Underlined->underlined_seq|Underlined_off->underlined_off_seq|Blink->blink_seq|Blink_off->blink_off_seq|Reverse_video->reverse_video_seq|Reverse_video_off->reverse_video_off_seq|Conceal->conceal_seq|Conceal_off->concel_off_seq|Crossed_out->crossed_out_seq|Crossed_out_off->crossed_out_off_seq|Framed->framed_seq|Framed_encircled_off->framed_encircled_off_seq|Encircled->encircled_seq|Overlined->overlined_seq|Overlined_off->overlined_off_seq|Default_font->default_font_seq|Fonti->font_seqi|Fgc->fg_color_seqc|Default_fg->default_fg_seq|Bgc->bg_color_seqc|Default_bg->default_bg_seqletsgr_of_style(s:style):string=s|>seq_of_style|>sgr_of_seqletseq_of_styles(s:stylelist):seq=s|>List.map seq_of_style|>List.flattenletstyles_sgr(s:stylelist):string=s|>seq_of_styles |>sgr_of_seq