Getting terminal width from within an ExUnit Formatter

I’m writing my own ExUnit formatter, and its dependent on knowing the terminal width to properly output its results.

I make a call to the Erlang io:columns/0 function in the init of my formatter. Instead of giving me a tuple of {:ok, <termwidth>}, I get {:error, :enotsup}. Erlang docs indicate that this is because I’m trying to read the terminal width of a non-tty.

Question is, how can I get the termwidth of the current TTY outputting the results of my ExUnit test run. If they are not being output to a TTY that is acceptable, I can code a fallback.

1 Like

I get this just fine. Are you ‘sure’ your output is a proper terminal stream? What is it precisely?

Had my question answered elsewhere, by José.

Basically the issue is that its not a terminal if it isn’t running inside an iex session.

$> elixir -e "IO.inspect :io.columns"
{:error, :enotsup}

$> iex -e "IO.inspect :io.columns"
Erlang/OTP 21 [erts-10.3.4] [source] [64-bit] [smp:8:8] [ds:8:8:10] [async-threads:1] [hipe]

{:ok, 238}

So for anyone else writing an ExUnit formatter or anything else that needs to get the column count, you have to run within IEx, or manually specify the tty device you’re targeting.

3 Likes

Ah that’s correct yeah, didn’t know you were running it not in iex. Erlang needs a shell running to pull that information.

1 Like