forked from vanlocvo/Nautilus-OpenInVSCode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vscodium-nautilus.py
72 lines (62 loc) · 2.36 KB
/
vscodium-nautilus.py
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
# Base on Nautilus Admin
# Copyright (C) 2020 Van Loc Vo <vvanloc.vo@gmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import os, subprocess
from gi import require_version
import urllib.parse
require_version('Gtk', '3.0')
require_version('Nautilus', '3.0')
from gi.repository import Nautilus, GObject
from gettext import gettext
CODE = '/usr/bin/codium'
class VSCodeNautilus(Nautilus.MenuProvider, GObject.GObject):
def __init__(self):
pass
def get_file_items(self, window, files):
"""Returns the menu items to display when one or more files/folders are
selected."""
# Don't show when more than 1 file is selected
if len(files) != 1:
return
file = files[0]
# Add the menu items
items = []
self.window = window
if file.get_uri_scheme() == "file": # must be a local file/directory
if os.path.exists(CODE):
items += [self._create_nautilus_item(file)]
return items
def get_background_items(self, window, file):
"""Returns the menu items to display when no file/folder is selected
(i.e. when right-clicking the background)."""
# Add the menu items
items = []
self.window = window
if file.is_directory() and file.get_uri_scheme() == "file":
items += [self._create_nautilus_item(file)]
return items
def _create_nautilus_item(self, file):
"""Creates the 'Open with VSCodium' menu item."""
item = Nautilus.MenuItem(name="VSCodiumNautilus::Nautilus",
label=gettext("Open in Codium"),
tip=gettext("Open this folder/file in VSCodium"))
item.connect("activate", self._nautilus_run, file)
return item
def _nautilus_run(self, menu, file):
"""'Open with VSCodium' menu item callback."""
uri = file.get_uri()
uri = urllib.parse.unquote(uri)
uri = uri.replace('file://','')
subprocess.Popen([CODE, uri])