Skip to content

Commit

Permalink
deprecate tty_size, replace with iosize & export it
Browse files Browse the repository at this point in the history
  • Loading branch information
vtjnash committed Dec 24, 2015
1 parent e36063d commit 9875f49
Show file tree
Hide file tree
Showing 18 changed files with 158 additions and 105 deletions.
36 changes: 7 additions & 29 deletions base/Terminals.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import Base:
flush,
read,
readuntil,
size,
iosize,
start_reading,
stop_reading,
write,
Expand All @@ -43,7 +43,7 @@ import Base:
abstract TextTerminal <: Base.IO

# INTERFACE
size(::TextTerminal) = error("Unimplemented")
iosize(::TextTerminal) = error("Unimplemented")
writepos(t::TextTerminal, x, y, s::Array{UInt8,1}) = error("Unimplemented")
cmove(t::TextTerminal, x, y) = error("Unimplemented")
getX(t::TextTerminal) = error("Unimplemented")
Expand Down Expand Up @@ -88,8 +88,8 @@ function writepos(t::TextTerminal, x, y, args...)
cmove(t, x, y)
write(t, args...)
end
width(t::TextTerminal) = size(t)[2]
height(t::TextTerminal) = size(t)[1]
width(t::TextTerminal) = iosize(t)[2]
height(t::TextTerminal) = iosize(t)[1]

# For terminals with buffers
flush(t::TextTerminal) = nothing
Expand Down Expand Up @@ -131,14 +131,10 @@ cmove_line_up(t::UnixTerminal, n) = (cmove_up(t, n); cmove_col(t, 0))
cmove_line_down(t::UnixTerminal, n) = (cmove_down(t, n); cmove_col(t, 0))
cmove_col(t::UnixTerminal, n) = write(t.out_stream, "$(CSI)$(n)G")

@windows_only begin
ispty(s::Base.TTY) = s.ispty
ispty(s) = false
end
@windows ? begin
function raw!(t::TTYTerminal,raw::Bool)
check_open(t.in_stream)
if ispty(t.in_stream)
if Base.ispty(t.in_stream)
run(if raw
`stty raw -echo onlcr -ocrnl opost`
else
Expand All @@ -162,26 +158,8 @@ 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>")

let s = zeros(Int32, 2)
function Base.size(t::TTYTerminal)
@windows_only if ispty(t.out_stream)
try
h,w = map(x->parse(Int,x),split(readall(open(`stty size`, "r", t.out_stream)[1])))
w > 0 || (w = 80)
h > 0 || (h = 24)
return h,w
catch
return 24,80
end
end
Base.uv_error("size (TTY)", ccall(: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
function Base.iosize(t::UnixTerminal)
return iosize(t.out_stream)
end

clear(t::UnixTerminal) = write(t.out_stream, "\x1b[H\x1b[2J")
Expand Down
14 changes: 14 additions & 0 deletions base/deprecated.jl
Original file line number Diff line number Diff line change
Expand Up @@ -904,3 +904,17 @@ end
export isreadable, iswritable, isexecutable

@deprecate RemoteRef RemoteChannel

function tty_size()
depwarn("tty_size is deprecated. use `iosize(io)` as a replacement", :tty_size)
if isdefined(Base, :active_repl)
os = REPL.outstream(Base.active_repl)
if isa(os, Terminals.TTYTerminal)
return iosize(os)
end
end
if isdefined(Base, :STDOUT)
return iosize(STDOUT)
end
return iosize()
end
20 changes: 12 additions & 8 deletions base/dict.jl
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ function _truncate_at_width_or_chars(str, width, chars="", truncmark="…")
end

showdict(t::Associative; kw...) = showdict(STDOUT, t; kw...)
function showdict{K,V}(io::IO, t::Associative{K,V}; compact = false,
sz=(s = tty_size(); (s[1]-3, s[2])))
function showdict{K,V}(io::IO, t::Associative{K,V}; compact = false)
(:SHOWN_SET => t) in io && (print(io, "#= circular reference =#"); return)

recur_io = IOContext(io, :SHOWN_SET => t)
Expand Down Expand Up @@ -95,11 +94,12 @@ function showdict{K,V}(io::IO, t::Associative{K,V}; compact = false,
end

# Otherwise show more descriptively, with one line per key/value pair
rows, cols = sz
print(io, summary(t))
isempty(t) && return
print(io, ":")
if limit
sz = iosize(io)
rows, cols = sz[1] - 3, sz[2]
rows < 2 && (print(io, ""); return)
cols < 12 && (cols = 12) # Minimum widths of 2 for key, 4 for value
cols -= 6 # Subtract the widths of prefix " " separator " => "
Expand All @@ -113,6 +113,8 @@ function showdict{K,V}(io::IO, t::Associative{K,V}; compact = false,
ks[i] = sprint(0, show, k, env=recur_io)
keylen = clamp(length(ks[i]), keylen, div(cols, 3))
end
else
rows = cols = 0
end

for (i, (k, v)) in enumerate(t)
Expand Down Expand Up @@ -149,19 +151,21 @@ summary{T<:Union{KeyIterator,ValueIterator}}(iter::T) =

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;
sz=(s = tty_size(); (s[1]-3, s[2])))
limit::Bool = limit_output(io)
rows, cols = sz
showkv(iter::Union{KeyIterator,ValueIterator}) = showkv(STDOUT, iter)
function showkv{T<:Union{KeyIterator,ValueIterator}}(io::IO, iter::T)
print(io, summary(iter))
isempty(iter) && return
print(io, ". ", T<:KeyIterator ? "Keys" : "Values", ":")
limit::Bool = limit_output(io)
if limit
sz = iosize(io)
rows, cols = sz[1] - 3, sz[2]
rows < 2 && (print(io, ""); return)
cols < 4 && (cols = 4)
cols -= 2 # For prefix " "
rows -= 2 # For summary and final ⋮ continuation lines
else
rows = cols = 0
end

for (i, v) in enumerate(iter)
Expand Down
7 changes: 3 additions & 4 deletions base/docs/helpdb/Profile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,17 @@
# Base.Profile

"""
print([io::IO = STDOUT,] [data::Vector]; format = :tree, C = false, combine = true, cols = tty_cols())
print([io::IO = STDOUT,] [data::Vector]; format = :tree, C = false, combine = true)
Prints profiling results to `io` (by default, `STDOUT`). If you do not supply a `data`
vector, the internal buffer of accumulated backtraces will be used. `format` can be `:tree`
or `:flat`. If `C==true`, backtraces from C and Fortran code are shown. `combine==true`
merges instruction pointers that correspond to the same line of code. `cols` controls the
width of the display.
merges instruction pointers that correspond to the same line of code.
"""
Profile.print(io::IO = STDOUT, data::Vector=?)

"""
print([io::IO = STDOUT,] data::Vector, lidict::Dict; format = :tree, combine = true, cols = tty_cols())
print([io::IO = STDOUT,] data::Vector, lidict::Dict; format = :tree, combine = true)
Prints profiling results to `io`. This variant is used to examine results exported by a
previous call to [`retrieve`](:func:`retrieve`). Supply the vector `data` of backtraces and
Expand Down
12 changes: 6 additions & 6 deletions base/docs/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ end
function repl_search(io::IO, s)
pre = "search:"
print(io, pre)
printmatches(io, s, completions(s), cols=Base.tty_size()[2]-length(pre))
printmatches(io, s, completions(s), cols = iosize(io)[2] - length(pre))
println(io, "\n")
end

Expand Down Expand Up @@ -243,7 +243,7 @@ end

printmatch(args...) = printfuzzy(STDOUT, args...)

function printmatches(io::IO, word, matches; cols = Base.tty_size()[2])
function printmatches(io::IO, word, matches; cols = iosize(io)[2])
total = 0
for match in matches
total + length(match) + 1 > cols && break
Expand All @@ -254,9 +254,9 @@ function printmatches(io::IO, word, matches; cols = Base.tty_size()[2])
end
end

printmatches(args...; cols = Base.tty_size()[2]) = printmatches(STDOUT, args..., cols = cols)
printmatches(args...; cols = iosize(STDOUT)[2]) = printmatches(STDOUT, args..., cols = cols)

function print_joined_cols(io::IO, ss, delim = "", last = delim; cols = Base.tty_size()[2])
function print_joined_cols(io::IO, ss, delim = "", last = delim; cols = iosize(io)[2])
i = 0
total = 0
for i = 1:length(ss)
Expand All @@ -266,13 +266,13 @@ function print_joined_cols(io::IO, ss, delim = "", last = delim; cols = Base.tty
print_joined(io, ss[1:i], delim, last)
end

print_joined_cols(args...; cols = Base.tty_size()[2]) = print_joined_cols(STDOUT, args...; cols=cols)
print_joined_cols(args...; cols = iosize(STDOUT)[2]) = print_joined_cols(STDOUT, args...; cols=cols)

function print_correction(io, word)
cors = levsort(word, accessible(current_module()))
pre = "Perhaps you meant "
print(io, pre)
print_joined_cols(io, cors, ", ", " or "; cols = Base.tty_size()[2]-length(pre))
print_joined_cols(io, cors, ", ", " or "; cols = iosize(io)[2] - length(pre))
println(io)
return
end
Expand Down
13 changes: 0 additions & 13 deletions base/env.jl
Original file line number Diff line number Diff line change
Expand Up @@ -165,16 +165,3 @@ function withenv{T<:AbstractString}(f::Function, keyvals::Pair{T}...)
end
end
withenv(f::Function) = f() # handle empty keyvals case; see #10853

## misc environment-related functionality ##

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 (parse(Int,get(ENV,"LINES","24")),
parse(Int,get(ENV,"COLUMNS","80")))
end
3 changes: 3 additions & 0 deletions base/exports.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1145,9 +1145,12 @@ export
getsockname,
htol,
hton,
IOContext,
iosize,
ismarked,
isopen,
isreadonly,
limit_output,
listen,
listenany,
ltoh,
Expand Down
2 changes: 1 addition & 1 deletion base/interactiveutil.jl
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ Print information about exported global variables in a module, optionally restri
The memory consumption estimate is an approximate lower bound on the size of the internal structure of the object.
"""
function whos(io::IO=STDOUT, m::Module=current_module(), pattern::Regex=r"")
maxline = tty_size()[2]
maxline = iosize(io)[2]
line = zeros(UInt8, maxline)
head = PipeBuffer(maxline + 1)
for v in sort!(names(m))
Expand Down
4 changes: 2 additions & 2 deletions base/markdown/render/terminal/render.jl
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
include("formatting.jl")

const margin = 2
cols() = Base.tty_size()[2]
cols(io) = iosize(io)[2]

function term(io::IO, content::Vector, cols)
isempty(content) && return
Expand All @@ -14,7 +14,7 @@ function term(io::IO, content::Vector, cols)
term(io, content[end], cols)
end

term(io::IO, md::MD, columns = cols()) = term(io, md.content, columns)
term(io::IO, md::MD, columns = cols(io)) = term(io, md.content, columns)

function term(io::IO, md::Paragraph, columns)
print(io, " "^margin)
Expand Down
2 changes: 1 addition & 1 deletion base/pkg/entry.jl
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ function resolve(
end

function warnbanner(msg...; label="[ WARNING ]", prefix="")
cols = Base.tty_size()[2]
cols = Base.iosize(STDERR)[2]
warn(prefix="", Base.cpad(label,cols,"="))
println(STDERR)
warn(prefix=prefix, msg...)
Expand Down
8 changes: 7 additions & 1 deletion base/profile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,13 @@ end

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_size()[2], maxdepth::Int = typemax(Int), sortedby::Symbol = :filefuncline)
function print{T<:Unsigned}(io::IO, data::Vector{T} = fetch(), lidict::Dict = getdict(data);
format = :tree,
C = false,
combine = true,
maxdepth::Int = typemax(Int),
sortedby::Symbol = :filefuncline)
cols = Base.iosize(io)[2]
if format == :tree
tree(io, data, lidict, C, combine, cols, maxdepth)
elseif format == :flat
Expand Down
13 changes: 7 additions & 6 deletions base/range.jl
Original file line number Diff line number Diff line change
Expand Up @@ -253,20 +253,21 @@ It figures out the width in characters of each element, and if they
end up too wide, it shows the first and last elements separated by a
horizontal elipsis. Typical output will look like `1.0,2.0,3.0,…,4.0,5.0,6.0`.
`print_range(io, r, sz, pre, sep, post, hdots)` uses optional
parameters `sz` for the (rows,cols) of the screen,
`pre` and `post` characters for each printed row, `sep` separator string between
printed elements, `hdots` string for the horizontal ellipsis.
`print_range(io, r, pre, sep, post, hdots)` uses optional
parameters `pre` and `post` characters for each printed row,
`sep` separator string between printed elements,
`hdots` string for the horizontal ellipsis.
"""
function print_range(io::IO, r::Range,
sz::Tuple{Integer, Integer} = (s = tty_size(); (s[1]-4, s[2])),
pre::AbstractString = " ",
sep::AbstractString = ",",
post::AbstractString = "",
hdots::AbstractString = ",\u2026,") # horiz ellipsis
# This function borrows from print_matrix() in show.jl
# and should be called by writemime (replutil.jl) and by display()
screenheight, screenwidth = sz
limit = limit_output(io)
sz = iosize(io)
screenheight, screenwidth = sz[1] - 4, sz[2]
screenwidth -= length(pre) + length(post)
postsp = ""
sepsize = length(sep)
Expand Down
Loading

0 comments on commit 9875f49

Please sign in to comment.