summaryrefslogtreecommitdiffstats
path: root/Completion/X/Command/_chromium
blob: be5e9bab396bacc858bf2ec1ff557d0f6ca22590 (plain) (blame)
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
#compdef chrome chromium google-chrome brave cromite helium helium-browser microsoft-edge opera opera-gx ungoogled-chromium vivaldi -P google-chrome-* brave-* microsoft-edge-* opera-* vivaldi-*

(( $+functions[_chromium_urls] )) ||
_chromium_urls() {
  # chromium doesn't allow you to open about: or chrome:// urls from the cli,
  # except for about:blank
  _alternative \
    'files: :_files' \
    'urls: :_urls' \
    'urls-about:about page:(about\:blank)'
}

# complete possible data directories (and, without -0, generic paths). as with
# firefox, we could try harder to look for the *exact* directory we need, but
# this is probably fine
# -0 <name>  instead of completing, insert directories into array <name>
# -a         use all possible directories instead of service-specific ones
(( $+functions[_chromium_data_dirs] )) ||
_chromium_data_dirs() {
  local cfg=${XDG_CONFIG_HOME:-$HOME/.config}
  local -a ca_opts expl dirs
  local -A opth

  zparseopts -A opth -D -F - \
    {M+:,P+:,q+:,r+:,R+:,S+:}=ca_opts 1 2 J: o: O: V: x: X: \
    0: a \
  || return

  local -a chrome_dirs=(
    $cfg/google-chrome{,-beta,-unstable,-canary}
    $HOME/.var/app/com.google.Chrome/config/google-chrome
    "$HOME/Library/Application Support/Google/Chrome"{,\ Beta,\ Dev,\ Canary}
  )
  local -a chromium_dirs=(
    $cfg/chromium
    $HOME/snap/chromium/current/.config/chromium
    $HOME/.var/app/org.chromium.Chromium/config/chromium
    "$HOME/Library/Application Support/Chromium"
  )
  local -a brave_dirs=(
    $cfg/brave{,-beta,-development,-unstable}
    $cfg/BraveSoftware/Brave-Browser{,-Beta,-Dev,-Development,-Nightly}
    $HOME/.var/app/com.brave.Browser/config/BraveSoftware/Brave-Browser
    "$HOME/Library/Application Support/BraveSoftware/Brave-Browser"{,-Beta,-Dev,-Development,-Nightly}
  )
  local -a cromite_dirs=(
    $cfg/cromite
  )
  local -a helium_dirs=(
    $cfg/net.imput.helium
    "$HOME/Library/Application Support/net.imput.helium"
  )
  local -a edge_dirs=(
    $cfg/microsoft-edge{,-beta,-dev,-canary}
    "$HOME/Library/Application Support/Microsoft Edge"{,\ Beta,\ Dev,\ Canary}
  )
  local -a opera_gx_dirs=(
    $cfg/opera-gx
    $HOME/.var/app/com.opera.opera-gx/config/opera
    "$HOME/Library/Application Support/com.operasoftware.OperaGX"
  )
  local -a opera_dirs=(
    $cfg/opera{,-beta,-developer}
    $HOME/.var/app/com.opera.Opera/config/opera
    "$HOME/Library/Application Support/com.operasoftware.Opera"
  )
  local -a vivaldi_dirs=(
    $cfg/vivaldi{,-snapshot}
    $HOME/.var/app/com.vivaldi.Vivaldi/config/vivaldi
    "$HOME/Library/Application Support/Vivaldi"{,\ Snapshot}
  )

  if (( $+opth[-a] )); then
    dirs=(
      $chrome_dirs $chromium_dirs $brave_dirs $cromite_dirs
      $helium_dirs $edge_dirs $opera_gx_dirs $opera_dirs $vivaldi_dirs
    )

  else
    case $service in
      google*|chrome*)      dirs=( $chrome_dirs ) ;;
      chromium*|ungoogled*) dirs=( $chromium_dirs ) ;;
      brave*)               dirs=( $brave_dirs ) ;;
      cromite*)             dirs=( $cromite_dirs ) ;;
      helium*)              dirs=( $helium_dirs ) ;;
      microsoft*|edge*)     dirs=( $edge_dirs ) ;;
      opera-gx*)            dirs=( $opera_gx_dirs ) ;;
      opera*)               dirs=( $opera_dirs ) ;;
      vivaldi*)             dirs=( $vivaldi_dirs ) ;;
    esac
  fi

  dirs=( $^dirs/'Local State'(#qN.-) )
  dirs=( ${dirs:h} )
  dirs=( ${(u)dirs} )

  (( $+opth[-0] )) && {
    trap "${opth[-0]}=( ${(j< >)${(@q+)dirs}} )" EXIT
    return $(( ! $#dirs ))
  }

  dirs=( ${dirs//:/\:} )

  _describe -t data-directories 'user data directory' \
    dirs "${(@)ca_opts}" && ret=0
  _wanted files expl 'user data directory' \
    _files -/ "${(@)ca_opts}" && ret=0

  return ret
}

(( $+functions[_chromium_profile_dirs] )) ||
_chromium_profile_dirs() {
  local -a ca_opts cli_dirs dirs profiles disp inst
  local -A opth

  zparseopts -A opth -D -F - \
    {1,2,J+:,M+:,o+:,O+:,P+:,q+,r+:,R+:,S+:,V+:,x+:,X+:}=ca_opts \
    a \
  || return

  cli_dirs=( ${${(MQ)words:#--user-data-dir=*}#*=} )

  if (( $+opth[-a] )); then
    _chromium_data_dirs -a0dirs
    dirs+=( $cli_dirs )
  else
    dirs=( $cli_dirs )
    (( $#dirs )) || _chromium_data_dirs -0dirs
  fi

  # the name given to a profile by the user has no relationship at all to the
  # profile's directory name. to make the results meaningful, we'll try to parse
  # the dirs + names out of the config
  (( $+commands[python3] )) &&
  profiles=( ${(f)"$(
    python3 -c 'if 1:
      import json, re, sys
      for st in sys.argv[1:]:
        with open(st) as f:
          ps = json.load(f).get("profile", {}).get("info_cache", {})
          for k, v in ps.items():
            dir = k
            nam = v.get("name", "")
            print(f"{nam}\0{dir}")
    ' $^dirs/'Local State'(#qN.-)
  )"} )

  (( $#profiles )) || profiles=( $^dirs/(Default|Profile\ *)(#qN/-:t) )

  disp=( ${${profiles//:/\\:}/$'\0'/:} )
  inst=( ${profiles#*$'\0'} )

  _describe -t profiles 'profile directory' disp inst "${(@)ca_opts}"
}

# chromium has hundreds of options, largely undocumented and apparently
# unstable. we make no attempt to support them all. these are the ones i found
# to be most commonly referenced
local -a args=(
  + '(excl)'
  '(- : *)--version[display version information]'

  + '(open)'
  '--incognito[open new Incognito window or tab]'
  '--new-tab[open new tab]'
  '--new-window[open new window]'

  + '(proxy)'
  '--proxy-server=-[specify proxy server]:proxy server:{
    compset -P "(http|socks|socks4|socks5)\://"
    _hosts "${(@)expl}"
  }'
  '--no-proxy-server[disable proxy server]'
  '--proxy-auto-detect[auto-detect proxy configuration]'
  # @todo this has to be http://, https://, or data:
  '--proxy-pac-url=-[specify proxy auto-configuration URL]:PAC-file URL:_urls'

  + misc
  '(*)--app=-[open specified URL in app mode]: :_chromium_urls'
  '--password-store=-[specify password storage back-end]:password store:(
    basic gnome gnome-libsecret kwallet kwallet5 kwallet6
  )'
  '--user-data-dir=-[specify user data directory]: :_chromium_data_dirs -a'
  '--profile-directory=-[specify profile directory]: :_chromium_profile_dirs'
  '(--app)*: :_chromium_urls'
)

[[ $service == (microsoft|edge)* ]] &&
args=( ${${args//incognito/inprivate}//Incognito/InPrivate} )

_arguments -s -S : $args