]>
git.saurik.com Git - cycript.git/blob - Display.cpp
1 /* Cycript - Optimizing JavaScript Compiler/Runtime
2 * Copyright (C) 2009-2013 Jay Freeman (saurik)
5 /* GNU General Public License, Version 3 {{{ */
7 * Cycript is free software: you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published
9 * by the Free Software Foundation, either version 3 of the License,
10 * or (at your option) any later version.
12 * Cycript is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with Cycript. If not, see <http://www.gnu.org/licenses/>.
25 #ifdef HAVE_READLINE_H
28 #include <readline/readline.h>
31 #if RL_READLINE_VERSION >= 0x0600
33 #include <sys/ioctl.h>
35 #include "Highlight.hpp"
39 typedef std::complex<int> CYCursor
;
41 extern "C" int rl_display_fixed
;
42 extern "C" int _rl_vis_botlin
;
43 extern "C" int _rl_last_c_pos
;
44 extern "C" int _rl_last_v_pos
;
50 unsigned CYDisplayWidth() {
52 if (ioctl(1, TIOCGWINSZ
, &info
) != -1)
54 return tgetnum(const_cast<char *>("co"));
57 void CYDisplayOutput_(int (*put
)(int), const char *&data
) {
60 if (next
== '\0' || next
== CYIgnoreEnd
)
67 CYCursor
CYDisplayOutput(int (*put
)(int), int width
, const char *data
, ssize_t offset
= 0) {
68 CYCursor
point(current_
);
81 CYDisplayOutput_(put
, data
);
87 current_
+= CYCursor(0, 1);
88 if (current_
.imag() == width
)
90 current_
= CYCursor(current_
.real() + 1, 0);
99 void CYDisplayMove_(char *negative
, char *positive
, int offset
) {
101 putp(tparm(negative
, -offset
));
103 putp(tparm(positive
, offset
));
106 void CYDisplayMove(CYCursor target
) {
107 CYCursor
offset(target
- current_
);
109 CYDisplayMove_(parm_up_cursor
, parm_down_cursor
, offset
.real());
111 if (char *parm
= tparm(column_address
, target
.imag()))
114 CYDisplayMove_(parm_left_cursor
, parm_right_cursor
, offset
.imag());
119 void CYDisplayUpdate() {
120 rl_display_fixed
= 1;
122 current_
= CYCursor(_rl_last_v_pos
, _rl_last_c_pos
);
124 #if RL_READLINE_VERSION >= 0x0600
125 const char *prompt(rl_display_prompt
);
127 const char *prompt(rl_prompt
);
130 std::ostringstream stream
;
131 CYLexerHighlight(rl_line_buffer
, rl_end
, stream
, true);
132 std::string
string(stream
.str());
133 const char *buffer(string
.c_str());
135 int width(CYDisplayWidth());
136 if (width_
!= width
) {
137 current_
= CYCursor();
138 CYDisplayOutput(NULL
, width
, prompt
);
139 current_
= CYDisplayOutput(NULL
, width
, buffer
, point_
);
142 CYDisplayMove(CYCursor());
143 CYDisplayOutput(putchar
, width
, prompt
);
144 CYCursor
target(CYDisplayOutput(putchar
, width
, stream
.str().c_str(), rl_point
));
146 _rl_vis_botlin
= current_
.real();
148 if (current_
.imag() == 0)
149 CYDisplayOutput(putchar
, width
, " ");
152 CYDisplayMove(target
);
155 _rl_last_v_pos
= current_
.real();
156 _rl_last_c_pos
= current_
.imag();