正如 legoscia 所指出的,io:format/2 试图在漂亮的打印元素和行长等方面保持礼貌,您可以通过格式字符串参数对每个元素进行调整。请注意,这意味着如果您使用格式控制序列,格式本身就是您控制每个元素的输出长度的地方。
允许您声明行总长度(相对于每个元素)的替代方法是io_lib:print/4。我倾向于发现这个特定的功能对日志消息格式等更有用:
1> T = {"Some really long things","are in this tuple","but it won't really matter","because they will be in line"}.
{"Some really long things","are in this tuple",
"but it won't really matter","because they will be in line"}
2> io:format("~tp~n", [T]).
{"Some really long things","are in this tuple","but it won't really matter",
"because they will be in line"}
3> S = io_lib:print(T, 1, 1000, -1).
[123,
["\"Some really long things\"",44,"\"are in this tuple\"",
44,"\"but it won't really matter\"",44,
"\"because they will be in line\""],
125]
4> lists:flatten(S).
"{\"Some really long things\",\"are in this tuple\",\"but it won't really matter\",\"because they will be in line\"}"
5> io:format("~ts~n", [S]).
{"Some really long things","are in this tuple","but it won't really matter","because they will be in line"}
ok
仔细阅读io 和io_lib 的文档。实际上,其中包含很多不时弹出的功能。
请记住,如果目标是控制终端本身,您还可以通过打印单个字符和使用 ASCII 控制序列来完成很多有趣的工作。如果您要解决的问题是构建基于文本的界面或类似的东西,则使用 io:put_chars/1 逐字符或逐位屏幕更新字符串输出可能是一个强大的工具。