Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
Source file console.ml
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265(** Color printing in terminals *)openCore(* http://www.termsys.demon.co.uk/vtansi.htm *)moduleAnsi=structletkill_line()=print_string"\027[2K"letbell()=print_string"\007"lethome_cursor()=print_string"\027[0G"letsave_cursor()=print_string"\027[s"letunsave_cursor()=print_string"\027[u"(* if it's good enough for git then it's good enough for us... *)letcapable=lazy(Unix.isattyUnix.stdout&&matchSys.getenv"TERM"with|Some"dumb"|None->false|Some_->true)moduleAttr=structtypecolor=[|`Black|`Red|`Green|`Yellow|`Blue|`Magenta|`Cyan|`White]typeattr=[|`Reset|`Bright|`Dim|`Underscore|`Blink|`Reverse|`Hidden]typet=[|attr|color|`Bgofcolor]letattr_to_int:attr->int=function|`Reset->0|`Bright->1|`Dim->2|`Underscore->4|`Blink->5|`Reverse->7|`Hidden->8letfg_to_int:color->int=function|`Black->30|`Red->31|`Green->32|`Yellow->33|`Blue->34|`Magenta->35|`Cyan->36|`White->37letbg_to_int:color->int=function|`Black->40|`Red->41|`Green->42|`Yellow->43|`Blue->44|`Magenta->45|`Cyan->46|`White->47letto_int:t->int=function|`Bgv->bg_to_intv|#colorasv->fg_to_intv|#attrasv->attr_to_intvletlist_to_string:tlist->string=function|[]->""|l->Printf.sprintf"\027[%sm"(String.concat~sep:";"(List.mapl~f:(funatt->string_of_int(to_intatt))))endtypecolor=Attr.colortypeattr=[|`Bright|`Dim|`Underscore|`Reverse|color|`Bgofcolor]letstring_with_attrstylestring=ifstyle=[]thenstringelseString.concat[Attr.list_to_string(style:>Attr.tlist);string;Attr.list_to_string[`Reset]]letoutput(style:attrlist)ocsstartlen=ifLazy.forcecapable&&style<>[]thenbeginOut_channel.output_stringoc(Attr.list_to_string(style:>Attr.tlist));Out_channel.outputoc~buf:s~pos:start~len;Out_channel.output_stringoc(Attr.list_to_string[`Reset]);Out_channel.flushocendelseOut_channel.outputoc~buf:s~pos:start~lenletoutput_string(style:attrlist)ocs=ifLazy.forcecapable&&style<>[]thenbeginOut_channel.output_stringoc(Attr.list_to_string(style:>Attr.tlist));Out_channel.output_stringocs;Out_channel.output_stringoc(Attr.list_to_string[`Reset]);Out_channel.flushocendelseOut_channel.output_stringocsletfprintf(style:attrlist)channelfmt=ifLazy.forcecapable&&style<>[]thenPrintf.fprintfchannel("%s"^^fmt^^"\027[0m%!")(Attr.list_to_string(style:>Attr.tlist))elsePrintf.fprintfchannel(fmt^^"%!")leteprintfstylefmt=fprintfstylestderrfmtletprintfstylefmt=fprintfstylestdoutfmtendletis_color_tty()=Lazy.forceAnsi.capablemoduleColumnize(In:sigtypetvallength:t->intend):sigvaliter:middle:(sep:In.t->In.t->int->unit)->last:(In.t->int->unit)->sep:In.t->In.tlist->int->unitend=structletlinescolumnsa=(Array.lengtha-1)/columns+1(** Size of an array printed out with this column configuration
(lines*chars per column)
*)letdimcolumnsa=letlines=linescolumnsainletrecloopcntcurrentacc=ifcnt=Array.lengthathenList.rev(current::acc)elseifcntmodlines=0thenloop(cnt+1)(In.lengtha.(cnt))(current::acc)elseloop(cnt+1)(max(In.lengtha.(cnt))current)accinlines,loop1(In.lengtha.(0))[]letrecline_len~sep_lenacc=function|[]->acc|[v]->acc+v|h::t->line_len~sep_len(acc+sep_len+h)tletfind_dim~sep_lenamax_len=letreclooplinescolscnt=let(nlines,ncols)=dim(cnt+1)ainifnlines>lines||lines=1(** we are not gaining in vertical space
anymore *)||line_len~sep_len0ncols>max_len(** we are overflowing *)thenArray.of_listcolselseloopnlinesncols(cnt+1)inletlines,cols=dim1ainlooplinescols1letcolumnizeacolumns=letlines=linescolumnsainletres=ref[]infori=lines-1downto0doletline_acc=ref[]inforj=columns-1downto0doletpos=i+j*linesinifpos<Array.lengthathenline_acc:=a.(pos)::!line_accdone;res:=!line_acc::!resdone;!resletrecfold_line~middle~lastsepaccpaddingline=matchline,paddingwith|[v],len::_->last~accv(len-In.lengthv)|h::t,len::tlen->fold_line~middle~lastsep(middle~acc~seph(len-In.lengthh))tlent|_->assertfalseletfold~init~middle~last~seplmax_len=ifl=[]theninitelseleta=Array.of_listlinletcolumns=find_dima~sep_len:(In.lengthsep)max_leninletres=columnizea(Array.lengthcolumns)inList.fold_leftres~f:(funaccline->fold_line~middle~lastsepacc(Array.to_listcolumns)line)~initletiter~middle~last=fold~init:()~last:(fun~acc:()->last)~middle:(fun~acc:()->middle)endletwidth()=matchLinux_ext.get_terminal_sizewith|Result.Error_->`Not_available|Result.Ok_whennot(Unix.isattyUnix.stdout)->`Not_a_tty|Result.Okget_size->`Cols(snd(get_size`Controlling))letprint_listocl=match(width():>[`Colsofint|`Not_a_tty|`Not_available])with|`Not_a_tty|`Not_available->List.iterl~f:(fun(s,_)->print_endlines)|`Colscols->letprint_styled(s,style)=Ansi.output_stringstyleocsinletsep=" ",[]inletlastv_=print_styledv;Out_channel.output_stringoc"\n"andmiddle~sepvpad_len=print_styledv;Out_channel.output_stringoc(String.makepad_len' ');print_styledsepinletmoduleCol=Columnize(structtypet=string*Ansi.attrlistletlength(s,_)=String.lengthsend)inCol.iter~sep~last~middlelcols