【问题标题】:Why does :io.columns fail when running through the escript为什么:io.columns 在通过 escript 运行时会失败
【发布时间】:2017-02-26 00:24:56
【问题描述】:

我正在使用 CLI 界面,并且想获取当前正在使用的终端的列数。

我找到了the documentation for :io.columns,它在 iex 中就像一个魅力,但是一旦我用 mix 编译了 escript 二进制文件,通过运行mix escript.build,然后我运行它,然后:io.columns 返回{error, enotsup}

IO 设备没有正常冒泡是怎么回事?如何访问列号?

【问题讨论】:

标签: io command-line-interface elixir


【解决方案1】:

还没有发现为什么它不起作用,但我会发布我使用的解决方案:直接与 bash 交谈

$ tput cols 给出列数,用 bash 表示,所以

灵药:System.cmd("tput", ["cols"])
对于二郎::os.cmd('tput cols')

【讨论】:

  • Mac 系统:Erlang/OTP 20 [erts-9.0.4] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads :0] [hipe] [kernel-poll:false] [dtrace] Eshell V9.0.4(使用 ^G 中止)(test1@127.0.0.1)1> os:cmd("tput cols")。 "80\n" (test1@127.0.0.1)2> io:columns()。 {ok,141} 但是是 bash $ tput cols 141 `
【解决方案2】:

这是由于最近的 Erlang/OTP 版本中的confirmed bug。该错误很可能会在下一个版本中修复。

在修复错误之前,以下解决方法可能很有用:

-module(test).

-export([main/1]).

main(_) ->
    io:format("Nr of columns: ~p~n",[my_columns()]).

my_columns() ->
    Parent = self(),
    spawn(
      fun()->
              Port = erlang:open_port({spawn, "tput cols"},
                                      [use_stdio, in, stream,{line, 10000}]),
              Columns =
                  receive
                      {Port, {data, {_,Line}}} ->
                          erlang:list_to_integer(Line)
                  end,
              Parent ! {my_columns_msg, Columns},
              spawn(fun Reader() ->
                            receive
                                {_Port, {data, {_,_}}} ->
                                    Reader();
                                _ -> ok
                            end
                    end)
      end),
    receive
        {my_columns_msg, Columns} -> Columns
    end.

请注意,调用os:cmd("tput cols")(在@nichochar 的答案中建议)不起作用,因为os:cmd/1 调用tputstdoutstdin 将不会连接到终端。

【讨论】:

    猜你喜欢
    • 2016-11-13
    • 1970-01-01
    • 2012-09-01
    • 2012-10-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 2021-08-27
    相关资源
    最近更新 更多