@@ -65,6 +65,8 @@ class windows_coutt : public std::streambuf
6565bool consolet::_is_terminal = false ;
6666bool consolet::_use_SGR = false ;
6767bool consolet::_init_done = false ;
68+ bool consolet::_width_is_set = false ;
69+ std::size_t consolet::_width;
6870std::ostream *consolet::_out = nullptr ;
6971std::ostream *consolet::_err = nullptr ;
7072
@@ -181,15 +183,19 @@ std::ostream &consolet::reset(std::ostream &str)
181183
182184std::size_t consolet::width ()
183185{
184- std::size_t width = 80 ; // default
186+ if (_width_is_set)
187+ return _width;
185188
186- if (_is_terminal)
189+ _width_is_set = true ;
190+ _width = 80 ; // default
191+
192+ if (is_terminal ())
187193 {
188194#ifdef _WIN32
189195 HANDLE h = GetStdHandle (STD_OUTPUT_HANDLE);
190196 CONSOLE_SCREEN_BUFFER_INFO info;
191197 GetConsoleScreenBufferInfo (h, &info);
192- width = info.srWindow .Right - info.srWindow .Left + 1 ;
198+ _width = info.srWindow .Right - info.srWindow .Left + 1 ;
193199#else
194200 std::ostringstream width_stream;
195201 run (" stty" , {" stty" , " size" }, " " , width_stream, " " );
@@ -200,12 +206,12 @@ std::size_t consolet::width()
200206 {
201207 auto width_l = atol (stty_output[1 ].c_str ());
202208 if (width_l >= 10 && width_l <= 400 )
203- width = width_l;
209+ _width = width_l;
204210 }
205211#endif
206212 }
207213
208- return width ;
214+ return _width ;
209215}
210216
211217extern " C" int mk_wcwidth (wchar_t ucs);
0 commit comments