From 24cc94d5fbe7d95dcd644998eee836f2dfb58dcc Mon Sep 17 00:00:00 2001 From: Matan Ziv-Av Date: Thu, 1 Sep 2022 11:42:33 +0300 Subject: [PATCH] Allow for showing line numbers As with semantic hints, the line numbers may be displayed always, never or when URL hints are shown. --- src/ViewManager.cpp | 22 ++++++++++++++++++++++ src/ViewManager.h | 2 ++ src/profile/Profile.cpp | 1 + src/profile/Profile.h | 9 +++++++++ src/terminalDisplay/TerminalPainter.cpp | 11 +++++++++++ 5 files changed, 45 insertions(+) diff --git a/src/ViewManager.cpp b/src/ViewManager.cpp index 1dbb5be3..8d2a5899 100644 --- a/src/ViewManager.cpp +++ b/src/ViewManager.cpp @@ -294,6 +294,12 @@ void ViewManager::setupActions() connect(action, &QAction::triggered, this, &ViewManager::toggleSemanticHints); _viewContainer->addAction(action); + action = new QAction(i18nc("@action Shortcut entry", "Toggle line numbers display"), this); + collection->addAction(QStringLiteral("toggle-line-numbers"), action); + collection->setDefaultShortcut(action, Qt::CTRL | Qt::ALT | Qt::Key_Backslash); + connect(action, &QAction::triggered, this, &ViewManager::toggleLineNumbers); + _viewContainer->addAction(action); + action = new QAction(this); action->setText(i18nc("@action:inmenu", "Equal size to all views")); collection->setDefaultShortcut(action, Konsole::ACCEL | Qt::SHIFT | Qt::Key_Backslash); @@ -522,6 +528,22 @@ void ViewManager::toggleSemanticHints() activeTerminalDisplay->update(); } +void ViewManager::toggleLineNumbers() +{ + int currentSessionId = currentSession(); + Q_ASSERT(currentSessionId >= 0); + Session *activeSession = SessionManager::instance()->idToSession(currentSessionId); + Q_ASSERT(activeSession); + auto profile = SessionManager::instance()->sessionProfile(activeSession); + + profile->setProperty(Profile::LineNumbers, (profile->lineNumbers() + 1) % 3); + + auto activeTerminalDisplay = _viewContainer->activeViewSplitter()->activeTerminalDisplay(); + const char *names[3] = {"Never", "Sometimes", "Always"}; + activeTerminalDisplay->showNotification(i18n("Line numbers ") + i18n(names[profile->lineNumbers()])); + activeTerminalDisplay->update(); +} + QHash ViewManager::forgetAll(ViewSplitter *splitter) { splitter->setParent(nullptr); diff --git a/src/ViewManager.h b/src/ViewManager.h index d46a7dcf..71bc5265 100644 --- a/src/ViewManager.h +++ b/src/ViewManager.h @@ -404,6 +404,8 @@ private Q_SLOTS: void toggleSemanticHints(); + void toggleLineNumbers(); + private: Q_DISABLE_COPY(ViewManager) diff --git a/src/profile/Profile.cpp b/src/profile/Profile.cpp index 58b341db..2f5fb6d9 100644 --- a/src/profile/Profile.cpp +++ b/src/profile/Profile.cpp @@ -120,6 +120,7 @@ const std::vector Profile::DefaultProperties = { {VerticalLine, "VerticalLine", TERMINAL_GROUP, false}, {VerticalLineAtChar, "VerticalLineAtChar", TERMINAL_GROUP, 80}, {PeekPrimaryKeySequence, "PeekPrimaryKeySequence", TERMINAL_GROUP, QString()}, + {LineNumbers, "LineNumbers", TERMINAL_GROUP, 0}, // Cursor {UseCustomCursorColor, "UseCustomCursorColor", CURSOR_GROUP, false}, diff --git a/src/profile/Profile.h b/src/profile/Profile.h index 39dc44e6..3e77367c 100644 --- a/src/profile/Profile.h +++ b/src/profile/Profile.h @@ -374,6 +374,10 @@ public: /** (QFont) Emoji font override */ EmojiFont, + /** When to show line numbers. + * 0 for Never, 1 when showing URL hints, 2 for always + */ + LineNumbers, }; Q_ENUM(Property) @@ -805,6 +809,11 @@ public: return property(Profile::SemanticHints); } + int lineNumbers() const + { + return property(Profile::LineNumbers); + } + bool semanticUpDown() const { return property(Profile::SemanticUpDown); diff --git a/src/terminalDisplay/TerminalPainter.cpp b/src/terminalDisplay/TerminalPainter.cpp index ef0d1850..94d61b55 100644 --- a/src/terminalDisplay/TerminalPainter.cpp +++ b/src/terminalDisplay/TerminalPainter.cpp @@ -78,6 +78,7 @@ void TerminalPainter::drawContents(Character *image, auto currentProfile = SessionManager::instance()->sessionProfile(m_parentDisplay->session()); const bool invertedRendition = currentProfile ? currentProfile->property(Profile::InvertSelectionColors) : false; const Enum::Hints semanticHints = currentProfile ? static_cast(currentProfile->semanticHints()) : Enum::HintsNever; + const Enum::Hints lineNumbers = currentProfile ? static_cast(currentProfile->lineNumbers()) : Enum::HintsNever; QVector univec; univec.reserve(m_parentDisplay->usedColumns()); @@ -250,6 +251,16 @@ void TerminalPainter::drawContents(Character *image, paint.setPen(pen); paint.drawLine(leftPadding, textY, m_parentDisplay->contentRect().right(), textY); } + if ((lineNumbers == Enum::HintsURL && m_parentDisplay->filterChain()->showUrlHint()) || lineNumbers == Enum::HintsAlways) { + QRect rect(m_parentDisplay->contentRect().right() - 4 * fontWidth, textY, m_parentDisplay->contentRect().right(), textY + fontHeight); + QPen pen(QColor(0xC00000)); + paint.setPen(pen); + QFont currentFont = paint.font(); + currentFont.setWeight(normalWeight); + currentFont.setItalic(false); + paint.setFont(currentFont); + paint.drawText(rect, Qt::AlignLeft, QString::number(y + m_parentDisplay->screenWindow()->currentLine())); + } if (doubleHeightLinePair) { y++;