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.
This commit is contained in:
Bruno Pinto 2015-08-27 19:55:29 +01:00 committed by Jorge Bucaran
parent 2ecc1fc100
commit 20ed43983b
24 changed files with 75 additions and 81 deletions

View File

@ -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.
```fish
set -q VAR; set -g VAR 42
set -q VAR; or set -g VAR 42
```
### Functions
@ -80,16 +80,9 @@ function greet -a message -d "Display a greeting message"
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
function parent
function parent_child
end
end
```
`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`.
Note that it's still possible to mimic private functions in `fish` by deleting the function before returning using `functions -e function_name`

View File

@ -1,7 +1,7 @@
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)
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"
mv "$HOME/.config/fish/config".{copy,fish}

View File

@ -1,4 +1,4 @@
function omf_help
function omf.help
echo \n"\
"(omf::dim)"Usage"(omf::off)"
omf "(omf::em)"action"(omf::off)" [options]

View File

@ -1,4 +1,4 @@
function omf_install_package
function omf.install_package
for search in $argv
if test -e $OMF_PATH/db/pkg/$search
set target pkg/$search
@ -20,7 +20,7 @@ function omf_install_package
if test -e $OMF_PATH/$target
echo (omf::dim)"Updating $search..."(omf::off)
pushd $OMF_PATH/$target
omf_util_sync "origin" >/dev/null ^&1
omf.util_sync "origin" >/dev/null ^&1
popd
echo (omf::em)"$search up to date."(omf::off)
else

View File

@ -1,5 +1,5 @@
# 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/*)
contains $item (basename {$OMF_PATH,$OMF_CONFIG}/pkg/*); or echo $item
end

View File

@ -1,5 +1,5 @@
# List all packages installed from the registry.
function omf_list_installed_packages
function omf.list_installed_packages
for item in (basename $OMF_PATH/pkg/*)
test $item = omf; or echo $item
end

View File

@ -0,0 +1,3 @@
function omf.list_installed_themes
basename $OMF_PATH/themes/*
end

View File

@ -1,5 +1,5 @@
# 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/*)
test $item = omf; or echo $item
end

View File

@ -1,4 +1,4 @@
function omf_list_themes
function omf.list_themes
set -l seen ""
for theme in (basename $OMF_PATH/db/themes/*) \
(basename {$OMF_PATH,$OMF_CONFIG}/themes/*)

View File

@ -1,4 +1,4 @@
function omf_new -a option name
function omf.new -a option name
switch $option
case "p" "pkg" "pack" "packg" "package"
set option "pkg"
@ -9,12 +9,12 @@ function omf_new -a option name
return $OMF_INVALID_ARG
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
return $OMF_INVALID_ARG
end
if set -l dir (omf_util_mkdir "$option/$name")
if set -l dir (omf.util_mkdir "$option/$name")
cd $dir
set -l github (git config github.user)
@ -23,7 +23,7 @@ function omf_new -a option name
set -l user (git config user.name)
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
echo (omf::em)"Switched to $dir"(omf::off)

View File

@ -1,9 +1,9 @@
function omf_new_from_template -a path github user name
function omf.new_from_template -a path github user name
for file in $path/*
if test -d $file
mkdir (basename $file)
pushd (basename $file)
omf_new_from_template $file $github $user $name
omf.new_from_template $file $github $user $name
else
set -l target (begin
if test (basename $file) = "{{NAME}}.fish"

View File

@ -1,17 +1,18 @@
function omf_query_env
function __omf_print_pretty_path -a path
function omf.query_env
function __omf.print_pretty_path -a path
printf "%s\n" $path \
| sed "s|$HOME|"(omf::em)"~"(omf::off)"|g" \
| sed "s|/|"(omf::em)"/"(omf::off)"|g"
end
if not set -q argv[1]
for var in (set)
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
else
for key in $$argv[1]
__omf_print_pretty_path $key
__omf.print_pretty_path $key
end
end
end

View File

@ -1,6 +1,6 @@
function omf_remove_package
function omf.remove_package
for pkg in $argv
if not omf_util_valid_package $pkg
if not omf.util_valid_package $pkg
if test $pkg = "omf"
echo (omf::err)"You can't remove `omf`"(omf::off) 1^&2
else

View File

@ -5,7 +5,7 @@
# name Name of the package.
# [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)
case pkg
case themes
@ -15,7 +15,7 @@ function omf_submit -a name url -d "Submit a package to the registry"
end
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
return $OMF_INVALID_ARG
end

View File

@ -1,4 +1,4 @@
function omf_theme
function omf.theme
if not test -e $OMF_CONFIG/themes/$argv[1]
if not test -e $OMF_PATH/themes/$argv[1]
set -l theme $OMF_PATH/db/themes/$argv[1]

View File

@ -1,4 +1,4 @@
function omf_update
function omf.update
set -l repo "upstream"
test -z (git config --get remote.upstream.url)
and set -l repo "origin"
@ -10,7 +10,7 @@ function omf_update
if git pull --rebase $repo master >/dev/null ^&1
git stash apply >/dev/null ^&1
else
omf_util_sync "origin"
omf.util_sync "origin"
end
end
end
end

View File

@ -1,3 +1,3 @@
function omf_version
function omf.version
echo "Oh My Fish! $OMF_VERSION"
end

View File

@ -1,3 +0,0 @@
function omf_list_installed_themes
basename $OMF_PATH/themes/*
end

View File

@ -1,30 +1,30 @@
# SYNOPSIS
# Completions for Oh My Fish CLI
function __omf_is_single_opt
function __omf.is_single_opt
test (count (commandline -opc)) -le 1
end
function __omf_opt_is
function __omf.opt_is
set -l cmd (commandline -opc)
test (count $cmd) -gt 1; and contains -- $cmd[2] $argv
end
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 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 t theme" -a (printf "%s " (omf_list_themes))
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 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 -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 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 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 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 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 destroy -n "__omf_is_single_opt" -d "Remove Oh My Fish"
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 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 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 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 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 destroy -n "__omf.is_single_opt" -d "Remove Oh My Fish"

View File

@ -27,19 +27,19 @@ end
function omf -d "Oh My Fish"
if test (count $argv) -eq 0
omf_help; and return 0
omf.help; and return 0
end
switch $argv[1]
case "v" "ver" "version"
omf_version
omf.version
case "q" "query"
switch (count $argv)
case 1
omf_query_env
omf.query_env
case 2
omf_query_env "$argv[2]"
omf.query_env "$argv[2]"
case "*"
echo (omf::err)"Invalid number of arguments"(omf::off) 1^&2
echo "Usage: $_ "(omf::em)"$argv[1]"(omf::off)" [<variable name>]" 1^&2
@ -47,16 +47,16 @@ function omf -d "Oh My Fish"
end
case "h" "help"
omf_help
omf.help
case "l" "li" "lis" "lst" "list"
omf_list_local_packages | column
omf.list_local_packages | column
case "i" "install" "get"
if test (count $argv) -eq 1
omf_list_db_packages | column
omf.list_db_packages | column
else
omf_install_package $argv[2..-1]
omf.install_package $argv[2..-1]
refresh
end
@ -66,11 +66,11 @@ function omf -d "Oh My Fish"
set -l regex "[[:<:]]($theme)[[:>:]]"
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
else if test (count $argv) -eq 2
omf_theme $argv[2]
omf.theme $argv[2]
refresh
else
echo (omf::err)"Invalid number of arguments"(omf::off) 1^&2
@ -84,26 +84,26 @@ function omf -d "Oh My Fish"
echo "Usage: $_ "(omf::em)"$argv[1]"(omf::off)" <[package|theme] name>" 1^&2
return $OMF_INVALID_ARG
end
omf_remove_package $argv[2..-1]
omf.remove_package $argv[2..-1]
case "u" "up" "upd" "update"
pushd $OMF_PATH
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)
else
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)
end
omf_theme (cat $OMF_CONFIG/theme)
omf_install_package (omf_list_installed_packages)
omf.theme (cat $OMF_CONFIG/theme)
omf.install_package (omf.list_installed_packages)
popd
refresh
case "s" "su" "sub" "submit"
switch (count $argv)
case 3
omf_submit $argv[2] $argv[3]
omf.submit $argv[2] $argv[3]
case "*"
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
@ -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
return $OMF_MISSING_ARG
end
omf_new $argv[2..-1]
omf.new $argv[2..-1]
case "destroy"
omf_destroy
omf.destroy
case "*"
echo (omf::err)"$argv[1] option not recognized"(omf::off) 1^&2

View File

@ -1,4 +1,4 @@
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 \
-d "{\"user\":\"$user\"}" >/dev/null ^&1
end
end

View File

@ -1,4 +1,4 @@
function omf_util_mkdir -a name
function omf.util_mkdir -a name
set -l name "$argv[1]"
if test -d "$OMF_CONFIG"
set name "$OMF_CONFIG/$name"

View File

@ -1,8 +1,8 @@
function omf_util_sync -a remote
function omf.util_sync -a remote
set -l repo $remote
set -q argv[1]; and set repo $argv[1]
git fetch origin master
git reset --hard FETCH_HEAD
git clean -df
end
end

View File

@ -1,4 +1,4 @@
function omf_util_valid_package -a package
function omf.util_valid_package -a package
test (echo "$package" | tr "[:upper:]" "[:lower:]") = "omf"; and return 10
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}\*