我如何在Haskell中漂亮地打印表格? [英] How do I pretty-print a table in Haskell?

查看:129
本文介绍了我如何在Haskell中漂亮地打印表格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



例如...

$ b $

我想在Haskell中打印一个类似于表格的数据结构(列列表)。 b

  Table [
StrColstrings[a,bc,c],
IntColints[ 1,30,-2],
DblColdouble[2.0,4.5,-3.2]]



$ p

pre
$ a $ 1 $
bc30 4.5
c-2 -3.2

目前我已经实施在Haskell平台的最新版本附带的Text.PrettyPrint中提供了此功能。不幸的是,< +>操作符使多行文档彼此对角地取向。

例如

 (文本a$ + $ textb)+< +> (textc$ + $ textd)

呈现为

  a 
bc
d

而不是

  ac 
bd

因此,我将这些单元格转置并首先水平合并,然后垂直合并,但这会导致列未对齐。



在较旧的表格漂亮打印问题中,八月指的是添加一些更多的代码,使列自动适应最广泛的条目。

我猜每个列的最大长度的大小文本会做到这一点,除了这个函数看起来并不是Haskell平台Text.PrettyPrint模块的一部分(我认为它在密切相关的Text.Pretty包中)。



什么是实现这种漂亮打印功能的最简单的非哈希方式?这是我项目的一小部分,所以我想避免编写自己的漂亮打印文件,如果可以的话,我宁愿避免模块依赖项。

解决方案

http://hackage.haskell.org/package/boxes


包装盒



使用一个简单的盒子模型...



I would like to prettyprint a table-like data structure in Haskell (a list of columns).

For example...

Table [
    StrCol "strings" ["a", "bc", "c"],
    IntCol "ints" [1, 30, -2],
    DblCol "doubles" [2.0, 4.5, -3.2]]

Should render something like...

strings ints doubles
    "a"    1     2.0
   "bc"   30     4.5
    "c"   -2    -3.2

Currently I have implemented this functionality in Text.PrettyPrint that comes with the most recent version of the Haskell Platform. Unfortunately the <+> operator orients multi-line documents "diagonally" from each other.

eg

(text "a" $+$ text "b") <+> (text "c" $+$ text "d")

renders as

a
b c
  d

rather than

a c
b d

As a result, I transpose the cells and merge them horizontally first, then vertically, but this results in the columns not being aligned.

In an older table pretty-printing question, augustss refers to adding some more code to have the columns automatically adapt to the widest entry.

I'm guessing that "sizedText" with the maximum length of each column would do this, except that this function doesn't appear to be part of the Haskell Platform Text.PrettyPrint module (I think it's in the closely related Text.Pretty package).

What's the simplest non-hacky way of implementing this pretty-print functionality? This is a small part of my project so I'd like to avoid writing my own pretty-printing, and I'd prefer to avoid module dependencies if I can.

解决方案

http://hackage.haskell.org/package/boxes

The boxes package

A pretty-printing library for laying out text in two dimensions, using a simple box model....

这篇关于我如何在Haskell中漂亮地打印表格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆