Skip to content

Commit 9c08e5d

Browse files
committed
Merge pull request #14661 from JuliaLang/tk/tput
add a try catch in hascolor so it returns false if tput not present
2 parents a5d23b0 + 9723d52 commit 9c08e5d

File tree

2 files changed

+138
-132
lines changed

2 files changed

+138
-132
lines changed

base/Terminals.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,14 @@ start_reading(t::UnixTerminal) = start_reading(t.in_stream)
179179
stop_reading(t::UnixTerminal) = stop_reading(t.in_stream)
180180
eof(t::UnixTerminal) = eof(t.in_stream)
181181

182-
@unix_only hascolor(t::TTYTerminal) = (startswith(t.term_type, "xterm") || success(`tput setaf 0`))
182+
@unix_only function hascolor(t::TTYTerminal)
183+
startswith(t.term_type, "xterm") && return true
184+
try
185+
return success(`tput setaf 0`)
186+
catch
187+
return false
188+
end
189+
end
183190
@windows_only hascolor(t::TTYTerminal) = true
184191

185192
end # module

test/repl.jl

Lines changed: 130 additions & 131 deletions
Original file line numberDiff line numberDiff line change
@@ -29,103 +29,103 @@ ccall(:jl_exit_on_sigint, Void, (Cint,), 0)
2929
# this should make sure nothing crashes without depending on how exactly the control
3030
# characters are being used.
3131
if @unix? true : (Base.windows_version() >= Base.WINDOWS_VISTA_VER)
32-
stdin_write, stdout_read, stderr_read, repl = fake_repl()
32+
stdin_write, stdout_read, stderr_read, repl = fake_repl()
3333

34-
repl.specialdisplay = Base.REPL.REPLDisplay(repl)
35-
repl.history_file = false
36-
37-
repltask = @async begin
38-
Base.REPL.run_repl(repl)
39-
end
40-
41-
sendrepl(cmd) = write(stdin_write,"inc || wait(b); r = $cmd; notify(c); r\r")
42-
43-
inc = false
44-
b = Condition()
45-
c = Condition()
46-
sendrepl("\"Hello REPL\"")
47-
inc=true
48-
begin
49-
notify(b)
50-
wait(c)
51-
end
52-
# Latex completions
53-
write(stdin_write, "\x32\\alpha\t")
54-
readuntil(stdout_read, "α")
55-
# Bracketed paste in search mode
56-
write(stdin_write, "\e[200~paste here ;)\e[201~")
57-
# Abort search (^C)
58-
write(stdin_write, '\x03')
59-
# Test basic completion in main mode
60-
write(stdin_write, "Base.REP\t")
61-
readuntil(stdout_read, "Base.REPL")
62-
write(stdin_write, '\x03')
63-
write(stdin_write, "\\alpha\t")
64-
readuntil(stdout_read,"α")
65-
write(stdin_write, '\x03')
66-
# Test cd feature in shell mode. We limit to 40 characters when
67-
# calling readuntil() to suppress the warning it (currently) gives for
68-
# long strings.
69-
origpwd = pwd()
70-
tmpdir = mktempdir()
71-
write(stdin_write, ";")
72-
readuntil(stdout_read, "shell> ")
73-
write(stdin_write, "cd $(escape_string(tmpdir))\n")
74-
readuntil(stdout_read, "cd $(escape_string(tmpdir))"[max(1,end-39):end])
75-
readuntil(stdout_read, realpath(tmpdir)[max(1,end-39):end])
76-
readuntil(stdout_read, "\n")
77-
readuntil(stdout_read, "\n")
78-
@test pwd() == realpath(tmpdir)
79-
write(stdin_write, ";")
80-
readuntil(stdout_read, "shell> ")
81-
write(stdin_write, "cd -\n")
82-
readuntil(stdout_read, origpwd[max(1,end-39):end])
83-
readuntil(stdout_read, "\n")
84-
readuntil(stdout_read, "\n")
85-
@test pwd() == origpwd
86-
write(stdin_write, ";")
87-
readuntil(stdout_read, "shell> ")
88-
write(stdin_write, "cd\n")
89-
readuntil(stdout_read, homedir()[max(1,end-39):end])
90-
readuntil(stdout_read, "\n")
91-
readuntil(stdout_read, "\n")
92-
@test pwd() == homedir()
93-
rm(tmpdir)
94-
cd(origpwd)
95-
96-
# Test that accepting a REPL result immediately shows up, not
97-
# just on the next keystroke
98-
write(stdin_write, "1+1\n") # populate history with a trivial input
99-
readline(stdout_read)
100-
write(stdin_write, "\e[A\n")
101-
t = Timer(10) do t
102-
isopen(t) || return
103-
error("Stuck waiting for the repl to write `1+1`")
104-
end
105-
# yield make sure this got processed
106-
readuntil(stdout_read, "1+1")
107-
close(t)
108-
109-
# Issue #10222
110-
# Test ignoring insert key in standard and prefix search modes
111-
write(stdin_write, "\e[2h\e[2h\n") # insert (VT100-style)
112-
@test search(readline(stdout_read), "[2h") == 0:-1
113-
readline(stdout_read)
114-
write(stdin_write, "\e[2~\e[2~\n") # insert (VT220-style)
115-
@test search(readline(stdout_read), "[2~") == 0:-1
116-
readline(stdout_read)
117-
write(stdin_write, "1+1\n") # populate history with a trivial input
118-
readline(stdout_read)
119-
write(stdin_write, "\e[A\e[2h\n") # up arrow, insert (VT100-style)
120-
readline(stdout_read)
121-
readline(stdout_read)
122-
write(stdin_write, "\e[A\e[2~\n") # up arrow, insert (VT220-style)
123-
readline(stdout_read)
124-
readline(stdout_read)
125-
126-
# Close REPL ^D
127-
write(stdin_write, '\x04')
128-
wait(repltask)
34+
repl.specialdisplay = Base.REPL.REPLDisplay(repl)
35+
repl.history_file = false
36+
37+
repltask = @async begin
38+
Base.REPL.run_repl(repl)
39+
end
40+
41+
sendrepl(cmd) = write(stdin_write,"inc || wait(b); r = $cmd; notify(c); r\r")
42+
43+
inc = false
44+
b = Condition()
45+
c = Condition()
46+
sendrepl("\"Hello REPL\"")
47+
inc=true
48+
begin
49+
notify(b)
50+
wait(c)
51+
end
52+
# Latex completions
53+
write(stdin_write, "\x32\\alpha\t")
54+
readuntil(stdout_read, "α")
55+
# Bracketed paste in search mode
56+
write(stdin_write, "\e[200~paste here ;)\e[201~")
57+
# Abort search (^C)
58+
write(stdin_write, '\x03')
59+
# Test basic completion in main mode
60+
write(stdin_write, "Base.REP\t")
61+
readuntil(stdout_read, "Base.REPL")
62+
write(stdin_write, '\x03')
63+
write(stdin_write, "\\alpha\t")
64+
readuntil(stdout_read,"α")
65+
write(stdin_write, '\x03')
66+
# Test cd feature in shell mode. We limit to 40 characters when
67+
# calling readuntil() to suppress the warning it (currently) gives for
68+
# long strings.
69+
origpwd = pwd()
70+
tmpdir = mktempdir()
71+
write(stdin_write, ";")
72+
readuntil(stdout_read, "shell> ")
73+
write(stdin_write, "cd $(escape_string(tmpdir))\n")
74+
readuntil(stdout_read, "cd $(escape_string(tmpdir))"[max(1,end-39):end])
75+
readuntil(stdout_read, realpath(tmpdir)[max(1,end-39):end])
76+
readuntil(stdout_read, "\n")
77+
readuntil(stdout_read, "\n")
78+
@test pwd() == realpath(tmpdir)
79+
write(stdin_write, ";")
80+
readuntil(stdout_read, "shell> ")
81+
write(stdin_write, "cd -\n")
82+
readuntil(stdout_read, origpwd[max(1,end-39):end])
83+
readuntil(stdout_read, "\n")
84+
readuntil(stdout_read, "\n")
85+
@test pwd() == origpwd
86+
write(stdin_write, ";")
87+
readuntil(stdout_read, "shell> ")
88+
write(stdin_write, "cd\n")
89+
readuntil(stdout_read, homedir()[max(1,end-39):end])
90+
readuntil(stdout_read, "\n")
91+
readuntil(stdout_read, "\n")
92+
@test pwd() == homedir()
93+
rm(tmpdir)
94+
cd(origpwd)
95+
96+
# Test that accepting a REPL result immediately shows up, not
97+
# just on the next keystroke
98+
write(stdin_write, "1+1\n") # populate history with a trivial input
99+
readline(stdout_read)
100+
write(stdin_write, "\e[A\n")
101+
t = Timer(10) do t
102+
isopen(t) || return
103+
error("Stuck waiting for the repl to write `1+1`")
104+
end
105+
# yield make sure this got processed
106+
readuntil(stdout_read, "1+1")
107+
close(t)
108+
109+
# Issue #10222
110+
# Test ignoring insert key in standard and prefix search modes
111+
write(stdin_write, "\e[2h\e[2h\n") # insert (VT100-style)
112+
@test search(readline(stdout_read), "[2h") == 0:-1
113+
readline(stdout_read)
114+
write(stdin_write, "\e[2~\e[2~\n") # insert (VT220-style)
115+
@test search(readline(stdout_read), "[2~") == 0:-1
116+
readline(stdout_read)
117+
write(stdin_write, "1+1\n") # populate history with a trivial input
118+
readline(stdout_read)
119+
write(stdin_write, "\e[A\e[2h\n") # up arrow, insert (VT100-style)
120+
readline(stdout_read)
121+
readline(stdout_read)
122+
write(stdin_write, "\e[A\e[2~\n") # up arrow, insert (VT220-style)
123+
readline(stdout_read)
124+
readline(stdout_read)
125+
126+
# Close REPL ^D
127+
write(stdin_write, '\x04')
128+
wait(repltask)
129129
end
130130

131131
function buffercontents(buf::IOBuffer)
@@ -347,41 +347,40 @@ let exename = joinpath(JULIA_HOME, Base.julia_exename())
347347

348348
# Test REPL in dumb mode
349349
@unix_only begin
350-
351-
const O_RDWR = Base.Filesystem.JL_O_RDWR
352-
const O_NOCTTY = Base.Filesystem.JL_O_NOCTTY
353-
354-
fdm = ccall(:posix_openpt,Cint,(Cint,),O_RDWR|O_NOCTTY)
355-
fdm == -1 && error("Failed to open PTY master")
356-
rc = ccall(:grantpt,Cint,(Cint,),fdm)
357-
rc != 0 && error("grantpt failed")
358-
rc = ccall(:unlockpt,Cint,(Cint,),fdm)
359-
rc != 0 && error("unlockpt")
360-
361-
fds = ccall(:open,Cint,(Ptr{UInt8},Cint),ccall(:ptsname,Ptr{UInt8},(Cint,),fdm), O_RDWR|O_NOCTTY)
362-
363-
# slave
364-
slave = RawFD(fds)
365-
master = Base.TTY(RawFD(fdm); readable = true)
366-
367-
nENV = copy(ENV)
368-
nENV["TERM"] = "dumb"
369-
p = spawn(setenv(`$exename --startup-file=no --quiet`,nENV),slave,slave,slave)
370-
output = readuntil(master,"julia> ")
371-
if ccall(:jl_running_on_valgrind,Cint,()) == 0
372-
# If --trace-children=yes is passed to valgrind, we will get a
373-
# valgrind banner here, not just the prompt.
374-
@test output == "julia> "
375-
end
376-
write(master,"1\nquit()\n")
377-
378-
wait(p)
379-
output = readuntil(master,' ')
380-
@test output == "1\r\nquit()\r\n1\r\n\r\njulia> "
381-
@test nb_available(master) == 0
382-
ccall(:close,Cint,(Cint,),fds) # XXX: this causes the kernel to throw away all unread data on the pty
383-
close(master)
384-
350+
const O_RDWR = Base.Filesystem.JL_O_RDWR
351+
const O_NOCTTY = Base.Filesystem.JL_O_NOCTTY
352+
353+
fdm = ccall(:posix_openpt, Cint, (Cint,), O_RDWR|O_NOCTTY)
354+
fdm == -1 && error("Failed to open PTY master")
355+
rc = ccall(:grantpt, Cint, (Cint,), fdm)
356+
rc != 0 && error("grantpt failed")
357+
rc = ccall(:unlockpt, Cint, (Cint,), fdm)
358+
rc != 0 && error("unlockpt")
359+
360+
fds = ccall(:open, Cint, (Ptr{UInt8}, Cint),
361+
ccall(:ptsname, Ptr{UInt8}, (Cint,), fdm), O_RDWR|O_NOCTTY)
362+
363+
# slave
364+
slave = RawFD(fds)
365+
master = Base.TTY(RawFD(fdm); readable = true)
366+
367+
nENV = copy(ENV)
368+
nENV["TERM"] = "dumb"
369+
p = spawn(setenv(`$exename --startup-file=no --quiet`,nENV),slave,slave,slave)
370+
output = readuntil(master,"julia> ")
371+
if ccall(:jl_running_on_valgrind,Cint,()) == 0
372+
# If --trace-children=yes is passed to valgrind, we will get a
373+
# valgrind banner here, not just the prompt.
374+
@test output == "julia> "
375+
end
376+
write(master,"1\nquit()\n")
377+
378+
wait(p)
379+
output = readuntil(master,' ')
380+
@test output == "1\r\nquit()\r\n1\r\n\r\njulia> "
381+
@test nb_available(master) == 0
382+
ccall(:close,Cint,(Cint,),fds) # XXX: this causes the kernel to throw away all unread data on the pty
383+
close(master)
385384
end
386385

387386
# Test stream mode

0 commit comments

Comments
 (0)