From a45a3092b0cf9f8703e1e93ff589b33315f1a661 Mon Sep 17 00:00:00 2001 From: xiaoyifang <105986+xiaoyifang@users.noreply.github.com> Date: Mon, 9 Dec 2024 09:57:21 +0800 Subject: [PATCH] opt: beautify the layout of dictserver output (#2012) * opt: beautify the layout of dictserver output --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com> --- src/common/utils.hh | 13 +++++++++ src/dict/dictserver.cc | 65 +++++++++++++++++++++++++++++++++--------- 2 files changed, 64 insertions(+), 14 deletions(-) diff --git a/src/common/utils.hh b/src/common/utils.hh index dfa7301e..15a4d35e 100644 --- a/src/common/utils.hh +++ b/src/common/utils.hh @@ -39,6 +39,19 @@ inline QString rstrip( const QString & str ) return {}; } +inline uint32_t leadingSpaceCount( const QString & str ) +{ + for ( int i = 0; i < str.size(); i++ ) { + if ( str.at( i ).isSpace() ) { + continue; + } + else { + return i; + } + } + return 0; +} + std::string c_string( const QString & str ); bool endsWithIgnoreCase( QByteArrayView str, QByteArrayView extension ); /** diff --git a/src/dict/dictserver.cc b/src/dict/dictserver.cc index 2dff23b2..7ca2aad5 100644 --- a/src/dict/dictserver.cc +++ b/src/dict/dictserver.cc @@ -592,12 +592,49 @@ public: cancel(); } ); - connect( this, &DictServerArticleRequest::finishedArticle, this, [ this ]( QString articleText ) { + connect( this, &DictServerArticleRequest::finishedArticle, this, [ this ]( QString _articleText ) { if ( Utils::AtomicInt::loadAcquire( isCancelled ) ) { cancel(); return; } + //modify the _articleText,remove extra lines[start with 15X etc.] + QList< QString > lines = _articleText.split( "\n", Qt::SkipEmptyParts ); + + QString resultStr; + + // process the line + static QRegularExpression leadingRespCode( "^\\d{3} " ); + uint32_t leadingSpaceCount = 0; + uint32_t firstLeadingSpaceCount = 0; + for ( const QString & line : lines ) { + //ignore 15X lines + if ( leadingRespCode.match( line ).hasMatch() ) { + continue; + } + // ignore dot(.),the end line character + if ( line.trimmed() == "." ) { + break; + } + + auto lsc = Utils::leadingSpaceCount( line ); + + if ( firstLeadingSpaceCount == 0 && lsc > firstLeadingSpaceCount ) { + firstLeadingSpaceCount = lsc; + } + + if ( lsc >= leadingSpaceCount && lsc > firstLeadingSpaceCount ) { + //extra space + resultStr.append( " " ); + resultStr.append( line.trimmed() ); + } + else { + resultStr.append( "\n" ); + resultStr.append( line ); + } + leadingSpaceCount = lsc; + } + static QRegularExpression phonetic( R"(\\([^\\]+)\\)", QRegularExpression::CaseInsensitiveOption ); // phonetics: \stuff\ ... static QRegularExpression divs_inside_phonetic( "]*)>