Search code examples
ocamlutop

Change utop output width


When displaying long lists and other large values, utop wraps them at about 80 columns, even when my terminal window is wider. How can I change the output width?

The only thing that I have found that might have provided the solution is UTop.size, which has type LTerm_geom.size React.signal, and seems to correctly record the size of my terminal window. In this example my terminal window had dimensions 164x37:

# #require "react";;
# #require "lambda-term";;
# React.S.value UTop.size;;
- : LTerm_geom.size = {LTerm_geom.rows = 37; cols = 164}

However, the value of cols doesn't seem to affect how values are displayed. For example, this is copied from the same session (with line breaks as they were displayed):

# List.hd algs;;
- : (int list * float) list =
[([2; 1; 0], 1.); ([2; 1], 0.54148398267); ([2; 0], 0.677137905076);
 ([2], 0.218621887745); ([1; 0], 0.781378112255); ([1], 0.322862094924);
 ([0], 0.45851601733); ([], 0.)]

Solution

  • There is a function called UTop.set_margin_function:

    μ> #typeof "UTop.set_margin_function";;
    val UTop.set_margin_function : (LTerm_geom.size -> int option) -> unit
    

    Here is a simplistic example usage:

    μ> UTop.set_margin_function (fun _ -> Some 150);;
    

    After this point utop will use about 150 columns for printing out the results.

    Here is an example session (shell + utop):

    > utop -version
    The universal toplevel for OCaml, version 1.19.3, compiled for OCaml version 4.04.1
    
    > cat ~/.ocamlinit
    (* Added by OPAM. *)
    let () =
      try Topdirs.dir_directory (Sys.getenv "OCAML_TOPLEVEL_PATH")
      with Not_found -> ()
    ;;
    
    > cat ~/.utoprc
    cat: .utoprc: No such file or directory
    
    > utop
    
    utop # gen_nat_list 1 50;;   (* gen_nat_list is some test function *)
    - : int list =
    [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21; 22;
    23; 24; 25; 26; 27; 28; 29; 30; 31; 32; 33; 34; 35; 36; 37; 38; 39; 40; 41;
    42; 43; 44; 45; 46; 47; 48; 49; 50]
    
    utop # UTop.set_margin_function (fun _ -> Some 150);;
    
    utop # gen_nat_ist 1 50;;
    - : int list =
    [1; 2; 3; 4; 5; 6; 7; 8; 9; 10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21; 22; 23; 24; 25; 26; 27; 28; 29; 30; 31; 32; 33; 34; 35; 36; 37; 38; 39;
     40; 41; 42; 43; 44; 45; 46; 47; 48; 49; 50]