Skip to content

Commit 52fecaf

Browse files
committed
deprecate tty_size, replace with iosize & export it
1 parent 17341ff commit 52fecaf

File tree

18 files changed

+158
-105
lines changed

18 files changed

+158
-105
lines changed

base/Terminals.jl

Lines changed: 7 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ import Base:
2929
flush,
3030
read,
3131
readuntil,
32-
size,
32+
iosize,
3333
start_reading,
3434
stop_reading,
3535
write,
@@ -43,7 +43,7 @@ import Base:
4343
abstract TextTerminal <: Base.IO
4444

4545
# INTERFACE
46-
size(::TextTerminal) = error("Unimplemented")
46+
iosize(::TextTerminal) = error("Unimplemented")
4747
writepos(t::TextTerminal, x, y, s::Array{UInt8,1}) = error("Unimplemented")
4848
cmove(t::TextTerminal, x, y) = error("Unimplemented")
4949
getX(t::TextTerminal) = error("Unimplemented")
@@ -88,8 +88,8 @@ function writepos(t::TextTerminal, x, y, args...)
8888
cmove(t, x, y)
8989
write(t, args...)
9090
end
91-
width(t::TextTerminal) = size(t)[2]
92-
height(t::TextTerminal) = size(t)[1]
91+
width(t::TextTerminal) = iosize(t)[2]
92+
height(t::TextTerminal) = iosize(t)[1]
9393

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

134-
@windows_only begin
135-
ispty(s::Base.TTY) = s.ispty
136-
ispty(s) = false
137-
end
138134
@windows ? begin
139135
function raw!(t::TTYTerminal,raw::Bool)
140136
check_open(t.in_stream)
141-
if ispty(t.in_stream)
137+
if Base.ispty(t.in_stream)
142138
run(if raw
143139
`stty raw -echo onlcr -ocrnl opost`
144140
else
@@ -162,26 +158,8 @@ disable_bracketed_paste(t::UnixTerminal) = write(t.out_stream, "$(CSI)?2004l")
162158
end_keypad_transmit_mode(t::UnixTerminal) = # tput rmkx
163159
write(t.out_stream, "$(CSI)?1l\x1b>")
164160

165-
let s = zeros(Int32, 2)
166-
function Base.size(t::TTYTerminal)
167-
@windows_only if ispty(t.out_stream)
168-
try
169-
h,w = map(x->parse(Int,x),split(readall(open(`stty size`, "r", t.out_stream)[1])))
170-
w > 0 || (w = 80)
171-
h > 0 || (h = 24)
172-
return h,w
173-
catch
174-
return 24,80
175-
end
176-
end
177-
Base.uv_error("size (TTY)", ccall(:uv_tty_get_winsize,
178-
Int32, (Ptr{Void}, Ptr{Int32}, Ptr{Int32}),
179-
t.out_stream.handle, pointer(s,1), pointer(s,2)) != 0)
180-
w,h = s[1],s[2]
181-
w > 0 || (w = 80)
182-
h > 0 || (h = 24)
183-
(Int(h),Int(w))
184-
end
161+
function Base.iosize(t::UnixTerminal)
162+
return iosize(t.out_stream)
185163
end
186164

187165
clear(t::UnixTerminal) = write(t.out_stream, "\x1b[H\x1b[2J")

base/deprecated.jl

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -904,3 +904,17 @@ end
904904
export isreadable, iswritable, isexecutable
905905

906906
@deprecate RemoteRef RemoteChannel
907+
908+
function tty_size()
909+
depwarn("tty_size is deprecated. use `iosize(io)` as a replacement", :tty_size)
910+
if isdefined(Base, :active_repl)
911+
os = REPL.outstream(Base.active_repl)
912+
if isa(os, Terminals.TTYTerminal)
913+
return iosize(os)
914+
end
915+
end
916+
if isdefined(Base, :STDOUT)
917+
return iosize(STDOUT)
918+
end
919+
return iosize()
920+
end

base/dict.jl

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,7 @@ function _truncate_at_width_or_chars(str, width, chars="", truncmark="…")
6161
end
6262

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

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

9796
# Otherwise show more descriptively, with one line per key/value pair
98-
rows, cols = sz
9997
print(io, summary(t))
10098
isempty(t) && return
10199
print(io, ":")
102100
if limit
101+
sz = iosize(io)
102+
rows, cols = sz[1] - 3, sz[2]
103103
rows < 2 && (print(io, ""); return)
104104
cols < 12 && (cols = 12) # Minimum widths of 2 for key, 4 for value
105105
cols -= 6 # Subtract the widths of prefix " " separator " => "
@@ -113,6 +113,8 @@ function showdict{K,V}(io::IO, t::Associative{K,V}; compact = false,
113113
ks[i] = sprint(0, show, k, env=recur_io)
114114
keylen = clamp(length(ks[i]), keylen, div(cols, 3))
115115
end
116+
else
117+
rows = cols = 0
116118
end
117119

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

150152
show(io::IO, iter::Union{KeyIterator,ValueIterator}) = show(io, collect(iter))
151153

152-
showkv(iter::Union{KeyIterator,ValueIterator}; kw...) = showkv(STDOUT, iter; kw...)
153-
function showkv{T<:Union{KeyIterator,ValueIterator}}(io::IO, iter::T;
154-
sz=(s = tty_size(); (s[1]-3, s[2])))
155-
limit::Bool = limit_output(io)
156-
rows, cols = sz
154+
showkv(iter::Union{KeyIterator,ValueIterator}) = showkv(STDOUT, iter)
155+
function showkv{T<:Union{KeyIterator,ValueIterator}}(io::IO, iter::T)
157156
print(io, summary(iter))
158157
isempty(iter) && return
159158
print(io, ". ", T<:KeyIterator ? "Keys" : "Values", ":")
159+
limit::Bool = limit_output(io)
160160
if limit
161+
sz = iosize(io)
162+
rows, cols = sz[1] - 3, sz[2]
161163
rows < 2 && (print(io, ""); return)
162164
cols < 4 && (cols = 4)
163165
cols -= 2 # For prefix " "
164166
rows -= 2 # For summary and final ⋮ continuation lines
167+
else
168+
rows = cols = 0
165169
end
166170

167171
for (i, v) in enumerate(iter)

base/docs/helpdb/Profile.jl

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,17 @@
33
# Base.Profile
44

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

1615
"""
17-
print([io::IO = STDOUT,] data::Vector, lidict::Dict; format = :tree, combine = true, cols = tty_cols())
16+
print([io::IO = STDOUT,] data::Vector, lidict::Dict; format = :tree, combine = true)
1817
1918
Prints profiling results to `io`. This variant is used to examine results exported by a
2019
previous call to [`retrieve`](:func:`retrieve`). Supply the vector `data` of backtraces and

base/docs/utils.jl

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ end
9494
function repl_search(io::IO, s)
9595
pre = "search:"
9696
print(io, pre)
97-
printmatches(io, s, completions(s), cols=Base.tty_size()[2]-length(pre))
97+
printmatches(io, s, completions(s), cols = iosize(io)[2] - length(pre))
9898
println(io, "\n")
9999
end
100100

@@ -243,7 +243,7 @@ end
243243

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

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

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

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

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

271271
function print_correction(io, word)
272272
cors = levsort(word, accessible(current_module()))
273273
pre = "Perhaps you meant "
274274
print(io, pre)
275-
print_joined_cols(io, cors, ", ", " or "; cols = Base.tty_size()[2]-length(pre))
275+
print_joined_cols(io, cors, ", ", " or "; cols = iosize(io)[2] - length(pre))
276276
println(io)
277277
return
278278
end

base/env.jl

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -165,16 +165,3 @@ function withenv{T<:AbstractString}(f::Function, keyvals::Pair{T}...)
165165
end
166166
end
167167
withenv(f::Function) = f() # handle empty keyvals case; see #10853
168-
169-
## misc environment-related functionality ##
170-
171-
function tty_size()
172-
if isdefined(Base, :active_repl)
173-
os = REPL.outstream(Base.active_repl)
174-
if isa(os, Terminals.TTYTerminal)
175-
return size(os)
176-
end
177-
end
178-
return (parse(Int,get(ENV,"LINES","24")),
179-
parse(Int,get(ENV,"COLUMNS","80")))
180-
end

base/exports.jl

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1145,9 +1145,12 @@ export
11451145
getsockname,
11461146
htol,
11471147
hton,
1148+
IOContext,
1149+
iosize,
11481150
ismarked,
11491151
isopen,
11501152
isreadonly,
1153+
limit_output,
11511154
listen,
11521155
listenany,
11531156
ltoh,

base/interactiveutil.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ Print information about exported global variables in a module, optionally restri
430430
The memory consumption estimate is an approximate lower bound on the size of the internal structure of the object.
431431
"""
432432
function whos(io::IO=STDOUT, m::Module=current_module(), pattern::Regex=r"")
433-
maxline = tty_size()[2]
433+
maxline = iosize(io)[2]
434434
line = zeros(UInt8, maxline)
435435
head = PipeBuffer(maxline + 1)
436436
for v in sort!(names(m))

base/markdown/render/terminal/render.jl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
include("formatting.jl")
44

55
const margin = 2
6-
cols() = Base.tty_size()[2]
6+
cols(io) = iosize(io)[2]
77

88
function term(io::IO, content::Vector, cols)
99
isempty(content) && return
@@ -14,7 +14,7 @@ function term(io::IO, content::Vector, cols)
1414
term(io, content[end], cols)
1515
end
1616

17-
term(io::IO, md::MD, columns = cols()) = term(io, md.content, columns)
17+
term(io::IO, md::MD, columns = cols(io)) = term(io, md.content, columns)
1818

1919
function term(io::IO, md::Paragraph, columns)
2020
print(io, " "^margin)

base/pkg/entry.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -490,7 +490,7 @@ function resolve(
490490
end
491491

492492
function warnbanner(msg...; label="[ WARNING ]", prefix="")
493-
cols = Base.tty_size()[2]
493+
cols = Base.iosize(STDERR)[2]
494494
warn(prefix="", Base.cpad(label,cols,"="))
495495
println(STDERR)
496496
warn(prefix=prefix, msg...)

0 commit comments

Comments
 (0)