1
1
import os
2
- import shutil
3
- import subprocess
4
- import zipfile
5
2
from pathlib import Path
6
3
7
4
from prompt_toolkit import print_formatted_text
@@ -18,59 +15,36 @@ def print_error(error: str) -> None:
18
15
print_formatted_text (FormattedText ([("ansired" , error )]))
19
16
20
17
21
- def pip_output (text : str ) -> None :
22
- print_formatted_text (FormattedText ([("ansigray" , text )]), end = "" )
23
-
24
-
25
18
def replace_home_with_tilde (path : Path ) -> Path :
26
19
relative_path = path .relative_to (Path .home ())
27
20
return Path ("~" ) / relative_path
28
21
29
22
30
- def ensure_zsh ():
23
+ def ensure_zsh () -> None :
31
24
shell = os .environ .get ("SHELL" )
25
+ if shell is None :
26
+ print_error ("Unable to determine shell environment. If you are confident "
27
+ "that you are running in zsh, run again with `SHELL=zsh python3 -m shelloracle --init`" )
28
+ exit (1 )
32
29
if "zsh" not in shell :
33
30
print_error (f"'{ shell } ' is currently unsupported. "
34
31
f"Please open an issue https://github.com/djcopley/ShellOracle/issues." )
35
32
exit (1 )
36
33
37
34
38
- def install_shelloracle ():
39
- print_info ("Installing shelloracle" )
40
- python = shutil .which ("python3" )
41
- pip = subprocess .Popen ([python , "-m" , "pip" , "install" , "--upgrade" , "shelloracle" ],
42
- stdout = subprocess .PIPE , text = True )
43
- for line in pip .stdout .readlines ():
44
- pip_output (line )
45
- if (ret := pip .wait ()) == 0 :
46
- print_info ("Successfully installed shelloracle" )
47
- else :
48
- print_error (f"Unable to install shelloracle" )
49
- exit (ret )
50
-
51
-
52
35
zshrc_path = Path .home () / ".zshrc"
53
36
shelloracle_zsh_dest = Path .home () / ".shelloracle.zsh"
54
37
55
38
56
- def read_shelloracle_zsh ():
57
- working_dir = Path (__file__ ).parent .absolute ()
58
- zsh_path = "shelloracle.zsh"
59
- if working_dir .suffix == ".pyz" :
60
- with zipfile .ZipFile (working_dir , "r" ) as zip_app :
61
- shelloracle_zsh = zip_app .read (zsh_path )
62
- else :
63
- shelloracle_zsh = (working_dir / zsh_path ).read_bytes ()
64
- return shelloracle_zsh
65
-
66
-
67
- def write_shelloracle_zsh ():
68
- shelloracle_zsh = read_shelloracle_zsh ()
39
+ def write_shelloracle_zsh () -> None :
40
+ zsh_path = Path (__file__ ).parent .absolute () / "shelloracle.zsh"
41
+ shelloracle_zsh = zsh_path .read_bytes ()
69
42
shelloracle_zsh_dest .write_bytes (shelloracle_zsh )
70
43
print_info (f"Successfully wrote key bindings to { replace_home_with_tilde (shelloracle_zsh_dest )} " )
71
44
72
45
73
- def update_zshrc ():
46
+ def update_zshrc () -> None :
47
+ zshrc_path .touch (exist_ok = True )
74
48
with zshrc_path .open ("r" ) as file :
75
49
zshrc = file .read ()
76
50
line = f"[ -f { shelloracle_zsh_dest } ] && source { shelloracle_zsh_dest } "
@@ -81,17 +55,16 @@ def update_zshrc():
81
55
print_info (f"Successfully updated { replace_home_with_tilde (zshrc_path )} " )
82
56
83
57
84
- def install () :
58
+ def bootstrap () -> None :
85
59
with create_app_session_from_tty ():
86
60
ensure_zsh ()
87
- install_shelloracle ()
88
61
89
62
if confirm ("Enable terminal keybindings?" , suffix = " ([y]/n) " ) is False :
90
- exit ( 0 )
63
+ return
91
64
92
65
write_shelloracle_zsh ()
93
66
update_zshrc ()
94
67
95
68
96
69
if __name__ == '__main__' :
97
- install ()
70
+ bootstrap ()
0 commit comments