Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RFC: Use real terminal size for show (fix #7295 and #4513) #7297

Merged
merged 5 commits into from
Jun 18, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 12 additions & 31 deletions base/Terminals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ import Base:
write,
writemime

immutable Size
width
height
end

## TextTerminal ##

abstract TextTerminal <: Base.IO
Expand Down Expand Up @@ -87,8 +82,8 @@ function writepos(t::TextTerminal, x, y, args...)
cmove(t, x, y)
write(t, args...)
end
width(t::TextTerminal) = size(t).width
height(t::TextTerminal) = size(t).height
width(t::TextTerminal) = size(t)[2]
height(t::TextTerminal) = size(t)[1]

# For terminals with buffers
flush(t::TextTerminal) = nothing
Expand All @@ -111,21 +106,6 @@ type TerminalBuffer <: UnixTerminal
out_stream::Base.IO
end

type FakeTerminal <: UnixTerminal
in_stream::Base.IO
out_stream::Base.IO
err_stream::Base.IO
hascolor::Bool
raw::Bool
size::Size
FakeTerminal(stdin,stdout,stderr,hascolor=true) =
new(stdin,stdout,stderr,hascolor,false,Size(80,80))
end

hascolor(t::FakeTerminal) = t.hascolor
raw!(t::FakeTerminal, raw::Bool) = t.raw = raw
size(t::FakeTerminal) = t.size

type TTYTerminal <: UnixTerminal
term_type::ASCIIString
in_stream::Base.TTY
Expand Down Expand Up @@ -170,15 +150,16 @@ disable_bracketed_paste(t::UnixTerminal) = write(t.out_stream, "$(CSI)?2004l")
end_keypad_transmit_mode(t::UnixTerminal) = # tput rmkx
write(t.out_stream, "$(CSI)?1l\x1b>")

function size(t::TTYTerminal)
s = zeros(Int32, 2)
Base.uv_error("size (TTY)", ccall((@windows ? :jl_tty_get_winsize : :uv_tty_get_winsize),
Int32, (Ptr{Void}, Ptr{Int32}, Ptr{Int32}),
t.out_stream.handle, pointer(s,1), pointer(s,2)) != 0)
w,h = s[1],s[2]
w > 0 || (w = 80)
h > 0 || (h = 24)
Size(w,h)
let s = zeros(Int32, 2)
function Base.size(t::TTYTerminal)
Base.uv_error("size (TTY)", ccall((@windows ? :jl_tty_get_winsize : :uv_tty_get_winsize),
Int32, (Ptr{Void}, Ptr{Int32}, Ptr{Int32}),
t.out_stream.handle, pointer(s,1), pointer(s,2)) != 0)
w,h = s[1],s[2]
w > 0 || (w = 80)
h > 0 || (h = 24)
(int(h),int(w))
end
end

clear(t::UnixTerminal) = write(t.out_stream, "\x1b[H\x1b[2J")
Expand Down
9 changes: 9 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,15 @@ Set{T<:Number}(xs::T...) = Set{T}(xs)
@deprecate readsfrom(cmd, args...) open(cmd, "r", args...)
@deprecate writesto(cmd, args...) open(cmd, "w", args...)

function tty_rows()
depwarn("tty_rows() is deprecated, use tty_size() instead", :tty_rows)
tty_size()[1]
end
Copy link
Sponsor Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Couldn't this be done with

@deprecate tty_rows() tty_size()[1]

?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That was my first thought, but @deprecate calls export tty_rows, which seems undesirable since tty_rows was not previously exported.

function tty_cols()
depwarn("tty_cols() is deprecated, use tty_size() instead", :tty_cols)
tty_size()[2]
end

# 0.3 discontinued functions

scale!{T<:Base.LinAlg.BlasReal}(X::Array{T}, s::Complex) = error("scale!: Cannot scale a real array by a complex value in-place. Use scale(X::Array{Real}, s::Complex) instead.")
Expand Down
6 changes: 4 additions & 2 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ end

showdict(t::Associative; kw...) = showdict(STDOUT, t; kw...)
function showdict{K,V}(io::IO, t::Associative{K,V}; limit::Bool = false,
rows = tty_rows()-3, cols = tty_cols())
sz=(s = tty_size(); (s[1]-3, s[2])))
rows, cols = sz
print(io, summary(t))
isempty(t) && return
print(io, ":")
Expand Down Expand Up @@ -118,7 +119,8 @@ show(io::IO, iter::Union(KeyIterator,ValueIterator)) = show(io, collect(iter))

showkv(iter::Union(KeyIterator,ValueIterator); kw...) = showkv(STDOUT, iter; kw...)
function showkv{T<:Union(KeyIterator,ValueIterator)}(io::IO, iter::T; limit::Bool = false,
rows = tty_rows()-3, cols = tty_cols())
sz=(s = tty_size(); (s[1]-3, s[2])))
rows, cols = sz
print(io, summary(iter))
isempty(iter) && return
print(io, ". ", T<:KeyIterator ? "Keys" : "Values", ":")
Expand Down
11 changes: 9 additions & 2 deletions base/env.jl
Original file line number Diff line number Diff line change
Expand Up @@ -184,5 +184,12 @@ end

## misc environment-related functionality ##

tty_cols() = parseint(Int32, get(ENV,"COLUMNS","80"), 10)
tty_rows() = parseint(Int32, get(ENV,"LINES","25"), 10)
function tty_size()
if isdefined(Base, :active_repl)
os = REPL.outstream(Base.active_repl)
if isa(os, Terminals.TTYTerminal)
return size(os)
end
end
return (24, 80)
end
2 changes: 1 addition & 1 deletion base/linalg/factorization.jl
Original file line number Diff line number Diff line change
Expand Up @@ -497,7 +497,7 @@ end
full(A::HessenbergQ) = LAPACK.orghr!(1, size(A.factors, 1), copy(A.factors), A.τ)

# Also printing of QRQs
print_matrix(io::IO, A::Union(QRPackedQ,QRCompactWYQ,HessenbergQ), rows::Integer, cols::Integer, punct...) = print_matrix(io, full(A), rows, cols, punct...)
print_matrix(io::IO, A::Union(QRPackedQ,QRCompactWYQ,HessenbergQ), sz::(Integer, Integer), punct...) = print_matrix(io, full(A), sz, punct...)


#######################
Expand Down
5 changes: 3 additions & 2 deletions base/pkg/entry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -596,11 +596,12 @@ function check_metadata()
end

function warnbanner(msg...; label="[ WARNING ]", prefix="")
warn(prefix="", Base.cpad(label,Base.tty_cols(),"="))
cols = Base.tty_size()[2]
warn(prefix="", Base.cpad(label,cols,"="))
println(STDERR)
warn(prefix=prefix, msg...)
println(STDERR)
warn(prefix="", "="^Base.tty_cols())
warn(prefix="", "="^cols)
end

function build!(pkgs::Vector, errs::Dict, seen::Set=Set())
Expand Down
2 changes: 1 addition & 1 deletion base/profile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ __init__() = init(1_000_000, 0.001)

clear() = ccall(:jl_profile_clear_data, Void, ())

function print{T<:Unsigned}(io::IO, data::Vector{T} = fetch(), lidict::Dict = getdict(data); format = :tree, C = false, combine = true, cols = Base.tty_cols())
function print{T<:Unsigned}(io::IO, data::Vector{T} = fetch(), lidict::Dict = getdict(data); format = :tree, C = false, combine = true, cols = Base.tty_size()[2])
if format == :tree
tree(io, data, lidict, C, combine, cols)
elseif format == :flat
Expand Down
11 changes: 6 additions & 5 deletions base/show.jl
Original file line number Diff line number Diff line change
Expand Up @@ -827,15 +827,15 @@ function print_matrix_vdots(io::IO,
end

function print_matrix(io::IO, X::AbstractVecOrMat,
rows::Integer = tty_rows()-4,
cols::Integer = tty_cols(),
sz::(Integer, Integer) = (s = tty_size(); (s[1]-4, s[2])),
pre::String = " ",
sep::String = " ",
post::String = "",
hdots::String = " \u2026 ",
vdots::String = "\u22ee",
ddots::String = " \u22f1 ",
hmod::Integer = 5, vmod::Integer = 5)
rows, cols = sz
cols -= length(pre) + length(post)
presp = repeat(" ", length(pre))
postsp = ""
Expand Down Expand Up @@ -1001,7 +1001,8 @@ end
showarray(X::AbstractArray; kw...) = showarray(STDOUT, X; kw...)
function showarray(io::IO, X::AbstractArray;
header::Bool=true, limit::Bool=_limit_output,
rows = tty_rows()-4, cols = tty_cols(), repr=false)
sz = (s = tty_size(); (s[1]-4, s[2])), repr=false)
rows, cols = sz
header && print(io, summary(X))
if !isempty(X)
header && println(io, ":")
Expand All @@ -1024,10 +1025,10 @@ function showarray(io::IO, X::AbstractArray;
else
punct = (" ", " ", "")
if ndims(X)<=2
print_matrix(io, X, rows, cols, punct...)
print_matrix(io, X, sz, punct...)
else
show_nd(io, X, limit,
(io,slice)->print_matrix(io,slice,rows,cols,punct...),
(io,slice)->print_matrix(io,slice,sz,punct...),
!repr)
end
end
Expand Down
2 changes: 1 addition & 1 deletion base/sparse/sparsematrix.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ countnz(S::SparseMatrixCSC) = countnz(S.nzval)

function Base.showarray(io::IO, S::SparseMatrixCSC;
header::Bool=true, limit::Bool=Base._limit_output,
rows = Base.tty_rows(), repr=false)
rows = Base.tty_size()[1], repr=false)
# TODO: repr?

if header
Expand Down
4 changes: 2 additions & 2 deletions test/collections.jl
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ for d in (["\n" => "\n", "1" => "\n", "\n" => "2"],
for cols in (12, 40, 80), rows in (2, 10, 24)
# Ensure output is limited as requested
s = IOBuffer()
Base.showdict(s, d, limit=true, rows=rows, cols=cols)
Base.showdict(s, d, limit=true, sz=(rows, cols))
out = split(takebuf_string(s),'\n')
for line in out[2:end]
@test strwidth(line) <= cols
Expand All @@ -188,7 +188,7 @@ for d in (["\n" => "\n", "1" => "\n", "\n" => "2"],

for f in (keys, values)
s = IOBuffer()
Base.showkv(s, f(d), limit=true, rows=rows, cols=cols)
Base.showkv(s, f(d), limit=true, sz=(rows, cols))
out = split(takebuf_string(s),'\n')
for line in out[2:end]
@test strwidth(line) <= cols
Expand Down
16 changes: 15 additions & 1 deletion test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,21 @@ Base.link_pipe(stdin_read,true,stdin_write,true)
Base.link_pipe(stdout_read,true,stdout_write,true)
Base.link_pipe(stderr_read,true,stderr_write,true)

repl = Base.REPL.LineEditREPL(Base.Terminals.FakeTerminal(stdin_read, stdout_write, stderr_write))
type FakeTerminal <: Base.Terminals.UnixTerminal
in_stream::Base.IO
out_stream::Base.IO
err_stream::Base.IO
hascolor::Bool
raw::Bool
FakeTerminal(stdin,stdout,stderr,hascolor=true) =
new(stdin,stdout,stderr,hascolor,false)
end

Base.Terminals.hascolor(t::FakeTerminal) = t.hascolor
Base.Terminals.raw!(t::FakeTerminal, raw::Bool) = t.raw = raw
Base.Terminals.size(t::FakeTerminal) = (24, 80)

repl = Base.REPL.LineEditREPL(FakeTerminal(stdin_read, stdout_write, stderr_write))
# In the future if we want we can add a test that the right object
# gets displayed by intercepting the display
repl.specialdisplay = Base.REPL.REPLDisplay(repl)
Expand Down