25 #define YUILogComponent "ncurses"
26 #include <yui/YUILog.h>
28 #include "stringutil.h"
33 #include <boost/algorithm/string.hpp>
40 NCtext::NCtext(
const NCstring & nstr )
47 NCtext::NCtext(
const NCstring & nstr,
size_t columns )
49 lbrset( nstr, columns );
59 void NCtext::lset(
const NCstring & ntext )
64 mtext.push_back(
"" );
66 if ( ntext.str().empty() )
69 std::wstring text( ntext.str() );
71 std::wstring::size_type spos = 0;
73 std::wstring::size_type cpos = std::wstring::npos;
78 boost::erase_all( text, L
"\r" );
80 while (( cpos = text.find( L
'\n', spos ) ) != std::wstring::npos )
83 mtext.push_back(
"" );
85 mtext.back() =
NCstring( mtext.back().str() + text.substr( spos, cpos - spos ) );
92 if ( spos < text.size() )
95 mtext.push_back(
"" );
97 mtext.back() =
NCstring( mtext.back().str() + text.substr( spos ) );
103 void NCtext::lbrset(
const NCstring & ntext,
size_t columns )
107 if ( ntext.str().empty() )
110 std::wstring text( ntext.str() );
113 boost::erase_all( text, L
"\r" );
115 std::wstring::size_type spos = 0;
117 std::wstring::size_type cpos = std::wstring::npos;
119 cpos = text.find( L
'\n', spos );
121 while ( cpos != std::wstring::npos )
123 std::wstring line = text.substr( spos, cpos - spos );
125 if ( line.size() <= columns )
127 mtext.push_back(
NCstring( line ) );
131 size_t start = columns;
132 mtext.push_back(
NCstring( line.substr( 0, columns ) ) );
134 while ( start < line.size() )
136 yuiDebug() <<
"Add: " << line.substr( start, columns ) << std::endl;
137 mtext.push_back(
NCstring( L
'~' + line.substr( start, columns - 1 ) ) );
138 start += columns - 1;
144 cpos = text.find( L
'\n', spos );
147 if ( spos < text.size() )
149 mtext.push_back(
NCstring( text.substr( spos ) ) );
155 unsigned NCtext::Lines()
const
157 if ( mtext.size() == 1 && mtext.front().Str() ==
"" )
167 void NCtext::append(
const NCstring &line )
169 mtext.push_back( line );
174 size_t NCtext::Columns()
const
180 std::wstring::const_iterator wstr_it;
182 for ( line = mtext.begin(); line != mtext.end(); ++line )
186 for ( wstr_it = ( *line ).str().begin(); wstr_it != ( *line ).str().end() ; ++wstr_it )
188 if ( iswprint( *wstr_it ) )
189 tmp_len += wcwidth( *wstr_it );
190 else if ( *wstr_it == L
'\t' )
191 tmp_len += NCurses::tabsize();
194 if ( tmp_len > llen )
203 const NCstring & NCtext::operator[]( std::wstring::size_type idx )
const
205 if ( idx >= Lines() )
208 const_iterator line = mtext.begin();
210 for ( ; idx; --idx, ++line )
217 std::ostream & operator<<( std::ostream & STREAM,
const NCtext & OBJ )
219 return STREAM <<
"[Text:" << OBJ.Lines() <<
',' << OBJ.Columns() <<
']';
226 void NClabel::stripHotkey()
228 hotline = std::wstring::npos;
231 for ( iterator line = mtext.begin(); line != mtext.end(); ++line, ++lineno )
235 if ( line->hotpos() != std::wstring::npos )
245 void NClabel::drawAt(
NCursesWindow & w, chtype style, chtype hotstyle,
247 const NC::ADJUST adjust,
250 wrect area( dim.intersectRelTo( w.area() ) );
254 unsigned maxlen = area.Sze.W;
255 unsigned len = ( width() < maxlen ) ? width() : maxlen;
261 unsigned dist = maxlen - len;
263 if ( adjust & NC::LEFT )
265 else if ( adjust & NC::RIGHT )
275 int maxl = area.Pos.L + area.Sze.H;
281 for ( NCtext::const_iterator line = begin();
282 line != end() && l < maxl;
283 ++line, ++l, ++lineno )
287 w.
move( l, area.Pos.C );
288 w.
addwstr( std::wstring( pre, L
' ' ).c_str() );
292 w.
move( l, area.Pos.C + pre );
298 if ( NCstring::terminalEncoding() !=
"UTF-8" )
301 bool ok = NCstring::RecodeFromWchar(( *line ).str(), NCstring::terminalEncoding(), &out );
305 w.
printw(
"%-*.*s", len, (
int )len, out.c_str() );
310 w.
printw(
"%ls", ( *line ).str().substr( 0, len ).c_str() );
314 if ( post && fillup )
316 w.
addwstr( std::wstring( post, L
' ' ).c_str() );
319 if ( lineno == hotline && hotstyle && pre + hotpos() < maxlen )
332 for ( ; l < maxl; ++l )
334 w.
printw( l, area.Pos.C,
"%-*.*s", area.Sze.W, area.Sze.W,
"" );
343 std::ostream & operator<<( std::ostream & STREAM,
const NClabel & OBJ )
345 STREAM <<
"[label" << OBJ.size() <<
':' << OBJ[0].str();
347 if ( OBJ.hasHotkey() )
348 STREAM <<
':' << OBJ.hotkey() <<
" at " << OBJ.hotpos();
350 return STREAM <<
']';
int printw(const char *fmt,...)
int addwstr(const wchar_t *str, int n=-1)
int add_attr_char(int y, int x)