Commit 72a1adc6 authored by Jekyll Wu's avatar Jekyll Wu
Browse files

Avoid extra trailing whitespace when copying text

A new 'isRealCharacter' flag is added into the 'Character' class
to distinguish real characters and charaters used as place holders.

The code is a bit ugly now. Should improve it later when the idea and
implementation are proven to be feasible and cause no regression.

CCBUG: 188528
parent 0bc1c575
......@@ -91,8 +91,9 @@ public:
explicit inline Character(quint16 _c = ' ',
CharacterColor _f = CharacterColor(COLOR_SPACE_DEFAULT,DEFAULT_FORE_COLOR),
CharacterColor _b = CharacterColor(COLOR_SPACE_DEFAULT,DEFAULT_BACK_COLOR),
quint8 _r = DEFAULT_RENDITION)
: character(_c), rendition(_r), foregroundColor(_f), backgroundColor(_b) {}
quint8 _r = DEFAULT_RENDITION,
bool _real = true)
: character(_c), rendition(_r), foregroundColor(_f), backgroundColor(_b), isRealCharacter(_real) {}
/** The unicode character value for this character.
*
......@@ -109,6 +110,17 @@ public:
/** The color used to draw this character's background. */
CharacterColor backgroundColor;
/** Indicate whether this character really exists, or exists simply as place holder.
*
* TODO: this boolean filed can be further improved to become a enum filed, which
* indicates different roles:
*
* RealCharacter: a character which really exists
* PlaceHolderCharacter: a character which exists as place holder
* TabStopCharacter: a special place holder for HT('\t')
*/
bool isRealCharacter;
/**
* Returns true if this character has a transparent background when
* it is drawn with the specified @p palette.
......
......@@ -65,7 +65,8 @@ const bool BS_CLEARS = false ;
const Character Screen::defaultChar = Character(' ',
CharacterColor(COLOR_SPACE_DEFAULT,DEFAULT_FORE_COLOR),
CharacterColor(COLOR_SPACE_DEFAULT,DEFAULT_BACK_COLOR),
DEFAULT_RENDITION);
DEFAULT_RENDITION,
false);
Screen::Screen(int l, int c):
lines(l),
......@@ -720,6 +721,7 @@ void Screen::displayCharacter(unsigned short c)
currentChar.foregroundColor = effectiveForeground;
currentChar.backgroundColor = effectiveBackground;
currentChar.rendition = effectiveRendition;
currentChar.isRealCharacter = true;
int i = 0;
int newCursorX = cuX + w--;
......@@ -735,6 +737,7 @@ void Screen::displayCharacter(unsigned short c)
ch.foregroundColor = effectiveForeground;
ch.backgroundColor = effectiveBackground;
ch.rendition = effectiveRendition;
ch.isRealCharacter = false;
w--;
}
......@@ -857,7 +860,7 @@ void Screen::clearImage(int loca, int loce, char c)
int topLine = loca/columns;
int bottomLine = loce/columns;
Character clearCh(c,currentForeground,currentBackground,DEFAULT_RENDITION);
Character clearCh(c,currentForeground,currentBackground,DEFAULT_RENDITION,false);
//if the character being used to clear the area is the same as the
//default character, the affected lines can simply be shrunk.
......
......@@ -100,6 +100,20 @@ void PlainTextDecoder::decodeLine(const Character* const characters, int count,
}
}
// find out the last technically real character in the line
int guard = -1 ;
for (int i = count-1 ; i >= 0 ; i--)
{
// FIXME: the special case of '\n' here is really ugly
// Maybe the '\n' should be added after calling this method in
// Screen::copyLineToStream()
if ( characters[i].isRealCharacter && characters[i].character != '\n' )
{
guard = i;
break;
}
}
for (int i=0;i<outputCount;)
{
if (characters[i].rendition & RE_EXTENDED_CHAR)
......@@ -115,8 +129,22 @@ void PlainTextDecoder::decodeLine(const Character* const characters, int count,
}
else
{
plainText.append( QChar(characters[i].character) );
i += qMax(1,konsole_wcwidth(characters[i].character));
// All characters which appear before the last real character are
// seen as real characters, even when they are technically marked as
// non-real.
//
// This feels tricky, but otherwise leading "whitespaces" may be
// lost in some situation. One typical example is copying the result
// of `dialog --infobox "qwe" 10 10` .
if ( characters[i].isRealCharacter || i <= guard )
{
plainText.append( QChar(characters[i].character) );
i += qMax(1,konsole_wcwidth(characters[i].character));
}
else
{
++i; // should we 'break' directly here?
}
}
}
*_output << plainText;
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment