forked from talonhub/community
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwsl.py
205 lines (162 loc) · 5.75 KB
/
wsl.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
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
from talon import Context, Module, actions, imgui, settings, ui, app
import os
import subprocess
mod = Module()
mod.apps.ubuntu = """
os: windows
and app.name: ubuntu.exe
"""
ctx = Context()
ctx.matches = r"""
app: ubuntu
app: windows_terminal
and win.title: /Ubuntu/
"""
directories_to_remap = {}
directories_to_exclude = {}
user_path = os.path.expanduser("~")
if app.platform == "windows":
is_windows = True
import ctypes
GetUserNameEx = ctypes.windll.secur32.GetUserNameExW
NameDisplay = 3
size = ctypes.pointer(ctypes.c_ulong(0))
GetUserNameEx(NameDisplay, None, size)
nameBuffer = ctypes.create_unicode_buffer(size.contents.value)
GetUserNameEx(NameDisplay, nameBuffer, size)
one_drive_path = os.path.expanduser(os.path.join("~", "OneDrive"))
# this is probably not the correct way to check for onedrive, quick and dirty
if os.path.isdir(os.path.expanduser(os.path.join("~", r"OneDrive\Desktop"))):
default_folder = os.path.join("~", "Desktop")
directories_to_remap = {
"Desktop": os.path.join(one_drive_path, "Desktop"),
"Documents": os.path.join(one_drive_path, "Documents"),
"Downloads": os.path.join(user_path, "Downloads"),
"Music": os.path.join(user_path, "Music"),
"OneDrive": one_drive_path,
"Pictures": os.path.join(one_drive_path, "Pictures"),
"Videos": os.path.join(user_path, "Videos"),
}
else:
# todo use expanduser for cross platform support
directories_to_remap = {
"Desktop": os.path.join(user_path, "Desktop"),
"Documents": os.path.join(user_path, "Documents"),
"Downloads": os.path.join(user_path, "Downloads"),
"Music": os.path.join(user_path, "Music"),
"OneDrive": one_drive_path,
"Pictures": os.path.join(user_path, "Pictures"),
"Videos": os.path.join(user_path, "Videos"),
}
def get_win_path(wsl_path):
path = ""
try:
path = (
subprocess.check_output(["wsl", "wslpath", "-w", wsl_path])
.strip(b"\n")
.decode()
)
except:
path = ""
return path
def get_usr_path():
path = ""
try:
path = (
subprocess.check_output(["wsl", "wslpath", "-a", "~"]).strip(b"\n").decode()
)
except:
path = ""
return path
def get_wsl_path(win_path):
path = ""
try:
path = (
subprocess.check_output(["wsl", "wslpath", "-u", "'{}'".format(win_path)])
.strip(b"\n")
.decode()
)
except:
path = ""
return path
@ctx.action_class("user")
class user_actions:
def file_manager_refresh_title():
actions.skip()
def file_manager_current_path():
path = ui.active_window().title
try:
path = path.split(":")[1].lstrip()
except:
path = ""
# print("current: " + path)
if "~" in path:
# the only way I could find to correctly support the user folder:
# get absolute path of ~, and strip /mnt/x from the string
abs_usr_path = get_usr_path()
abs_usr_path = abs_usr_path[abs_usr_path.find("/home") : len(abs_usr_path)]
path = path.replace("~", abs_usr_path)
path = get_win_path(path)
if path in directories_to_remap:
path = directories_to_remap[path]
if path in directories_to_exclude:
path = ""
return path
# def file_manager_terminal_here():
# actions.key("ctrl-l")
# actions.insert("cmd.exe")
# actions.key("enter")
# def file_manager_show_properties():
# """Shows the properties for the file"""
# actions.key("alt-enter")
def file_manager_open_user_directory(path: str):
"""expands and opens the user directory"""
if path in directories_to_remap:
path = directories_to_remap[path]
path = os.path.expanduser(os.path.join("~", path))
if ":" in path:
path = get_wsl_path(path)
actions.user.file_manager_open_directory(path)
def file_manager_open_directory(path: str):
"""opens the directory that's already visible in the view"""
if ":" in str(path):
path = get_wsl_path(path)
actions.insert('cd "{}"'.format(path))
actions.key("enter")
actions.user.file_manager_refresh_title()
def file_manager_select_directory(path: str):
"""selects the directory"""
actions.insert('"{}"'.format(path))
def file_manager_new_folder(name: str):
"""Creates a new folder in a gui filemanager or inserts the command to do so for terminals"""
actions.insert('mkdir "{}"'.format(name))
def file_manager_open_file(path: str):
actions.insert(path)
# actions.key("enter")
def file_manager_select_file(path: str):
actions.insert(path)
def file_manager_open_volume(volume: str):
actions.user.file_manager_open_directory(volume)
def terminal_list_directories():
actions.insert("ls")
actions.key("enter")
def terminal_list_all_directories():
actions.insert("ls -a")
actions.key("enter")
def terminal_change_directory(path: str):
actions.insert("cd {}".format(path))
if path:
actions.key("enter")
def terminal_change_directory_root():
"""Root of current drive"""
actions.insert("cd /")
actions.key("enter")
def terminal_clear_screen():
"""Clear screen"""
actions.key("ctrl-l")
def terminal_run_last():
actions.key("up enter")
def terminal_kill_all():
actions.key("ctrl-c")
actions.insert("y")
actions.key("enter")