aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/terminal/celliterator.h
blob: c246aaa3114983ab83b691077e7817ace0d58b6a (plain)
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
// Copyright (C) 2022 The Qt Company Ltd.
// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR GPL-3.0+ OR GPL-3.0 WITH Qt-GPL-exception-1.0

#pragma once

#include <string>

#include <QPoint>

namespace Terminal::Internal {

class TerminalSurface;

class CellIterator
{
public:
    using iterator_category = std::bidirectional_iterator_tag;
    using difference_type = std::ptrdiff_t;
    using value_type = std::u32string::value_type;
    using pointer = std::u32string::value_type *;
    // We need to return copies for std::reverse_iterator to work
    using reference = std::u32string::value_type;

    enum class State { BEGIN, INSIDE, END } m_state{};

public:
    CellIterator(const TerminalSurface *surface, QPoint pos);
    CellIterator(const TerminalSurface *surface, int pos);
    CellIterator(const TerminalSurface *surface);

public:
    QPoint gridPos() const;

public:
    CellIterator &operator-=(int n);
    CellIterator &operator+=(int n);

    reference operator*() const { return m_char; }
    pointer operator->() { return &m_char; }

    CellIterator &operator++() { return *this += 1; }
    CellIterator operator++(int)
    {
        CellIterator tmp = *this;
        ++(*this);
        return tmp;
    }

    CellIterator &operator--() { return *this -= 1; }
    CellIterator operator--(int)
    {
        CellIterator tmp = *this;
        --(*this);
        return tmp;
    }

    bool operator!=(const CellIterator &other) const
    {
        if (other.m_state != m_state)
            return true;

        if (other.m_pos != m_pos)
            return true;

        return false;
    }

    bool operator==(const CellIterator &other) const { return !operator!=(other); }

    CellIterator operator-(int n) const
    {
        CellIterator result = *this;
        result -= n;
        return result;
    }

    CellIterator operator+(int n) const
    {
        CellIterator result = *this;
        result += n;
        return result;
    }

    int position() const { return m_pos; }
    void setSkipZeros(bool skipZeros) { m_skipZeros = skipZeros; }

private:
    bool updateChar();

    const TerminalSurface *m_surface{nullptr};
    int m_pos{-1};
    int m_maxpos{-1};
    bool m_skipZeros{false};
    mutable std::u32string::value_type m_char;
};

} // namespace Terminal::Internal