11#include " readline/buffer.h"
22#include " readline/types.h"
33#include < iostream>
4- #include < sys/ioctl.h>
5- #include < unistd.h>
64#include < algorithm>
75#include < cstring>
86
7+ #ifdef _WIN32
8+ #define NOMINMAX
9+ #include < windows.h>
10+ #include < io.h>
11+ #define STDOUT_FILENO _fileno (stdout)
12+ #else
13+ #include < sys/ioctl.h>
14+ #include < unistd.h>
15+ #endif
16+
917namespace readline {
1018
1119Buffer::Buffer (const Prompt& prompt)
1220 : prompt_(prompt) {
1321
1422 // Get terminal size
23+ #ifdef _WIN32
24+ CONSOLE_SCREEN_BUFFER_INFO csbi;
25+ if (GetConsoleScreenBufferInfo (GetStdHandle (STD_OUTPUT_HANDLE), &csbi)) {
26+ width_ = csbi.srWindow .Right - csbi.srWindow .Left + 1 ;
27+ height_ = csbi.srWindow .Bottom - csbi.srWindow .Top + 1 ;
28+ }
29+ #else
1530 struct winsize ws;
1631 if (ioctl (STDOUT_FILENO, TIOCGWINSZ, &ws) == 0 ) {
1732 width_ = ws.ws_col ;
1833 height_ = ws.ws_row ;
1934 }
35+ #endif
2036
2137 line_width_ = width_ - static_cast <int >(prompt_.get_prompt ().length ());
2238}
@@ -95,7 +111,7 @@ void Buffer::move_left() {
95111 std::cout << cursor_left_n (1 );
96112 }
97113
98- int line = display_pos_ / line_width_ - 1 ;
114+ int line = static_cast < int >( display_pos_ / line_width_) - 1 ;
99115 bool has_space = get_line_spacing (line);
100116 if (has_space) {
101117 display_pos_ -= 1 ;
@@ -120,14 +136,14 @@ void Buffer::move_right() {
120136
121137 if (display_pos_ % line_width_ == 0 ) {
122138 std::cout << cursor_down_n (1 ) << CURSOR_BOL
123- << cursor_right_n (prompt_.get_prompt ().length ());
139+ << cursor_right_n (static_cast < int >( prompt_.get_prompt ().length () ));
124140 } else if ((display_pos_ - r_length) % line_width_ == line_width_ - 1 && has_space) {
125141 std::cout << cursor_down_n (1 ) << CURSOR_BOL
126- << cursor_right_n (prompt_.get_prompt ().length () + r_length);
142+ << cursor_right_n (static_cast < int >( prompt_.get_prompt ().length () ) + r_length);
127143 display_pos_ += 1 ;
128144 } else if (!line_has_space_.empty () && display_pos_ % line_width_ == line_width_ - 1 && has_space) {
129145 std::cout << cursor_down_n (1 ) << CURSOR_BOL
130- << cursor_right_n (prompt_.get_prompt ().length ());
146+ << cursor_right_n (static_cast < int >( prompt_.get_prompt ().length () ));
131147 display_pos_ += 1 ;
132148 } else {
133149 std::cout << cursor_right_n (r_length);
@@ -165,27 +181,27 @@ void Buffer::move_right_word() {
165181
166182void Buffer::move_to_start () {
167183 if (pos_ > 0 ) {
168- int curr_line = display_pos_ / line_width_;
184+ int curr_line = static_cast < int >( display_pos_ / line_width_) ;
169185 if (curr_line > 0 ) {
170186 std::cout << cursor_up_n (curr_line);
171187 }
172- std::cout << CURSOR_BOL << cursor_right_n (prompt_.get_prompt ().length ());
188+ std::cout << CURSOR_BOL << cursor_right_n (static_cast < int >( prompt_.get_prompt ().length () ));
173189 pos_ = 0 ;
174190 display_pos_ = 0 ;
175191 }
176192}
177193
178194void Buffer::move_to_end () {
179195 if (pos_ < buffer_.size ()) {
180- int curr_line = display_pos_ / line_width_;
181- int total_lines = display_size () / line_width_;
196+ int curr_line = static_cast < int >( display_pos_ / line_width_) ;
197+ int total_lines = static_cast < int >( display_size () / line_width_) ;
182198 if (curr_line < total_lines) {
183199 std::cout << cursor_down_n (total_lines - curr_line);
184- int remainder = display_size () % line_width_;
200+ int remainder = static_cast < int >( display_size () % line_width_) ;
185201 std::cout << CURSOR_BOL
186- << cursor_right_n (prompt_.get_prompt ().length () + remainder);
202+ << cursor_right_n (static_cast < int >( prompt_.get_prompt ().length () ) + remainder);
187203 } else {
188- std::cout << cursor_right_n (display_size () - display_pos_);
204+ std::cout << cursor_right_n (static_cast < int >( display_size () - display_pos_) );
189205 }
190206
191207 pos_ = buffer_.size ();
@@ -260,7 +276,7 @@ int Buffer::count_remaining_line_width(int place) {
260276 if (pos_ + counter < buffer_.size ()) {
261277 char32_t r = buffer_[pos_ + counter];
262278 place += char_width (r);
263- prev_len = to_utf8 (r).length ();
279+ prev_len = static_cast < int >( to_utf8 (r).length () );
264280 } else {
265281 break ;
266282 }
@@ -282,7 +298,7 @@ void Buffer::draw_remaining() {
282298 remaining_text.length ()));
283299
284300 if (!curr_line.empty ()) {
285- std::cout << CLEAR_TO_EOL << curr_line << cursor_left_n (curr_line.length ());
301+ std::cout << CLEAR_TO_EOL << curr_line << cursor_left_n (static_cast < int >( curr_line.length () ));
286302 } else {
287303 std::cout << CLEAR_TO_EOL;
288304 }
@@ -369,7 +385,7 @@ void Buffer::delete_word() {
369385void Buffer::replace (const std::u32string& text) {
370386 display_pos_ = 0 ;
371387 pos_ = 0 ;
372- int line_nums = display_size () / line_width_;
388+ int line_nums = static_cast < int >( display_size () / line_width_) ;
373389
374390 buffer_.clear ();
375391
@@ -390,20 +406,20 @@ void Buffer::clear_screen() {
390406 std::cout << CLEAR_SCREEN << CURSOR_RESET << prompt_.get_prompt ();
391407 if (is_empty ()) {
392408 std::string ph = prompt_.get_placeholder ();
393- std::cout << COLOR_GREY << ph << cursor_left_n (ph.length ()) << COLOR_DEFAULT;
409+ std::cout << COLOR_GREY << ph << cursor_left_n (static_cast < int >( ph.length () )) << COLOR_DEFAULT;
394410 } else {
395411 size_t curr_pos = display_pos_;
396412 size_t curr_index = pos_;
397413 pos_ = 0 ;
398414 display_pos_ = 0 ;
399415 draw_remaining ();
400- std::cout << CURSOR_RESET << cursor_right_n (prompt_.get_prompt ().length ());
416+ std::cout << CURSOR_RESET << cursor_right_n (static_cast < int >( prompt_.get_prompt ().length () ));
401417 if (curr_pos > 0 ) {
402- int target_line = curr_pos / line_width_;
418+ int target_line = static_cast < int >( curr_pos / line_width_) ;
403419 if (target_line > 0 ) {
404420 std::cout << cursor_down_n (target_line);
405421 }
406- int remainder = curr_pos % line_width_;
422+ int remainder = static_cast < int >( curr_pos % line_width_) ;
407423 if (remainder > 0 ) {
408424 std::cout << cursor_right_n (remainder);
409425 }
0 commit comments