Deedle icon indicating copy to clipboard operation
Deedle copied to clipboard

Make fsi print more beautiful

Open ingted opened this issue 7 months ago • 0 comments

Image

Modify frame.Format

  member frame.Format(rowStartCount, rowEndCount, columnStartCount, columnEndCount, printTypes, showInfo) =
    try
      //add this function
      let formatWithAutoPadding
            (maxCellWidth: int)
            (rows: string[][])
            : string[][] =

          // 1. Truncate 長度
          let truncated =
            rows
            |> Array.map (fun row ->
                row |> Array.map (fun s ->
                  if s.Length > maxCellWidth then
                    s.Substring(0, maxCellWidth - 3) + "..."  // 末尾加省略號
                  else s))

          // 2. 計算每欄最大長度
          let colCount =
            truncated
            |> Array.map Array.length
            |> Array.max

          let colWidths =
            [| for j in 0 .. colCount - 1 ->
                 truncated
                 |> Array.map (fun row -> if j < row.Length then row.[j].Length else 0)
                 |> Array.max |]

          // 3. PadRight 並 4. Join
          truncated
          |> Array.map (fun row ->
              [| for j in 0 .. colCount - 1 ->
                   let cell = if j < row.Length then row.[j] else ""
                   cell.PadRight(colWidths.[j]) |]
              //|> String.concat " "
              )


      let formattedtable =
        frame.FormatStrings(rowStartCount, rowEndCount, columnStartCount, columnEndCount, printTypes)
        |> formatWithAutoPadding 50 //add this line

and the output in FSI will be cell-column-aligned.

ingted avatar Jul 24 '25 23:07 ingted