Commit 20ed4398 authored by Bruno Pinto's avatar Bruno Pinto Committed by Jorge Bucaran

Naming functions with `omf.` namespace.

Having a clear namespace `omf.` improves the readability of the code as
we clarify what is the function name and what is the namespace.
parent 2ecc1fc1
...@@ -59,7 +59,7 @@ The following syntax is more concise, but arguably less transparent. ...@@ -59,7 +59,7 @@ The following syntax is more concise, but arguably less transparent.
> You still may use `and` / `or` statements if you consider `if..else..then` to be overkill. > You still may use `and` / `or` statements if you consider `if..else..then` to be overkill.
```fish ```fish
set -q VAR; set -g VAR 42 set -q VAR; or set -g VAR 42
``` ```
### Functions ### Functions
...@@ -80,16 +80,9 @@ function greet -a message -d "Display a greeting message" ...@@ -80,16 +80,9 @@ function greet -a message -d "Display a greeting message"
end end
``` ```
`fish` does not have private functions, so in order to avoid polluting the global namespace, use a prefix based in the scope of your code. For example, if you are writing a `ninja` plugin using `__ninja_function_name`. In order to avoid name collision, name your functions using a prefix based on the name of your package. For example, if you are writing a `ninja` package use `ninja.function_name`.
If you are writing a function inside another function, prefix the inner one with the parent's name. `fish` does not have private functions, so in order to avoid polluting the global namespace, use double underscore before your function name. For example, if you are writing a `ninja` plugin using `__ninja.function_name`.
```fish
function parent
function parent_child
end
end
```
Note that it's still possible to mimic private functions in `fish` by deleting the function before returning using `functions -e function_name` Note that it's still possible to mimic private functions in `fish` by deleting the function before returning using `functions -e function_name`
......
function omf_destroy -d "Remove Oh My Fish" function omf.destroy -d "Remove Oh My Fish"
echo (omf::dim)"Removing Oh My Fish..."(omf::off) echo (omf::dim)"Removing Oh My Fish..."(omf::off)
omf_remove_package (basename $OMF_PATH/pkg/*) >/dev/null ^&1 omf.remove_package (basename $OMF_PATH/pkg/*) >/dev/null ^&1
if test -e "$HOME/.config/fish/config.copy" if test -e "$HOME/.config/fish/config.copy"
mv "$HOME/.config/fish/config".{copy,fish} mv "$HOME/.config/fish/config".{copy,fish}
......
function omf_help function omf.help
echo \n"\ echo \n"\
"(omf::dim)"Usage"(omf::off)" "(omf::dim)"Usage"(omf::off)"
omf "(omf::em)"action"(omf::off)" [options] omf "(omf::em)"action"(omf::off)" [options]
......
function omf_install_package function omf.install_package
for search in $argv for search in $argv
if test -e $OMF_PATH/db/pkg/$search if test -e $OMF_PATH/db/pkg/$search
set target pkg/$search set target pkg/$search
...@@ -20,7 +20,7 @@ function omf_install_package ...@@ -20,7 +20,7 @@ function omf_install_package
if test -e $OMF_PATH/$target if test -e $OMF_PATH/$target
echo (omf::dim)"Updating $search..."(omf::off) echo (omf::dim)"Updating $search..."(omf::off)
pushd $OMF_PATH/$target pushd $OMF_PATH/$target
omf_util_sync "origin" >/dev/null ^&1 omf.util_sync "origin" >/dev/null ^&1
popd popd
echo (omf::em)"✔ $search up to date."(omf::off) echo (omf::em)"✔ $search up to date."(omf::off)
else else
......
# List all packages available to install from the registry. # List all packages available to install from the registry.
function omf_list_db_packages function omf.list_db_packages
for item in (basename $OMF_PATH/db/pkg/*) for item in (basename $OMF_PATH/db/pkg/*)
contains $item (basename {$OMF_PATH,$OMF_CONFIG}/pkg/*); or echo $item contains $item (basename {$OMF_PATH,$OMF_CONFIG}/pkg/*); or echo $item
end end
......
# List all packages installed from the registry. # List all packages installed from the registry.
function omf_list_installed_packages function omf.list_installed_packages
for item in (basename $OMF_PATH/pkg/*) for item in (basename $OMF_PATH/pkg/*)
test $item = omf; or echo $item test $item = omf; or echo $item
end end
......
function omf_list_installed_themes function omf.list_installed_themes
basename $OMF_PATH/themes/* basename $OMF_PATH/themes/*
end end
# List all custom packages and packages installed from the registry. # List all custom packages and packages installed from the registry.
function omf_list_local_packages function omf.list_local_packages
for item in (basename {$OMF_PATH,$OMF_CONFIG}/pkg/*) for item in (basename {$OMF_PATH,$OMF_CONFIG}/pkg/*)
test $item = omf; or echo $item test $item = omf; or echo $item
end end
......
function omf_list_themes function omf.list_themes
set -l seen "" set -l seen ""
for theme in (basename $OMF_PATH/db/themes/*) \ for theme in (basename $OMF_PATH/db/themes/*) \
(basename {$OMF_PATH,$OMF_CONFIG}/themes/*) (basename {$OMF_PATH,$OMF_CONFIG}/themes/*)
......
function omf_new -a option name function omf.new -a option name
switch $option switch $option
case "p" "pkg" "pack" "packg" "package" case "p" "pkg" "pack" "packg" "package"
set option "pkg" set option "pkg"
...@@ -9,12 +9,12 @@ function omf_new -a option name ...@@ -9,12 +9,12 @@ function omf_new -a option name
return $OMF_INVALID_ARG return $OMF_INVALID_ARG
end end
if not omf_util_valid_package "$name" if not omf.util_valid_package "$name"
echo (omf::err)"$name is not a valid package/theme name"(omf::off) 1^&2 echo (omf::err)"$name is not a valid package/theme name"(omf::off) 1^&2
return $OMF_INVALID_ARG return $OMF_INVALID_ARG
end end
if set -l dir (omf_util_mkdir "$option/$name") if set -l dir (omf.util_mkdir "$option/$name")
cd $dir cd $dir
set -l github (git config github.user) set -l github (git config github.user)
...@@ -23,7 +23,7 @@ function omf_new -a option name ...@@ -23,7 +23,7 @@ function omf_new -a option name
set -l user (git config user.name) set -l user (git config user.name)
test -z "$user"; and set user "{{USER}}" test -z "$user"; and set user "{{USER}}"
omf_new_from_template "$OMF_PATH/pkg/omf/templates/$option" \ omf.new_from_template "$OMF_PATH/pkg/omf/templates/$option" \
$github $user $name $github $user $name
echo (omf::em)"Switched to $dir"(omf::off) echo (omf::em)"Switched to $dir"(omf::off)
......
function omf_new_from_template -a path github user name function omf.new_from_template -a path github user name
for file in $path/* for file in $path/*
if test -d $file if test -d $file
mkdir (basename $file) mkdir (basename $file)
pushd (basename $file) pushd (basename $file)
omf_new_from_template $file $github $user $name omf.new_from_template $file $github $user $name
else else
set -l target (begin set -l target (begin
if test (basename $file) = "{{NAME}}.fish" if test (basename $file) = "{{NAME}}.fish"
......
function omf_query_env function omf.query_env
function __omf_print_pretty_path -a path function __omf.print_pretty_path -a path
printf "%s\n" $path \ printf "%s\n" $path \
| sed "s|$HOME|"(omf::em)"~"(omf::off)"|g" \ | sed "s|$HOME|"(omf::em)"~"(omf::off)"|g" \
| sed "s|/|"(omf::em)"/"(omf::off)"|g" | sed "s|/|"(omf::em)"/"(omf::off)"|g"
end end
if not set -q argv[1] if not set -q argv[1]
for var in (set) for var in (set)
echo (omf::dim)(echo $var | awk '{ printf $1"\n"; }')(omf::off) echo (omf::dim)(echo $var | awk '{ printf $1"\n"; }')(omf::off)
echo (omf::em)(__omf_print_pretty_path (echo $var | awk '{$1=""; print $0}'))(omf::off) echo (omf::em)(__omf.print_pretty_path (echo $var | awk '{$1=""; print $0}'))(omf::off)
end end
else else
for key in $$argv[1] for key in $$argv[1]
__omf_print_pretty_path $key __omf.print_pretty_path $key
end end
end end
end end
function omf_remove_package function omf.remove_package
for pkg in $argv for pkg in $argv
if not omf_util_valid_package $pkg if not omf.util_valid_package $pkg
if test $pkg = "omf" if test $pkg = "omf"
echo (omf::err)"You can't remove `omf`"(omf::off) 1^&2 echo (omf::err)"You can't remove `omf`"(omf::off) 1^&2
else else
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
# name Name of the package. # name Name of the package.
# [url] URL to the package repository. # [url] URL to the package repository.
function omf_submit -a name url -d "Submit a package to the registry" function omf.submit -a name url -d "Submit a package to the registry"
switch (dirname $name) switch (dirname $name)
case pkg case pkg
case themes case themes
...@@ -15,7 +15,7 @@ function omf_submit -a name url -d "Submit a package to the registry" ...@@ -15,7 +15,7 @@ function omf_submit -a name url -d "Submit a package to the registry"
end end
set -l pkg (basename $name) set -l pkg (basename $name)
if not omf_util_valid_package $pkg if not omf.util_valid_package $pkg
echo (omf::err)"$pkg is not a valid package/theme name"(omf::off) 1^&2 echo (omf::err)"$pkg is not a valid package/theme name"(omf::off) 1^&2
return $OMF_INVALID_ARG return $OMF_INVALID_ARG
end end
......
function omf_theme function omf.theme
if not test -e $OMF_CONFIG/themes/$argv[1] if not test -e $OMF_CONFIG/themes/$argv[1]
if not test -e $OMF_PATH/themes/$argv[1] if not test -e $OMF_PATH/themes/$argv[1]
set -l theme $OMF_PATH/db/themes/$argv[1] set -l theme $OMF_PATH/db/themes/$argv[1]
......
function omf_update function omf.update
set -l repo "upstream" set -l repo "upstream"
test -z (git config --get remote.upstream.url) test -z (git config --get remote.upstream.url)
and set -l repo "origin" and set -l repo "origin"
...@@ -10,7 +10,7 @@ function omf_update ...@@ -10,7 +10,7 @@ function omf_update
if git pull --rebase $repo master >/dev/null ^&1 if git pull --rebase $repo master >/dev/null ^&1
git stash apply >/dev/null ^&1 git stash apply >/dev/null ^&1
else else
omf_util_sync "origin" omf.util_sync "origin"
end end
end end
end end
\ No newline at end of file
function omf_version function omf.version
echo "Oh My Fish! $OMF_VERSION" echo "Oh My Fish! $OMF_VERSION"
end end
# SYNOPSIS # SYNOPSIS
# Completions for Oh My Fish CLI # Completions for Oh My Fish CLI
function __omf_is_single_opt function __omf.is_single_opt
test (count (commandline -opc)) -le 1 test (count (commandline -opc)) -le 1
end end
function __omf_opt_is function __omf.opt_is
set -l cmd (commandline -opc) set -l cmd (commandline -opc)
test (count $cmd) -gt 1; and contains -- $cmd[2] $argv test (count $cmd) -gt 1; and contains -- $cmd[2] $argv
end end
complete --no-files -c omf -d "Oh My Fish" complete --no-files -c omf -d "Oh My Fish"
complete -c omf -n "__omf_opt_is q query" -a (printf "%s " (set | awk '{ printf $1"\n"; }')) complete -c omf -n "__omf.opt_is q query" -a (printf "%s " (set | awk '{ printf $1"\n"; }'))
complete -c omf -n "__omf_opt_is r rm remove" -a (printf "%s " (omf_list_local_packages) (omf_list_installed_themes)) complete -c omf -n "__omf.opt_is r rm remove" -a (printf "%s " (omf.list_local_packages) (omf.list_installed_themes))
complete -c omf -n "__omf_opt_is i install" -a (printf "%s " (omf_list_db_packages)) complete -c omf -n "__omf.opt_is i install" -a (printf "%s " (omf.list_db_packages))
complete -c omf -n "__omf_opt_is t theme" -a (printf "%s " (omf_list_themes)) complete -c omf -n "__omf.opt_is t theme" -a (printf "%s " (omf.list_themes))
complete -c omf -a list -n "__omf_is_single_opt" -d "List local packages" complete -c omf -a list -n "__omf.is_single_opt" -d "List local packages"
complete -c omf -a install -n "__omf_is_single_opt" -d "Install one or more packages" complete -c omf -a install -n "__omf.is_single_opt" -d "Install one or more packages"
complete -c omf -a theme -n "__omf_is_single_opt" -d "List / Use themes" complete -c omf -a theme -n "__omf.is_single_opt" -d "List / Use themes"
complete -c omf -a remove -n "__omf_is_single_opt" -d "Remove a theme or package" complete -c omf -a remove -n "__omf.is_single_opt" -d "Remove a theme or package"
complete -c omf -a update -n "__omf_is_single_opt" -d "Update Oh My Fish" complete -c omf -a update -n "__omf.is_single_opt" -d "Update Oh My Fish"
complete -c omf -a new -n "__omf_is_single_opt" -d "Create a new package from a template" complete -c omf -a new -n "__omf.is_single_opt" -d "Create a new package from a template"
complete -c omf -a submit -n "__omf_is_single_opt" -d "Submit a package to the registry" complete -c omf -a submit -n "__omf.is_single_opt" -d "Submit a package to the registry"
complete -c omf -a query -n "__omf_is_single_opt" -d "Query environment variables" complete -c omf -a query -n "__omf.is_single_opt" -d "Query environment variables"
complete -c omf -a help -n "__omf_is_single_opt" -d "Display this help" complete -c omf -a help -n "__omf.is_single_opt" -d "Display this help"
complete -c omf -a version -n "__omf_is_single_opt" -d "Display version" complete -c omf -a version -n "__omf.is_single_opt" -d "Display version"
complete -c omf -a destroy -n "__omf_is_single_opt" -d "Remove Oh My Fish" complete -c omf -a destroy -n "__omf.is_single_opt" -d "Remove Oh My Fish"
...@@ -27,19 +27,19 @@ end ...@@ -27,19 +27,19 @@ end
function omf -d "Oh My Fish" function omf -d "Oh My Fish"
if test (count $argv) -eq 0 if test (count $argv) -eq 0
omf_help; and return 0 omf.help; and return 0
end end
switch $argv[1] switch $argv[1]
case "v" "ver" "version" case "v" "ver" "version"
omf_version omf.version
case "q" "query" case "q" "query"
switch (count $argv) switch (count $argv)
case 1 case 1
omf_query_env omf.query_env
case 2 case 2
omf_query_env "$argv[2]" omf.query_env "$argv[2]"
case "*" case "*"
echo (omf::err)"Invalid number of arguments"(omf::off) 1^&2 echo (omf::err)"Invalid number of arguments"(omf::off) 1^&2
echo "Usage: $_ "(omf::em)"$argv[1]"(omf::off)" [<variable name>]" 1^&2 echo "Usage: $_ "(omf::em)"$argv[1]"(omf::off)" [<variable name>]" 1^&2
...@@ -47,16 +47,16 @@ function omf -d "Oh My Fish" ...@@ -47,16 +47,16 @@ function omf -d "Oh My Fish"
end end
case "h" "help" case "h" "help"
omf_help omf.help
case "l" "li" "lis" "lst" "list" case "l" "li" "lis" "lst" "list"
omf_list_local_packages | column omf.list_local_packages | column
case "i" "install" "get" case "i" "install" "get"
if test (count $argv) -eq 1 if test (count $argv) -eq 1
omf_list_db_packages | column omf.list_db_packages | column
else else
omf_install_package $argv[2..-1] omf.install_package $argv[2..-1]
refresh refresh
end end
...@@ -66,11 +66,11 @@ function omf -d "Oh My Fish" ...@@ -66,11 +66,11 @@ function omf -d "Oh My Fish"
set -l regex "[[:<:]]($theme)[[:>:]]" set -l regex "[[:<:]]($theme)[[:>:]]"
test "$OSTYPE" != "Darwin"; and set regex "\b($theme)\b" test "$OSTYPE" != "Darwin"; and set regex "\b($theme)\b"
omf_list_themes | column | sed -E "s/$regex/"(omf::em)"\1"(omf::off)"/" omf.list_themes | column | sed -E "s/$regex/"(omf::em)"\1"(omf::off)"/"
omf::off omf::off
else if test (count $argv) -eq 2 else if test (count $argv) -eq 2
omf_theme $argv[2] omf.theme $argv[2]
refresh refresh
else else
echo (omf::err)"Invalid number of arguments"(omf::off) 1^&2 echo (omf::err)"Invalid number of arguments"(omf::off) 1^&2
...@@ -84,26 +84,26 @@ function omf -d "Oh My Fish" ...@@ -84,26 +84,26 @@ function omf -d "Oh My Fish"
echo "Usage: $_ "(omf::em)"$argv[1]"(omf::off)" <[package|theme] name>" 1^&2 echo "Usage: $_ "(omf::em)"$argv[1]"(omf::off)" <[package|theme] name>" 1^&2
return $OMF_INVALID_ARG return $OMF_INVALID_ARG
end end
omf_remove_package $argv[2..-1] omf.remove_package $argv[2..-1]
case "u" "up" "upd" "update" case "u" "up" "upd" "update"
pushd $OMF_PATH pushd $OMF_PATH
echo (omf::em)"Updating Oh My Fish..."(omf::off) echo (omf::em)"Updating Oh My Fish..."(omf::off)
if omf_update if omf.update
echo (omf::em)"Oh My Fish is up to date."(omf::off) echo (omf::em)"Oh My Fish is up to date."(omf::off)
else else
echo (omf::err)"Oh My Fish failed to update."(omf::off) echo (omf::err)"Oh My Fish failed to update."(omf::off)
echo "Please open a new issue here → "(omf::em)"git.io/omf-issues"(omf::off) echo "Please open a new issue here → "(omf::em)"git.io/omf-issues"(omf::off)
end end
omf_theme (cat $OMF_CONFIG/theme) omf.theme (cat $OMF_CONFIG/theme)
omf_install_package (omf_list_installed_packages) omf.install_package (omf.list_installed_packages)
popd popd
refresh refresh
case "s" "su" "sub" "submit" case "s" "su" "sub" "submit"
switch (count $argv) switch (count $argv)
case 3 case 3
omf_submit $argv[2] $argv[3] omf.submit $argv[2] $argv[3]
case "*" case "*"
echo (omf::err)"Argument missing"(omf::off) 1^&2 echo (omf::err)"Argument missing"(omf::off) 1^&2
echo "Usage: $_ "(omf::em)"$argv[1]"(omf::off)" "(omf::em)"pkg|themes"(omf::off)"/<name> <url>" 1^&2 echo "Usage: $_ "(omf::em)"$argv[1]"(omf::off)" "(omf::em)"pkg|themes"(omf::off)"/<name> <url>" 1^&2
...@@ -116,10 +116,10 @@ function omf -d "Oh My Fish" ...@@ -116,10 +116,10 @@ function omf -d "Oh My Fish"
echo "Usage: $_ "(omf::em)"$argv[1]"(omf::off)" "(omf::em)"pkg|theme"(omf::off)" <name>" 1^&2 echo "Usage: $_ "(omf::em)"$argv[1]"(omf::off)" "(omf::em)"pkg|theme"(omf::off)" <name>" 1^&2
return $OMF_MISSING_ARG return $OMF_MISSING_ARG
end end
omf_new $argv[2..-1] omf.new $argv[2..-1]
case "destroy" case "destroy"
omf_destroy omf.destroy
case "*" case "*"
echo (omf::err)"$argv[1] option not recognized"(omf::off) 1^&2 echo (omf::err)"$argv[1] option not recognized"(omf::off) 1^&2
......
function omf_util_fork_repo -a user repo function omf.util_fork_repo -a user repo
curl -u "$user" --fail --silent https://api.github.com/repos/$repo/forks \ curl -u "$user" --fail --silent https://api.github.com/repos/$repo/forks \
-d "{\"user\":\"$user\"}" >/dev/null ^&1 -d "{\"user\":\"$user\"}" >/dev/null ^&1
end end
\ No newline at end of file
function omf_util_mkdir -a name function omf.util_mkdir -a name
set -l name "$argv[1]" set -l name "$argv[1]"
if test -d "$OMF_CONFIG" if test -d "$OMF_CONFIG"
set name "$OMF_CONFIG/$name" set name "$OMF_CONFIG/$name"
......
function omf_util_sync -a remote function omf.util_sync -a remote
set -l repo $remote set -l repo $remote
set -q argv[1]; and set repo $argv[1] set -q argv[1]; and set repo $argv[1]
git fetch origin master git fetch origin master
git reset --hard FETCH_HEAD git reset --hard FETCH_HEAD
git clean -df git clean -df
end end
\ No newline at end of file
function omf_util_valid_package -a package function omf.util_valid_package -a package
test (echo "$package" | tr "[:upper:]" "[:lower:]") = "omf"; and return 10 test (echo "$package" | tr "[:upper:]" "[:lower:]") = "omf"; and return 10
switch $package switch $package
case {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}\* case {a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z}\*
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment