-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfuzzel-ssh.py
More file actions
executable file
·53 lines (44 loc) · 1.54 KB
/
fuzzel-ssh.py
File metadata and controls
executable file
·53 lines (44 loc) · 1.54 KB
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
#!/usr/bin/python3 -u
"""
Description: Open SSH session in terminal
Author: thnikk
"""
from subprocess import Popen, PIPE
import sys
import json
import os
def notify(subject, body):
""" Create notification """
print(body)
# pylint: disable=consider-using-with
Popen(['notify-send', subject, body])
def get_selection(input_list, prompt="", max_lines=8) -> str:
""" Get selection from list with custom prompt """
length = str(min(len(input_list), max_lines))
with Popen(
["fuzzel", "--dmenu", "-l", length, "-p", prompt],
stdin=PIPE, stdout=PIPE, stderr=PIPE
) as fuzzel:
selection = fuzzel.communicate(
input=bytes("\n".join(input_list), 'utf-8'))[0]
if fuzzel.returncode != 0:
sys.exit(1)
return selection.decode().strip()
def main():
""" Main function """
config_file = os.path.expanduser('~/.config/fuzzel/fuzzel-ssh.json')
try:
with open(config_file, 'r', encoding='utf-8') as file:
config = json.loads(file.read())
except FileNotFoundError:
with open(config_file, 'w', encoding='utf-8') as file:
ex_config = {'nickname': 'user@IP'}
file.write(json.dumps(ex_config))
notify('fuzzel-ssh', f'Default config created in {config_file}, '
'please edit before running again.')
sys.exit(1)
selection = get_selection(list(config))
# pylint: disable=consider-using-with
Popen(['wezterm', 'ssh', config[selection]])
if __name__ == "__main__":
main()