2017-11-04 07:03:53 +01:00
|
|
|
/***********************************************************************
|
|
|
|
* ftermbuffer.cpp - Buffer for virtual terminal strings *
|
|
|
|
* *
|
2020-07-08 21:32:47 +02:00
|
|
|
* This file is part of the FINAL CUT widget toolkit *
|
2017-11-04 07:03:53 +01:00
|
|
|
* *
|
2021-02-09 22:01:21 +01:00
|
|
|
* Copyright 2017-2021 Markus Gans *
|
2017-11-04 07:03:53 +01:00
|
|
|
* *
|
2020-07-08 21:32:47 +02:00
|
|
|
* FINAL CUT is free software; you can redistribute it and/or modify *
|
|
|
|
* it under the terms of the GNU Lesser General Public License as *
|
|
|
|
* published by the Free Software Foundation; either version 3 of *
|
2017-11-04 07:03:53 +01:00
|
|
|
* the License, or (at your option) any later version. *
|
|
|
|
* *
|
2020-07-08 21:32:47 +02:00
|
|
|
* FINAL CUT is distributed in the hope that it will be useful, but *
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of *
|
2017-11-04 07:03:53 +01:00
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
|
|
|
|
* GNU Lesser General Public License for more details. *
|
|
|
|
* *
|
|
|
|
* You should have received a copy of the GNU Lesser General Public *
|
|
|
|
* License along with this program. If not, see *
|
|
|
|
* <http://www.gnu.org/licenses/>. *
|
|
|
|
***********************************************************************/
|
2017-07-12 01:16:57 +02:00
|
|
|
|
2020-02-19 21:59:13 +01:00
|
|
|
#include <algorithm>
|
2017-09-11 03:06:02 +02:00
|
|
|
#include <string>
|
|
|
|
#include <vector>
|
|
|
|
|
2019-07-21 23:31:21 +02:00
|
|
|
#include "final/fc.h"
|
|
|
|
#include "final/fcolorpair.h"
|
|
|
|
#include "final/fstring.h"
|
2020-02-16 00:01:36 +01:00
|
|
|
#include "final/fstyle.h"
|
2017-09-17 21:32:46 +02:00
|
|
|
#include "final/ftermbuffer.h"
|
2019-07-21 23:31:21 +02:00
|
|
|
#include "final/fvterm.h"
|
|
|
|
#include "final/ftypes.h"
|
2017-07-12 01:16:57 +02:00
|
|
|
|
2018-09-20 23:59:01 +02:00
|
|
|
namespace finalcut
|
|
|
|
{
|
2017-07-12 01:16:57 +02:00
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
// class FTermBuffer
|
|
|
|
//----------------------------------------------------------------------
|
2020-11-24 21:06:39 +01:00
|
|
|
FTermBuffer::~FTermBuffer() noexcept = default; // destructor
|
|
|
|
|
2017-07-12 01:16:57 +02:00
|
|
|
|
|
|
|
// public methods of FTermBuffer
|
|
|
|
//----------------------------------------------------------------------
|
2020-10-04 00:59:21 +02:00
|
|
|
FString FTermBuffer::toString() const
|
2017-07-12 01:16:57 +02:00
|
|
|
{
|
2019-09-28 03:13:06 +02:00
|
|
|
std::wstring wide_string{};
|
|
|
|
wide_string.reserve(data.size());
|
2021-02-09 22:01:21 +01:00
|
|
|
std::for_each ( data.begin()
|
|
|
|
, data.end()
|
|
|
|
, [&wide_string] (const FChar& fchar)
|
|
|
|
{
|
|
|
|
for (auto&& ch : fchar.ch)
|
|
|
|
{
|
|
|
|
if ( ch == L'\0' )
|
|
|
|
return;
|
|
|
|
else
|
|
|
|
wide_string.push_back(ch);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
);
|
2020-05-02 00:07:35 +02:00
|
|
|
return wide_string;
|
2017-07-12 01:16:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2019-09-28 03:13:06 +02:00
|
|
|
int FTermBuffer::write (const FString& string)
|
2017-07-12 01:16:57 +02:00
|
|
|
{
|
2019-09-28 03:13:06 +02:00
|
|
|
assert ( ! string.isNull() );
|
2021-02-09 22:01:21 +01:00
|
|
|
data.reserve(data.size() + string.getLength());
|
|
|
|
const auto last = string.end();
|
|
|
|
auto begin = string.begin();
|
|
|
|
auto iter = begin;
|
|
|
|
int char_width{0};
|
2017-07-12 01:16:57 +02:00
|
|
|
|
2021-02-09 22:01:21 +01:00
|
|
|
for (auto&& ch : string)
|
2017-07-12 01:16:57 +02:00
|
|
|
{
|
2021-02-09 22:01:21 +01:00
|
|
|
auto width = getColumnWidth(ch);
|
|
|
|
auto wspace = std::iswspace(wint_t(ch));
|
|
|
|
|
|
|
|
if ( width == 0 && ! wspace ) // zero-width character
|
|
|
|
{
|
|
|
|
if ( iter == begin)
|
|
|
|
++begin;
|
|
|
|
|
|
|
|
++iter;
|
|
|
|
}
|
|
|
|
else if ( iter != begin )
|
|
|
|
add(begin, iter, char_width);
|
|
|
|
|
|
|
|
if ( iter == begin && (width > 0 || is7bit(ch)) ) // 1st char
|
|
|
|
++iter;
|
|
|
|
|
|
|
|
if ( width > 0 )
|
|
|
|
char_width += width;
|
|
|
|
|
|
|
|
if ( wspace )
|
|
|
|
add(begin, iter, char_width);
|
2017-07-12 01:16:57 +02:00
|
|
|
}
|
|
|
|
|
2021-02-09 22:01:21 +01:00
|
|
|
if ( iter == last )
|
|
|
|
add(begin, iter, char_width);
|
|
|
|
|
|
|
|
return int(string.getLength());
|
2017-07-12 01:16:57 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------------
|
2019-09-28 03:13:06 +02:00
|
|
|
int FTermBuffer::write (wchar_t ch)
|
2017-07-12 01:16:57 +02:00
|
|
|
{
|
2020-12-03 10:43:20 +01:00
|
|
|
FChar nc{FVTerm::getAttribute()}; // next character
|
2020-11-14 20:59:26 +01:00
|
|
|
nc.ch[0] = ch;
|
2021-02-09 22:01:21 +01:00
|
|
|
addColumnWidth(nc); // add column width
|
2017-08-20 17:30:30 +02:00
|
|
|
nc.attr.bit.no_changes = false;
|
|
|
|
nc.attr.bit.printed = false;
|
2020-12-03 10:43:20 +01:00
|
|
|
data.emplace_back(nc);
|
2017-07-12 01:16:57 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2020-02-16 00:01:36 +01:00
|
|
|
//----------------------------------------------------------------------
|
2020-07-12 15:25:21 +02:00
|
|
|
void FTermBuffer::write (const FStyle& style) const
|
2020-02-16 00:01:36 +01:00
|
|
|
{
|
2020-12-31 20:45:10 +01:00
|
|
|
Style attr = style.getStyle();
|
2020-02-16 00:01:36 +01:00
|
|
|
|
2020-12-31 20:45:10 +01:00
|
|
|
if ( attr == Style::None )
|
2020-02-16 00:01:36 +01:00
|
|
|
FVTerm::setNormal();
|
2021-02-09 22:01:21 +01:00
|
|
|
else
|
|
|
|
{
|
|
|
|
if ( (attr & Style::Bold) != Style::None ) FVTerm::setBold();
|
|
|
|
if ( (attr & Style::Dim) != Style::None ) FVTerm::setDim();
|
|
|
|
if ( (attr & Style::Italic) != Style::None ) FVTerm::setItalic();
|
|
|
|
if ( (attr & Style::Underline) != Style::None ) FVTerm::setUnderline();
|
|
|
|
if ( (attr & Style::Blink) != Style::None ) FVTerm::setBlink();
|
|
|
|
if ( (attr & Style::Reverse) != Style::None ) FVTerm::setReverse();
|
|
|
|
if ( (attr & Style::Standout) != Style::None ) FVTerm::setStandout();
|
|
|
|
if ( (attr & Style::Invisible) != Style::None ) FVTerm::setInvisible();
|
|
|
|
if ( (attr & Style::Protected) != Style::None ) FVTerm::setProtected();
|
|
|
|
if ( (attr & Style::CrossedOut) != Style::None ) FVTerm::setCrossedOut();
|
|
|
|
if ( (attr & Style::DoubleUnderline) != Style::None ) FVTerm::setDoubleUnderline();
|
|
|
|
if ( (attr & Style::Transparent) != Style::None ) FVTerm::setTransparent();
|
|
|
|
if ( (attr & Style::ColorOverlay) != Style::None ) FVTerm::setColorOverlay();
|
|
|
|
if ( (attr & Style::InheritBackground) != Style::None ) FVTerm::setInheritBackground();
|
|
|
|
}
|
2020-02-16 00:01:36 +01:00
|
|
|
}
|
|
|
|
|
2019-01-30 12:17:48 +01:00
|
|
|
//----------------------------------------------------------------------
|
2020-07-12 15:25:21 +02:00
|
|
|
void FTermBuffer::write (const FColorPair& pair) const
|
2019-01-30 12:17:48 +01:00
|
|
|
{
|
2019-09-04 23:57:31 +02:00
|
|
|
FVTerm::setColor(pair.getForegroundColor(), pair.getBackgroundColor());
|
2019-01-30 12:17:48 +01:00
|
|
|
}
|
|
|
|
|
2017-07-12 01:16:57 +02:00
|
|
|
|
2021-02-09 22:01:21 +01:00
|
|
|
// private methods of FTermBuffer
|
|
|
|
//----------------------------------------------------------------------
|
|
|
|
void FTermBuffer::add ( FString::const_iterator& begin
|
|
|
|
, FString::const_iterator& end
|
|
|
|
, int& char_width )
|
|
|
|
{
|
|
|
|
if ( begin == end )
|
|
|
|
return;
|
|
|
|
|
|
|
|
FChar nc{FVTerm::getAttribute()}; // next character
|
|
|
|
nc.attr.byte[2] = 0;
|
|
|
|
nc.attr.byte[3] = 0;
|
|
|
|
|
|
|
|
if ( char_width == 2 && FTerm::getEncoding() != Encoding::UTF8 )
|
|
|
|
{
|
|
|
|
nc.ch[0] = '.';
|
|
|
|
nc.attr.bit.char_width = 1;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
nc.attr.bit.char_width = char_width & 0x03;
|
|
|
|
|
|
|
|
std::copy(begin, std::min(end, begin + UNICODE_MAX), nc.ch.begin());
|
|
|
|
data.emplace_back(nc);
|
|
|
|
begin = end;
|
|
|
|
char_width = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2017-08-06 17:02:19 +02:00
|
|
|
// FTermBuffer non-member operators
|
2017-07-12 01:16:57 +02:00
|
|
|
//----------------------------------------------------------------------
|
2019-10-08 04:37:19 +02:00
|
|
|
FTermBuffer::FCharVector& operator << ( FTermBuffer::FCharVector& termString
|
|
|
|
, const FTermBuffer& buf )
|
2017-08-06 17:02:19 +02:00
|
|
|
{
|
|
|
|
if ( ! buf.data.empty() )
|
|
|
|
termString.assign(buf.data.begin(), buf.data.end());
|
|
|
|
|
|
|
|
return termString;
|
|
|
|
}
|
2018-09-20 23:59:01 +02:00
|
|
|
|
|
|
|
} // namespace finalcut
|