File: EscapeSequenceUrlExtractor.cpp

package info (click to toggle)
deepin-terminal 5.9.40%2Bdfsg-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 9,880 kB
  • sloc: cpp: 40,525; sh: 246; exp: 37; makefile: 18
file content (120 lines) | stat: -rw-r--r-- 2,734 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
/*
    SPDX-FileCopyrightText: 2007-2008 Robert Knight <robertknight@gmail.com>
    SPDX-FileCopyrightText: 1997, 1998 Lars Doelle <lars.doelle@on-line.de>

    SPDX-License-Identifier: GPL-2.0-or-later
*/

#include "EscapeSequenceUrlExtractor.h"

#include <QUrl>

namespace Konsole {
EscapeSequenceUrlExtractor::EscapeSequenceUrlExtractor()
= default;

void Konsole::EscapeSequenceUrlExtractor::setScreen(Konsole::Screen* screen)
{
    _screen = screen;
    clear();
}

bool EscapeSequenceUrlExtractor::reading() const
{
    return _reading;
}

void EscapeSequenceUrlExtractor::beginUrlInput()
{
    _reading = true;
}

void EscapeSequenceUrlExtractor::appendUrlText(QChar c)
{
    if (!reading()) {
        return;
    }

    if (_currentUrl.text.isEmpty()) {
        // We need to  on getCursorX because we want the position of the
        // last printed character, not the cursor.
        const int realCcolumn = _screen->getCursorY() + _screen->getHistLines();
        _currentUrl.begin = Coordinate{realCcolumn, _screen->getCursorX() - 1};
    }
    _currentUrl.text += c;
}

void EscapeSequenceUrlExtractor::setUrl(const QString& url)
{
    if (_allowedUriSchemas.contains(QUrl(url).scheme() + QLatin1String("://"))) {
        _currentUrl.url = url;
    } else {
        abortUrlInput();
    }
}

void EscapeSequenceUrlExtractor::abortUrlInput()
{
    _reading = false;
    _currentUrl = ExtractedUrl{};
    _ignoreNextUrlInput = true;
}

void EscapeSequenceUrlExtractor::endUrlInput()
{
    Q_ASSERT(reading());
    _reading = false;

    const int realCcolumn = _screen->getCursorY() + _screen->getHistLines();
    const auto currentPos = Coordinate{realCcolumn, _screen->getCursorX()};
    _currentUrl.end = currentPos;
    _history.append(_currentUrl);

    _currentUrl = ExtractedUrl{};
}

void EscapeSequenceUrlExtractor::clear()
{
    _history.clear();
}

void EscapeSequenceUrlExtractor::setAllowedLinkSchema(const QStringList& schema)
{
    _allowedUriSchemas = schema;
}

void EscapeSequenceUrlExtractor::historyLinesRemoved(int lines)
{
    for (auto &url : _history) {
        url.begin.row -= lines;
        url.end.row -= lines;
   }
    _history.erase(
        std::remove_if(std::begin(_history), std::end(_history), [](const ExtractedUrl& url) {
            const bool toRemove = url.begin.row < 0;
            return toRemove;

        }),
        std::end(_history));
}

QVector<ExtractedUrl> EscapeSequenceUrlExtractor::history() const
{
    return _history;
}

void Konsole::EscapeSequenceUrlExtractor::toggleUrlInput()
{
    if (_ignoreNextUrlInput) {
        _ignoreNextUrlInput = false;
        return;
    }

    if (_reading) {
        endUrlInput();
    } else {
        beginUrlInput();
    }
}

}