about summary refs log tree commit diff
path: root/saneterm/termview.py
blob: 1b8e3d8c23228ffde1d75d22e684e107e615785b (plain) (blame)
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
from gi.repository import Gtk
from gi.repository import GObject

class LimitTextBuffer(Gtk.TextBuffer):
    """
    Buffer which stores a limit amount of lines. If the limit is -1
    an unlimited amount of lines is stored. Old lines are deleted
    automatically if the limit is exceeded.
    """

    def __init__(self, limit):
        Gtk.TextBuffer.__init__(self)

        if limit == -1:
            return # unlimited

        self.limit = limit
        self.connect_after("insert-text", self.__insert_text)

    def __insert_text(self, buffer, loc, text, len):
        lines = buffer.get_line_count()
        diff = lines - self.limit
        if diff <= 0:
            return

        end = buffer.get_start_iter()
        end.forward_lines(diff)

        start = buffer.get_start_iter()
        buffer.delete(start, end)

        # Revalide the given iterator
        loc.assign(buffer.get_end_iter())

class TermView(Gtk.TextView):
    """
    TextView-based widget for line-based terminal emulators. The widget
    has two input sources (a) input entered by the application user and
    (b) input received from a backend (usually a PTY).

    Since the widget is line-based, user input is only received after
    the user finishes input of the current line by emitting a newline
    character. Afterwards, a new-user-input signal is emitted to which
    the application should connect. To display input received from the
    backend source (e.g. a PTY) the insert_data method should be used.

    Internally, the widget tracks input through two markers. The
    _last_output_mark tracks the position in the underlying TextView
    where data from the backend source was last written. While the
    _last_mark constitues the position where user input would be
    added.

    Control characters are not line-buffered. Instead these are
    intercepted through pre-defined key bindings by Gtk and communicated
    to the application via the termios-ctrlkey signal.
    """

    def __init__(self, limit=-1):
        # TODO: set insert-hypens to false in GTK 4
        # https://docs.gtk.org/gtk4/property.TextTag.insert-hyphens.html
        Gtk.TextView.__init__(self)

        self._textbuffer = LimitTextBuffer(limit)
        self._textbuffer.connect("end-user-action", self.__end_user_action)
        self.set_buffer(self._textbuffer)

        self.set_monospace(True)
        self.set_input_hints(Gtk.InputHints.NO_SPELLCHECK | Gtk.InputHints.EMOJI)

        self._last_mark = self._textbuffer.create_mark(None,
                self._textbuffer.get_end_iter(), True)
        self._last_output_mark = self._last_mark

        signals = {
            "kill-after-output": self.__kill_after_output,
            "move-input-start": self.__move_input_start,
            "move-input-end": self.__move_input_end,
        }

        for signal in signals.items():
            name, func = signal
            GObject.signal_new(name, self,
                    GObject.SIGNAL_ACTION, GObject.TYPE_NONE,
                    ())

            self.connect(name, func)

        GObject.signal_new("termios-ctrlkey", self,
                GObject.SIGNAL_ACTION, GObject.TYPE_NONE,
                (GObject.TYPE_LONG,))

        GObject.signal_new("new-user-input", self,
                GObject.SIGNAL_RUN_LAST, GObject.TYPE_NONE,
                (GObject.TYPE_PYOBJECT,))

    def insert_data(self, str):
        self._textbuffer.insert(self._textbuffer.get_end_iter(), str)

        end = self._textbuffer.get_end_iter()
        self._last_mark = self._textbuffer.create_mark(None, end, True)
        self._last_output_mark = self._last_mark

    def flush(self):
        end = self._textbuffer.get_end_iter()

        line_start = self._textbuffer.get_iter_at_mark(self._last_output_mark)
        line = self._textbuffer.get_text(line_start, end, True)

        self.emit("new-user-input", line)

        self._last_output_mark = self._textbuffer.create_mark(None, end, True)
        self._last_mark = self._last_mark

    def __cursor_at_mark(self, mark):
        if self._textbuffer.get_has_selection():
            return False

        cur = self._textbuffer.get_iter_at_offset(self._textbuffer.props.cursor_position)
        other = self._textbuffer.get_iter_at_mark(mark)

        return cur.compare(other) == 0

    def cursor_at_out(self):
        return self.__cursor_at_mark(self._last_output_mark)

    def cursor_at_end(self):
        return self.__cursor_at_mark(self._last_mark)

    # XXX: Can maybe be removed in favor of do_delete_from_cursor.
    def do_backspace(self):
        # If current position is output positon ignore backspace.
        if not self.cursor_at_out():
            Gtk.TextView.do_backspace(self)

    def __end_user_action(self, buffer):
        start = buffer.get_iter_at_mark(self._last_mark)
        end = self._textbuffer.get_end_iter()

        text = buffer.get_text(start, end, True)
        if text == "\n":
            self.flush()
        else:
            self._last_mark = buffer.create_mark(None, end, True)

    def do_delete_from_cursor(self, type, count):
        # If the type is GTK_DELETE_CHARS, GTK+ deletes the selection.
        if type == Gtk.DeleteType.CHARS:
            Gtk.TextView.do_delete_from_cursor(self, type, count)
            return

        buf = self._textbuffer
        cur = buf.get_iter_at_offset(buf.props.cursor_position)
        out = buf.get_iter_at_mark(self._last_output_mark)

        # Only go backward by $count chars if there are enough
        # characters in the buffer and the movement would not
        # go beyond the last output point.
        if cur.backward_chars(count):
            return
        elif cur.compare(out) != 1: # cur <= out
            return

        Gtk.TextView.do_delete_from_cursor(self, type, count)

    def __kill_after_output(self, textview):
        buffer = textview.get_buffer()

        start = buffer.get_iter_at_mark(self._last_output_mark)
        end = buffer.get_end_iter()

        buffer.delete(start, end)

    def __move_input_start(self, textview):
        buffer = textview.get_buffer()

        start = buffer.get_iter_at_mark(self._last_output_mark)
        buffer.place_cursor(start)

    def __move_input_end(self, textview):
        buffer = textview.get_buffer()

        end = buffer.get_iter_at_mark(self._last_mark)
        buffer.place_cursor(end)