Skip to content

Commit

Permalink
gchrome cleanup & bugfix
Browse files Browse the repository at this point in the history
fixed: passing command line arguments (like urls) now works
  • Loading branch information
matthewfallshaw committed Jul 28, 2019
1 parent daac18a commit be41db3
Show file tree
Hide file tree
Showing 9 changed files with 63 additions and 61 deletions.
16 changes: 2 additions & 14 deletions config/fish/completions/gchrome.fish
Original file line number Diff line number Diff line change
@@ -1,22 +1,10 @@
set SEP 'SHUQUAOZ6EIYUO4KAHTH' # a presumed unique key

if test -d "$HOME/.config/google-chrome"
set __gchrome_base_dir "$HOME/.config/google-chrome"
else
set __gchrome_base_dir "$HOME/Library/Application Support/Google/Chrome"
end

function __gchrome_get_profiles
jq -r '[.profile.info_cache | to_entries[] | {"key": .key, "value": .value.name}] | .[] | "\(.value|@sh)'$SEP'\(.key|@sh)"' "$__gchrome_base_dir/Local State"
end

function __gchrome_get_profile_from_pair
set -l k_v (string split $SEP $argv[1])
set -l k_v (string split \t $argv[1])
echo $k_v[2]
end

function __gchrome_get_profile_alias_from_pair
set -l k_v (string split $SEP $argv[1])
set -l k_v (string split \t $argv[1])
echo $k_v[1]
end

Expand Down
10 changes: 10 additions & 0 deletions config/fish/functions/__gchrome_base_dir.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function __gchrome_base_dir
if not set -q __gchrome_base_dir
if test -d "$HOME/.config/google-chrome"
set __gchrome_base_dir "$HOME/.config/google-chrome"
else
set __gchrome_base_dir "$HOME/Library/Application Support/Google/Chrome"
end
end
echo $__gchrome_base_dir
end
11 changes: 11 additions & 0 deletions config/fish/functions/__gchrome_check_dependencies.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function __gchrome_check_dependencies
if not which -s jq
echo "gchrome: google-chrome profile launcher
Missing dependency: `jq` not found. (`jq` is a lightweight and flexible command-line JSON processor.)
Install it with something like:
`brew install jq`
or
`sudo apt-get install jq`"
return 1
end
end
13 changes: 13 additions & 0 deletions config/fish/functions/__gchrome_executable.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function __gchrome_executable
if not set -q __gchrome_executable
if test -f "/usr/bin/google-chrome"
set __gchrome_executable "/usr/bin/google-chrome"
else if test -f "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
set __gchrome_executable "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
else
echo "Can't find Chrome executable" >&2
return 1
end
end
echo $__gchrome_executable
end
4 changes: 4 additions & 0 deletions config/fish/functions/__gchrome_get_last_used_profile.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function __gchrome_get_last_used_profile
set -l __gchrome_base_dir (__gchrome_base_dir)
jq -r '.profile.last_used' "$__gchrome_base_dir/Local State"
end
4 changes: 4 additions & 0 deletions config/fish/functions/__gchrome_get_profiles.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
function __gchrome_get_profiles
set -l __gchrome_base_dir (__gchrome_base_dir)
jq -r '[.profile.info_cache | to_entries[] | {"key": .key, "value": .value.name}] | .[] | "\(.value|@sh)\t\(.key|@sh)"' "$__gchrome_base_dir/Local State"
end
7 changes: 7 additions & 0 deletions config/fish/functions/__gchrome_is_valid_profile.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
function __gchrome_is_valid_profile
set -l __gchrome_base_dir (__gchrome_base_dir)
set -l profile_path "$__gchrome_base_dir/$argv[1]"
[ -n "$argv[1]" ] &&
[ -d (readlink -f "$profile_path") ] &&
[ -f (readlink -f "$profile_path/Cookies") ]
end
6 changes: 6 additions & 0 deletions config/fish/functions/__gchrome_is_valid_profile_alias.fish
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
function __gchrome_is_valid_profile_alias
if [ -z $argv[1] ]; return 1; end

set -l profile (__gchrome_get_profile_from_alias $argv[1])
__gchrome_is_valid_profile $profile && return 0 || return 1
end
53 changes: 6 additions & 47 deletions config/fish/functions/gchrome.fish
Original file line number Diff line number Diff line change
@@ -1,52 +1,18 @@
# google-chrome profile launcher
#
# Adaptation of various ideas and solutions mentioned in https://superuser.com/a/377195/28775
# This script is meant to be "sourced" into your bash-profile. It provides command 'gchrome'
# and provides auto-completion of the profile-name.
# Converted for fish from https://github.com/codemedic/bash-ninja/blob/master/gchrome

source $__fish_config_dir/completions/gchrome.fish

if test -f "/usr/bin/google-chrome"
set __gchrome_executable "/usr/bin/google-chrome"
else
set __gchrome_executable "/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"
end
function gchrome -d "Launch a new Google Chrome window from specified Profile"
__gchrome_check_dependencies || return 1

function __gchrome_check_dependencies
if not which -s jq
echo "gchrome: google-chrome profile launcher
Missing dependency: `jq` not found. (`jq` is a lightweight and flexible command-line JSON processor.)
Install it with something like:
`brew install jq`
or
`sudo apt-get install jq`"
return 1
if ! __gchrome_is_valid_profile $argv[1] && ! __gchrome_is_valid_profile_alias $argv[1]
set --prepend argv "$__gchrome_get_last_used_profile"
end
end

function __gchrome_is_valid_profile
set -l profile_path "$__gchrome_base_dir/$argv[1]"
[ -n "$argv[1]" ] &&
[ -d (readlink -f "$profile_path") ] &&
[ -f (readlink -f "$profile_path/Cookies") ]
end

function __gchrome_is_valid_profile_alias
if [ -z $argv[1] ]; return 1; end

set -l profile (__gchrome_get_profile_from_alias $argv[1])
__gchrome_is_valid_profile $profile && return 0 || return 1
end

function __gchrome_get_last_used_profile
jq -r '.profile.last_used' "$__gchrome_base_dir/Local State"
end

function __gchrome
__gchrome_check_dependencies || return 1

set -l opts
set -l opts --new-window
if __gchrome_is_valid_profile "$argv[1]"
set --append opts "--profile-directory=$argv[1]"
set --erase argv[1]
Expand All @@ -55,13 +21,6 @@ function __gchrome
set --erase argv[1]
end

set -l __gchrome_executable (__gchrome_executable)
$__gchrome_executable $opts "$argv" >/dev/null 2>&1
end

function gchrome -d "Launch a new Google Chrome window from specified Profile"
if __gchrome_is_valid_profile $argv[1] || __gchrome_is_valid_profile_alias $argv[1]
__gchrome "$argv"
else
__gchrome "$__gchrome_get_last_used_profile" "$argv"
end
end

0 comments on commit be41db3

Please sign in to comment.