-
Notifications
You must be signed in to change notification settings - Fork 1
/
TermLayouts.jl
174 lines (142 loc) · 4.75 KB
/
TermLayouts.jl
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
170
171
172
173
174
module TermLayouts
using Preferences
using Configurations
include("core.jl")
include("config.jl")
include("io.jl")
include("errors.jl")
include("strings.jl")
"Loads TermLayouts preferences from the environment"
function loadprefs()
panel_defaults = Dict(
"left" => Dict(
"width" => 70,
"title" => "",
"title_color" => "",
"border_color" => "red"),
"right" => Dict(
"width" => 30,
"title" => "",
"title_color" => "",
"border_color" => "blue"
)
)
panel_prefs = @load_preference("panels", panel_defaults)
if (panel_prefs["left"]["width"] + panel_prefs["right"]["width"]) > 100
@warn "Panel widths add up to more than 100, cropping right panel"
panel_prefs["right"]["width"] = 100 - panel_prefs["left"]["width"]
end
return TermLayoutsPreferences(from_dict(PanelPrefs, panel_prefs["left"]), from_dict(PanelPrefs, panel_prefs["right"]))
end
"Activate TermLayouts, and spawn a new REPL session"
function run()
state = TermLayoutsState()
prefs = loadprefs()
# Create pipes
inputpipe = Pipe()
outputpipe = Pipe()
errpipe = Pipe()
Base.link_pipe!(inputpipe, reader_supports_async=true, writer_supports_async=true)
Base.link_pipe!(outputpipe, reader_supports_async=true, writer_supports_async=true)
Base.link_pipe!(errpipe, reader_supports_async=true, writer_supports_async=true)
true_stdout = stdout
redirect_stdout(outputpipe.in)
# Link pipes to REPL
term = REPL.Terminals.TTYTerminal("dumb", inputpipe.out, outputpipe.in, errpipe.in)
repl = REPL.LineEditREPL(term, true)
repl.specialdisplay = REPL.REPLDisplay(repl)
repl.history_file = false
# Start REPL
print(true_stdout, "starting REPL...")
start_eval_backend(state)
# Clear screen before proceeding
# Still doesn't work on windows for some reason
print(true_stdout, "\e[3J")
print(true_stdout, "\e[1;1H")
repltask = @async begin
REPL.run_repl(repl)
end
# Setup some variables that describe the state of the REPL
should_exit = false
keyboard_io = stdin
replstr = ""
# Create a console-like object that will make it easier to parse ANSI commands
virtual_console = EditableString([], 0, 0, "\e[0m")
# Read output from REPL and process it asynchronously
@async while !eof(outputpipe)
data = String(readavailable(outputpipe))
# Read the output, and process it relative to the current state of the console
parseANSI(virtual_console, data, true)
sleep(1e-2)
end
# Render the layout asynchronously
@async while !should_exit
# Create panels and give them default sizes
fullh, fullw = displaysize(true_stdout)
lpanelw = Int(round(fullw * prefs.panel1.width / 100))
rpanelw = Int(round(fullw * prefs.panel2.width / 100))
# Grab the string that describes the current state of the fake console, and set the panel's text to it
outstr = to_string(virtual_console, true)
# Reshape text
reshaped = Term.reshape_text(outstr, lpanelw - 3)
# Get last few lines
reshaped_lines = split(reshaped, "\n")
reshaped_cropped = reshaped_lines[max(1, length(reshaped_lines) - fullh + 3):end]
last_line = string(reshaped_cropped[max(1, length(reshaped_cropped) - 1)])
text_before_cursor = simplifyANSI(last_line, false)
# TODO: Allow cursor to move around after
cursorX = length(text_before_cursor) + 3
cursorY = length(reshaped_cropped)
final_outstr = join(reshaped_cropped, "\n")
replstr = final_outstr
# Create panels
lpanel = Term.Panel(
replstr,
width=lpanelw,
height=fullh,
style=prefs.panel1.border_color,
title=length(prefs.panel1.title) > 0 ? prefs.panel1.title : nothing,
title_style=prefs.panel1.title_color,
)
rpanel = Term.Panel(
width=rpanelw,
height=fullh,
style=prefs.panel2.border_color,
title=length(prefs.panel2.title) > 0 ? prefs.panel2.title : nothing,
title_style=prefs.panel2.title_color,
)
top = lpanel * rpanel
print(true_stdout, "\e[3J")
print(true_stdout, "\e[1;1H")
print(true_stdout, string(top))
print(true_stdout, "\e[" * string(cursorY) * ";" * string(cursorX) * "H")
sleep(1 / 30)
end
while !should_exit
# Read in keys
control_value = :CONTROL_VOID
control_value = read_key(keyboard_io)
# Exit on Ctrl+C
if control_value == :EXIT
should_exit = true
else
# Pass down keys to the REPL
write(inputpipe.in, control_value)
end
sleep(1e-2)
end
# Delete line (^U) and close REPL (^D)
write(inputpipe.in, "\x15\x04")
redirect_stdout(true_stdout)
print(true_stdout, "\e[2J")
print(true_stdout, "\e[3J")
print(true_stdout, "\e[1;1H")
Base.wait(repltask)
t = @async begin
close(inputpipe.in)
close(outputpipe.in)
close(errpipe.in)
end
Base.wait(t)
end
end