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
175
176
177
178
179
|
#include <chrono>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <wctype.h>
#include "util.hxx"
#include "terminal.hxx"
#undef min
namespace replxx {
int mk_wcwidth( char32_t );
int virtual_render( char32_t const* display_, int size_, int& x_, int& y_, int screenColumns_, int promptLen_, char32_t* rendered_, int* renderedSize_ ) {
char32_t* out( rendered_ );
int visibleCount( 0 );
auto render = [&rendered_, &renderedSize_, &out, &visibleCount]( char32_t c_, bool visible_, bool renderAttributes_ = true ) {
if ( rendered_ && renderedSize_ && renderAttributes_ ) {
*out = c_;
++ out;
if ( visible_ ) {
++ visibleCount;
}
}
};
bool wrapped( false );
auto advance_cursor = [&x_, &y_, &screenColumns_, &wrapped]( int by_ = 1 ) {
wrapped = false;
x_ += by_;
if ( x_ >= screenColumns_ ) {
x_ = 0;
++ y_;
wrapped = true;
}
};
bool const renderAttributes( !!tty::out );
int pos( 0 );
while ( pos < size_ ) {
char32_t c( display_[pos] );
if ( ( c == '\n' ) || ( c == '\r' ) ) {
render( c, true );
if ( ( c == '\n' ) && ! wrapped ) {
++ y_;
}
x_ = promptLen_;
++ pos;
continue;
}
if ( c == '\b' ) {
render( c, true );
-- x_;
if ( x_ < 0 ) {
x_ = screenColumns_ - 1;
-- y_;
}
++ pos;
continue;
}
if ( c == '\033' ) {
render( c, false, renderAttributes );
++ pos;
if ( pos >= size_ ) {
advance_cursor( 2 );
continue;
}
c = display_[pos];
if ( c != '[' ) {
advance_cursor( 2 );
continue;
}
render( c, false, renderAttributes );
++ pos;
if ( pos >= size_ ) {
advance_cursor( 3 );
continue;
}
int codeLen( 0 );
while ( pos < size_ ) {
c = display_[pos];
if ( ( c != ';' ) && ( ( c < '0' ) || ( c > '9' ) ) ) {
break;
}
render( c, false, renderAttributes );
++ codeLen;
++ pos;
}
if ( pos >= size_ ) {
continue;
}
c = display_[pos];
if ( c != 'm' ) {
advance_cursor( 3 + codeLen );
continue;
}
render( c, false, renderAttributes );
++ pos;
continue;
}
if ( is_control_code( c ) ) {
render( c, true );
advance_cursor( 2 );
++ pos;
continue;
}
int wcw( mk_wcwidth( c ) );
if ( wcw < 0 ) {
break;
}
render( c, true );
advance_cursor( wcw );
++ pos;
}
if ( rendered_ && renderedSize_ ) {
*renderedSize_ = out - rendered_;
}
return ( visibleCount );
}
char const* ansi_color( Replxx::Color color_ ) {
int unsigned code( static_cast<int unsigned>( color_ ) );
int unsigned fg( code & 0xFFu );
int unsigned bg( ( code >> 8 ) & 0xFFu );
char const* bold( ( code & color::BOLD ) != 0 ? ";1" : "" );
char const* underline = ( ( code & color::UNDERLINE ) != 0 ? ";4" : "" );
static int const MAX_COLOR_CODE_SIZE( 32 );
static char colorBuffer[MAX_COLOR_CODE_SIZE];
int pos( 0 );
if ( ( code & static_cast<int unsigned>( Replxx::Color::DEFAULT ) ) != 0 ) {
pos = snprintf( colorBuffer, MAX_COLOR_CODE_SIZE, "\033[0%s%sm", underline, bold );
} else if ( fg <= static_cast<int unsigned>( Replxx::Color::LIGHTGRAY ) ) {
pos = snprintf( colorBuffer, MAX_COLOR_CODE_SIZE, "\033[0;22;3%d%s%sm", fg, underline, bold );
} else if ( fg <= static_cast<int unsigned>( Replxx::Color::WHITE ) ) {
#ifdef _WIN32
static bool const has256colorDefault( true );
#else
static bool const has256colorDefault( false );
#endif
static char const* TERM( getenv( "TERM" ) );
static bool const has256color( TERM ? ( strstr( TERM, "256" ) != nullptr ) : has256colorDefault );
static char const* ansiEscapeCodeTemplate = has256color ? "\033[0;9%d%s%sm" : "\033[0;1;3%d%s%sm";
pos = snprintf( colorBuffer, MAX_COLOR_CODE_SIZE, ansiEscapeCodeTemplate, fg - static_cast<int>( Replxx::Color::GRAY ), underline, bold );
} else {
pos = snprintf( colorBuffer, MAX_COLOR_CODE_SIZE, "\033[0;38;5;%d%s%sm", fg, underline, bold );
}
if ( ( code & color::BACKGROUND_COLOR_SET ) == 0 ) {
return colorBuffer;
}
if ( bg <= static_cast<int unsigned>( Replxx::Color::WHITE ) ) {
if ( bg <= static_cast<int unsigned>( Replxx::Color::LIGHTGRAY ) ) {
snprintf( colorBuffer + pos, MAX_COLOR_CODE_SIZE - pos, "\033[4%dm", bg );
} else {
snprintf( colorBuffer + pos, MAX_COLOR_CODE_SIZE - pos, "\033[10%dm", bg - static_cast<int>( Replxx::Color::GRAY ) );
}
} else {
snprintf( colorBuffer + pos, MAX_COLOR_CODE_SIZE - pos, "\033[48;5;%dm", bg );
}
return colorBuffer;
}
std::string now_ms_str( void ) {
std::chrono::milliseconds ms( std::chrono::duration_cast<std::chrono::milliseconds>( std::chrono::system_clock::now().time_since_epoch() ) );
time_t t( ms.count() / 1000 );
tm broken;
#ifdef _WIN32
#define localtime_r( t, b ) localtime_s( ( b ), ( t ) )
#endif
localtime_r( &t, &broken );
#undef localtime_r
static int const BUFF_SIZE( 32 );
char str[BUFF_SIZE];
strftime( str, BUFF_SIZE, "%Y-%m-%d %H:%M:%S.", &broken );
snprintf( str + sizeof ( "YYYY-mm-dd HH:MM:SS" ), 5, "%03d", static_cast<int>( ms.count() % 1000 ) );
return ( str );
}
}
|