1#
2#  Copyright (C) 2018 Codethink Limited
3#
4#  This program is free software; you can redistribute it and/or
5#  modify it under the terms of the GNU Lesser General Public
6#  License as published by the Free Software Foundation; either
7#  version 2 of the License, or (at your option) any later version.
8#
9#  This library is distributed in the hope that it will be useful,
10#  but WITHOUT ANY WARRANTY; without even the implied warranty of
11#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
12#  Lesser General Public License for more details.
13#
14#  You should have received a copy of the GNU Lesser General Public
15#  License along with this library. If not, see <http://www.gnu.org/licenses/>.
16#
17#  Authors:
18#        Tristan Van Berkom <tristan.vanberkom@codethink.co.uk>
19import os
20import click
21
22from .app import App
23
24
25# This trick is currently only supported on some terminals,
26# avoid using it where it can cause garbage to be printed
27# to the terminal.
28#
29def _osc_777_supported():
30
31    term = os.environ.get('TERM')
32
33    if term and (term.startswith('xterm') or term.startswith('vte')):
34
35        # Since vte version 4600, upstream silently ignores
36        # the OSC 777 without printing garbage to the terminal.
37        #
38        # For distros like Fedora who have patched vte, this
39        # will trigger a desktop notification and bring attention
40        # to the terminal.
41        #
42        vte_version = os.environ.get('VTE_VERSION')
43        try:
44            vte_version_int = int(vte_version)
45        except (ValueError, TypeError):
46            return False
47
48        if vte_version_int >= 4600:
49            return True
50
51    return False
52
53
54# A linux specific App implementation
55#
56class LinuxApp(App):
57
58    def notify(self, title, text):
59
60        # Currently we only try this notification method
61        # of sending an escape sequence to the terminal
62        #
63        if _osc_777_supported():
64            click.echo("\033]777;notify;{};{}\007".format(title, text), err=True)
65