1
1
import shutil
2
- import subprocess
3
- from collections .abc import Iterable
4
2
from pathlib import Path
5
- from sys import platform
6
3
7
4
from . import Profile , profiles
8
5
from .desktop import create_desktop_file
9
- from .paths import qutebrowser_exe
10
- from .utils import env_menus , error , installed_menus , or_phrase
6
+ from .utils import error
11
7
12
8
13
9
def from_session (
@@ -27,117 +23,10 @@ def from_session(
27
23
return True
28
24
29
25
30
- def launch (profile : Profile , foreground : bool , qb_args : tuple [str , ...]) -> bool :
31
- if not profiles .exists (profile ):
32
- return False
33
-
34
- args = profile .cmdline () + list (qb_args )
35
- return launch_internal (foreground , args )
36
-
37
-
38
- def launch_qutebrowser (foreground : bool , qb_args : tuple [str , ...]) -> bool :
39
- return launch_internal (foreground , [qutebrowser_exe (), * qb_args ])
40
-
41
-
42
- def launch_internal (foreground : bool , args : list [str ]) -> bool :
43
- if not shutil .which (args [0 ]):
44
- error ("qutebrowser is not installed" )
45
- return False
46
-
47
- if foreground :
48
- return subprocess .run (args , check = False ).returncode == 0
49
- else :
50
- p = subprocess .Popen (args , stdout = subprocess .DEVNULL , stderr = subprocess .PIPE )
51
- try :
52
- # give qb a chance to validate input before returning to shell
53
- stdout , stderr = p .communicate (timeout = 0.1 )
54
- print (stderr .decode (errors = "ignore" ), end = "" )
55
- except subprocess .TimeoutExpired :
56
- pass
57
-
58
- return True
59
-
60
-
61
26
def desktop (profile : Profile ) -> bool :
62
27
exists = profile .exists ()
63
28
if exists :
64
29
create_desktop_file (profile )
65
30
else :
66
31
error (f"profile { profile .name } not found at { profile .root } " )
67
32
return exists
68
-
69
-
70
- def choose (
71
- profile_dir : Path , menu : str | None , foreground : bool , qb_args : tuple [str , ...]
72
- ) -> bool :
73
- menu = menu or next (installed_menus (), None )
74
- if not menu :
75
- possible_menus = or_phrase ([menu for menu in env_menus () if menu != "fzf-tmux" ])
76
- error (
77
- "no menu program found, use --menu to provide a dmenu-compatible menu or install one of "
78
- + possible_menus
79
- )
80
- return False
81
- if menu == "applescript" and platform != "darwin" :
82
- error (f"applescript cannot be used on a { platform } host" )
83
- return False
84
- real_profiles = {profile .name for profile in profile_dir .iterdir ()}
85
- if len (real_profiles ) == 0 :
86
- error ("no profiles" )
87
- return False
88
- profiles = [* real_profiles , "qutebrowser" ]
89
-
90
- command = menu_command (menu , profiles , qb_args )
91
- if not command :
92
- return False
93
-
94
- selection_cmd = subprocess .Popen (
95
- command ,
96
- shell = True ,
97
- stdout = subprocess .PIPE ,
98
- stderr = None ,
99
- )
100
- out = selection_cmd .stdout
101
- selection = out and out .read ().decode (errors = "ignore" ).rstrip ("\n " )
102
-
103
- if selection == "qutebrowser" and "qutebrowser" not in real_profiles :
104
- return launch_qutebrowser (foreground , qb_args )
105
- elif selection :
106
- profile = Profile (selection , profile_dir )
107
- return launch (profile , foreground , qb_args )
108
- else :
109
- error ("no profile selected" )
110
- return False
111
-
112
-
113
- def menu_command (
114
- menu : str , profiles : Iterable [str ], qb_args : tuple [str , ...]
115
- ) -> str | None :
116
- profiles = sorted (profiles )
117
- arg_string = " " .join (qb_args )
118
- if menu == "applescript" :
119
- profile_list = '", "' .join (profiles )
120
- return f"""osascript -e \' set profiles to {{"{ profile_list } "}}
121
- set profile to choose from list profiles with prompt "qutebrowser: { arg_string } " default items {{item 1 of profiles}}
122
- item 1 of profile\' """
123
-
124
- prompt = "-p qutebrowser"
125
- command = menu
126
- if len (menu .split (" " )) == 1 :
127
- program = Path (menu ).name
128
- if program == "rofi" :
129
- command = f"{ menu } -dmenu -no-custom { prompt } -mesg '{ arg_string } '"
130
- elif program == "wofi" :
131
- command = f"{ menu } --dmenu { prompt } "
132
- elif program .startswith ("dmenu" ):
133
- command = f"{ menu } { prompt } "
134
- elif program .startswith ("fzf" ):
135
- command = f"{ menu } --prompt 'qutebrowser '"
136
- elif program == "fuzzel" :
137
- command = f"{ menu } -d"
138
- exe = command .split (" " )[0 ]
139
- if not shutil .which (exe ):
140
- error (f"command '{ exe } ' not found" )
141
- return None
142
- profile_list = "\n " .join (profiles )
143
- return f'echo "{ profile_list } " | { command } '
0 commit comments