forked from JuliaLang/julia
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminals.jl
More file actions
169 lines (133 loc) · 5.25 KB
/
Copy pathTerminals.jl
File metadata and controls
169 lines (133 loc) · 5.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
# This file is a part of Julia. License is MIT: https://julialang.org/license
module Terminals
export
AbstractTerminal,
TextTerminal,
UnixTerminal,
TerminalBuffer,
TTYTerminal,
cmove,
cmove_col,
cmove_down,
cmove_left,
cmove_line_down,
cmove_line_up,
cmove_right,
cmove_up,
disable_bracketed_paste,
enable_bracketed_paste,
end_keypad_transmit_mode,
getX,
getY,
hascolor,
pos,
raw!
import Base:
displaysize,
flush,
pipe_reader,
pipe_writer
## AbstractTerminal: abstract supertype of all terminals ##
abstract type AbstractTerminal <: Base.AbstractPipe end
## TextTerminal ##
abstract type TextTerminal <: AbstractTerminal end
# Terminal interface:
pipe_reader(::TextTerminal) = error("Unimplemented")
pipe_writer(::TextTerminal) = error("Unimplemented")
displaysize(::TextTerminal) = error("Unimplemented")
cmove(t::TextTerminal, x, y) = error("Unimplemented")
getX(t::TextTerminal) = error("Unimplemented")
getY(t::TextTerminal) = error("Unimplemented")
pos(t::TextTerminal) = (getX(t), getY(t))
# Relative moves (Absolute position fallbacks)
cmove_up(t::TextTerminal, n) = cmove(getX(t), max(1, getY(t)-n))
cmove_up(t) = cmove_up(t, 1)
cmove_down(t::TextTerminal, n) = cmove(getX(t), max(height(t), getY(t)+n))
cmove_down(t) = cmove_down(t, 1)
cmove_left(t::TextTerminal, n) = cmove(max(1, getX(t)-n), getY(t))
cmove_left(t) = cmove_left(t, 1)
cmove_right(t::TextTerminal, n) = cmove(max(width(t), getX(t)+n), getY(t))
cmove_right(t) = cmove_right(t, 1)
cmove_line_up(t::TextTerminal, n) = cmove(1, max(1, getY(t)-n))
cmove_line_up(t) = cmove_line_up(t, 1)
cmove_line_down(t::TextTerminal, n) = cmove(1, max(height(t), getY(t)+n))
cmove_line_down(t) = cmove_line_down(t, 1)
cmove_col(t::TextTerminal, c) = cmove(c, getY(t))
# Defaults
hascolor(::TextTerminal) = false
# Utility Functions
width(t::TextTerminal) = (displaysize(t)::Tuple{Int,Int})[2]
height(t::TextTerminal) = (displaysize(t)::Tuple{Int,Int})[1]
# For terminals with buffers
flush(t::TextTerminal) = nothing
clear(t::TextTerminal) = error("Unimplemented")
clear_line(t::TextTerminal, row) = error("Unimplemented")
clear_line(t::TextTerminal) = error("Unimplemented")
raw!(t::TextTerminal, raw::Bool) = error("Unimplemented")
beep(t::TextTerminal) = nothing
enable_bracketed_paste(t::TextTerminal) = nothing
disable_bracketed_paste(t::TextTerminal) = nothing
## UnixTerminal ##
abstract type UnixTerminal <: TextTerminal end
pipe_reader(t::UnixTerminal) = t.in_stream::IO
pipe_writer(t::UnixTerminal) = t.out_stream::IO
@nospecialize
mutable struct TerminalBuffer <: UnixTerminal
out_stream::IO
end
mutable struct TTYTerminal <: UnixTerminal
term_type::String
in_stream::IO
out_stream::IO
err_stream::IO
end
@specialize
const CSI = "\x1b["
cmove_up(t::UnixTerminal, n) = write(t.out_stream, "$(CSI)$(n)A")
cmove_down(t::UnixTerminal, n) = write(t.out_stream, "$(CSI)$(n)B")
cmove_right(t::UnixTerminal, n) = write(t.out_stream, "$(CSI)$(n)C")
cmove_left(t::UnixTerminal, n) = write(t.out_stream, "$(CSI)$(n)D")
cmove_line_up(t::UnixTerminal, n) = (cmove_up(t, n); cmove_col(t, 1))
cmove_line_down(t::UnixTerminal, n) = (cmove_down(t, n); cmove_col(t, 1))
cmove_col(t::UnixTerminal, n) = (write(t.out_stream, '\r'); n > 1 && cmove_right(t, n-1))
if Sys.iswindows()
function raw!(t::TTYTerminal,raw::Bool)
if Base.ispty(t.in_stream)
try
run((raw ? `stty raw -echo onlcr -ocrnl opost` : `stty sane`),
t.in_stream, t.out_stream, t.err_stream)
true
catch ex
# Fall back to ccall if stty fails (e.g., in some CI environments)
if ex isa ProcessFailedException
ccall(:jl_tty_set_mode, Int32, (Ptr{Cvoid},Int32), t.in_stream.handle::Ptr{Cvoid}, raw) == 0
else
rethrow()
end
end
else
ccall(:jl_tty_set_mode, Int32, (Ptr{Cvoid},Int32), t.in_stream.handle::Ptr{Cvoid}, raw) == 0
end
end
else
function raw!(t::TTYTerminal, raw::Bool)
ccall(:jl_tty_set_mode, Int32, (Ptr{Cvoid},Int32), t.in_stream.handle::Ptr{Cvoid}, raw) == 0
end
end
# eval some of these definitions to insert CSI as a constant string
@eval enable_bracketed_paste(t::UnixTerminal) = write(t.out_stream, $"$(CSI)?2004h")
@eval disable_bracketed_paste(t::UnixTerminal) = write(t.out_stream, $"$(CSI)?2004l")
@eval end_keypad_transmit_mode(t::UnixTerminal) = # tput rmkx
write(t.out_stream, $"$(CSI)?1l\x1b>")
@eval clear(t::UnixTerminal) = write(t.out_stream, $"$(CSI)H$(CSI)2J")
@eval clear_line(t::UnixTerminal) = write(t.out_stream, $"\r$(CSI)0K")
beep(t::UnixTerminal) = write(t.err_stream,"\x7")
Base.displaysize(t::UnixTerminal) = displaysize(t.out_stream)::Tuple{Int,Int}
hascolor(t::TTYTerminal) = get(t.out_stream, :color, false)::Bool
# use cached value of have_color
Base.in(key_value::Pair, t::TTYTerminal) = in(key_value, pipe_writer(t))
Base.haskey(t::TTYTerminal, key) = haskey(pipe_writer(t), key)
Base.getindex(t::TTYTerminal, key) = getindex(pipe_writer(t), key)
Base.get(t::TTYTerminal, key, default) = get(pipe_writer(t), key, default)
Base.peek(t::TTYTerminal, ::Type{T}) where {T} = peek(t.in_stream, T)::T
end # module