1
- import os
1
+ import subprocess
2
2
from pathlib import Path
3
3
4
4
from prompt_toolkit import print_formatted_text
@@ -20,50 +20,70 @@ def replace_home_with_tilde(path: Path) -> Path:
20
20
return Path ("~" ) / relative_path
21
21
22
22
23
- def ensure_zsh () -> None :
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 )
29
- if "zsh" not in shell :
30
- print_error (f"'{ shell } ' is currently unsupported. "
31
- f"Please open an issue https://github.com/djcopley/ShellOracle/issues." )
32
- exit (1 )
23
+ supported_shells = ["zsh" , "bash" ]
33
24
34
25
35
- zshrc_path = Path .home () / ".zshrc"
36
- shelloracle_zsh_dest = Path .home () / ".shelloracle.zsh"
26
+ def get_installed_shells () -> list [str ]:
27
+ shells = []
28
+ for shell in supported_shells :
29
+ if subprocess .run (["command" , "-v" , shell ], stdout = subprocess .DEVNULL ):
30
+ shells .append (shell )
31
+ return shells
37
32
38
33
39
- def write_shelloracle_zsh () -> None :
40
- zsh_path = Path (__file__ ).parent .absolute () / "shelloracle.zsh"
41
- shelloracle_zsh = zsh_path .read_bytes ()
42
- shelloracle_zsh_dest .write_bytes (shelloracle_zsh )
43
- print_info (f"Successfully wrote key bindings to { replace_home_with_tilde (shelloracle_zsh_dest )} " )
34
+ def get_bundled_script_path (shell : str ) -> Path :
35
+ parent = Path (__file__ ).parent
36
+ if shell == "zsh" :
37
+ return parent / "shelloracle.zsh"
38
+ elif shell == "bash" :
39
+ return parent / "shelloracle.bash"
44
40
45
41
46
- def update_zshrc () -> None :
47
- zshrc_path .touch (exist_ok = True )
48
- with zshrc_path .open ("r" ) as file :
42
+ def get_script_path (shell : str ) -> Path :
43
+ if shell == "zsh" :
44
+ return Path .home () / ".shelloracle.zsh"
45
+ elif shell == "bash" :
46
+ return Path .home () / ".shelloracle.bash"
47
+
48
+
49
+ def get_rc_path (shell : str ) -> Path :
50
+ if shell == "zsh" :
51
+ return Path .home () / ".zshrc"
52
+ elif shell == "bash" :
53
+ return Path .home () / ".bashrc"
54
+
55
+
56
+ def write_script_home (shell : str ) -> None :
57
+ shelloracle = get_bundled_script_path (shell ).read_bytes ()
58
+ destination = get_script_path (shell )
59
+ destination .write_bytes (shelloracle )
60
+ print_info (f"Successfully wrote key bindings to { replace_home_with_tilde (destination )} " )
61
+
62
+
63
+ def update_rc (shell : str ) -> None :
64
+ rc_path = get_rc_path (shell )
65
+ rc_path .touch (exist_ok = True )
66
+ with rc_path .open ("r" ) as file :
49
67
zshrc = file .read ()
50
- line = f"[ -f { shelloracle_zsh_dest } ] && source { shelloracle_zsh_dest } "
68
+ shelloracle_script = get_script_path (shell )
69
+ line = f"[ -f { shelloracle_script } ] && source { shelloracle_script } "
51
70
if line not in zshrc :
52
- with zshrc_path .open ("a" ) as file :
71
+ with rc_path .open ("a" ) as file :
53
72
file .write ("\n " )
54
73
file .write (line )
55
- print_info (f"Successfully updated { replace_home_with_tilde (zshrc_path )} " )
74
+ print_info (f"Successfully updated { replace_home_with_tilde (rc_path )} " )
56
75
57
76
58
77
def bootstrap () -> None :
59
78
with create_app_session_from_tty ():
60
- ensure_zsh ()
61
-
62
- if confirm ("Enable terminal keybindings?" , suffix = " ([y]/n) " ) is False :
79
+ if not (shells := get_installed_shells ()):
80
+ print_error (f"No compatible shells found. Supported shells: { ', ' .join (supported_shells )} " )
63
81
return
64
-
65
- write_shelloracle_zsh ()
66
- update_zshrc ()
82
+ if confirm ("Enable terminal keybindings and update rc ?" , suffix = " ([y]/n) " ) is False :
83
+ return
84
+ for shell in shells :
85
+ write_script_home (shell )
86
+ update_rc (shell )
67
87
68
88
69
89
if __name__ == '__main__' :
0 commit comments