Skip to content

Commit 02aeb44

Browse files
committed
Merge pull request #13825 from JuliaLang/jn/iocontext
introduce IOContext and ImmutableDict to fix some of show, print, & friends
2 parents 91ba0f7 + 52fecaf commit 02aeb44

35 files changed

+726
-409
lines changed

base/Enums.jl

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,9 +93,12 @@ macro enum(T,syms...)
9393
end
9494
end
9595
function Base.show(io::IO,x::$(esc(typename)))
96-
print(io, x, "::", $(esc(typename)), " = ", Int(x))
96+
if Base.limit_output(io)
97+
print(io, x)
98+
else
99+
print(io, x, "::", $(esc(typename)), " = ", Int(x))
100+
end
97101
end
98-
Base.showcompact(io::IO,x::$(esc(typename))) = print(io, x)
99102
function Base.writemime(io::IO,::MIME"text/plain",::Type{$(esc(typename))})
100103
print(io, "Enum ", $(esc(typename)), ":")
101104
for (sym, i) in $vals

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/complex.jl

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -59,25 +59,24 @@ complex(x::Real, y::Real) = Complex(x, y)
5959
complex(x::Real) = Complex(x)
6060
complex(z::Complex) = z
6161

62-
function complex_show(io::IO, z::Complex, compact::Bool)
62+
function show(io::IO, z::Complex)
6363
r, i = reim(z)
64-
compact ? showcompact(io,r) : show(io,r)
64+
compact = limit_output(io)
65+
showcompact_lim(io, r)
6566
if signbit(i) && !isnan(i)
6667
i = -i
6768
print(io, compact ? "-" : " - ")
6869
else
6970
print(io, compact ? "+" : " + ")
7071
end
71-
compact ? showcompact(io, i) : show(io, i)
72+
showcompact_lim(io, i)
7273
if !(isa(i,Integer) && !isa(i,Bool) || isa(i,AbstractFloat) && isfinite(i))
7374
print(io, "*")
7475
end
7576
print(io, "im")
7677
end
77-
complex_show(io::IO, z::Complex{Bool}, compact::Bool) =
78+
show(io::IO, z::Complex{Bool}) =
7879
print(io, z == im ? "im" : "Complex($(z.re),$(z.im))")
79-
show(io::IO, z::Complex) = complex_show(io, z, false)
80-
showcompact(io::IO, z::Complex) = complex_show(io, z, true)
8180

8281
function read{T<:Real}(s::IO, ::Type{Complex{T}})
8382
r = read(s,T)

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

0 commit comments

Comments
 (0)