From 2930c812281b0bc441dd987ed90a2d463f072250 Mon Sep 17 00:00:00 2001
From: Bruno Pinto <brunoferreirapinto@gmail.com>
Date: Sun, 31 May 2015 13:29:05 +0100
Subject: [PATCH] omf list function

---
 plugins/omf/omf.fish                  |  2 +
 plugins/omf/omf.helper.fish           |  1 +
 plugins/omf/omf.packages.fish         | 46 +++++++++++++--
 plugins/omf/omf.packages.install.fish | 56 +++++++++---------
 plugins/omf/omf.packages.update.fish  | 83 +++++++++++++--------------
 5 files changed, 110 insertions(+), 78 deletions(-)

diff --git a/plugins/omf/omf.fish b/plugins/omf/omf.fish
index 7eabbf4..a595417 100644
--- a/plugins/omf/omf.fish
+++ b/plugins/omf/omf.fish
@@ -11,6 +11,8 @@ function omf -d "Oh My Fish helper"
         omf.packages --install
       case 'update'
         omf.packages --update
+      case 'list' 'ls'
+        omf.packages --list
       case '*'
         omf.helper
     end
diff --git a/plugins/omf/omf.helper.fish b/plugins/omf/omf.helper.fish
index 5179dcc..5a6ccc5 100644
--- a/plugins/omf/omf.helper.fish
+++ b/plugins/omf/omf.helper.fish
@@ -11,5 +11,6 @@ function omf.helper -d 'Prints all functions supported by Oh My Fish helper'
   omf.log normal '  Examples:'
   omf.log normal '    omf install'
   omf.log normal '    omf update'
+  omf.log normal '    omf list'
 end
 
diff --git a/plugins/omf/omf.packages.fish b/plugins/omf/omf.packages.fish
index 3c08261..1150755 100644
--- a/plugins/omf/omf.packages.fish
+++ b/plugins/omf/omf.packages.fish
@@ -9,20 +9,54 @@
 #     Install all packages
 #   --update
 #     Update all packages
+#   --list
+#     List all active packages
 #
 # DESCRIPTION
 #   Manage all plugins and themes specified on the $fish_plugins
 #   and $fish_theme variables
 #
 function omf.packages --argument-names options -d 'Manage all plugins and themes'
-  set -l modified_packages 0
+  set -g __omf_packages_modified 0
 
   switch $options
-    case "--install"
-      omf.packages.install
-    case "--update"
-      omf.packages.update
-    case "*"
+    case '--install'
+      for plugin in $fish_plugins
+        omf.packages.install --plugin $plugin
+      end
+
+      omf.packages.install --theme $fish_theme
+
+      if [ $__omf_packages_modified -eq 0 ]
+        omf.log green 'All packages were already installed.'
+      end
+    case '--update'
+      for plugin in $fish_plugins
+        omf.packages.update --plugin $plugin
+      end
+
+      omf.packages.update --theme $fish_theme
+
+      if [ $__omf_packages_modified -eq 0 ]
+        omf.log green 'All packages were already updated'
+      end
+    case '--list'
+      omf.log yellow 'Plugins: '
+      omf.log normal $fish_plugins
+
+      omf.log normal ''
+      omf.log yellow 'Theme: '
+      omf.log normal $fish_theme
+    case '*'
       omf.log red 'Unknown option'
   end
 end
+
+function omf.packages.report.started -e omf_package_installing -e omf_package_updating
+  omf.log -n white "Installing $argv... "
+end
+
+function omf.packages.report.finished -e omf_package_installed -e omf_package_updated
+  omf.log green  "√"
+  set __omf_packages_modified (expr $__omf_packages_modified + 1)
+end
diff --git a/plugins/omf/omf.packages.install.fish b/plugins/omf/omf.packages.install.fish
index ac8dbe3..5a84792 100644
--- a/plugins/omf/omf.packages.install.fish
+++ b/plugins/omf/omf.packages.install.fish
@@ -1,36 +1,32 @@
 # NAME
-#   omf.packages.install - Install all plugins and themes
+#   omf.packages.install - Install a plugin or theme
+#
+# SYNOPSIS
+#   --plugin <plugin_name>
+#   --theme  <theme_name>
 #
 # DESCRIPTION
-#   Install all plugins and themes specified on the $fish_plugins
-#   and $fish_theme variables
+#   Install a plugin or theme
 #
-function omf.packages.install -d "Install all plugins and themes"
-  set -l installed_packages 0
-
-  # Plugins
-  for plugin in $fish_plugins
-    if [ -e $fish_path/plugins/$plugin -o -e $fish_custom/plugins/$plugin ]
-      #echo "$plugin is already installed. Skipping."
-    else
-      omf.log -n white "Installing $plugin... "
-      git clone "https://github.com/oh-my-fish/plugins-$plugin" $fish_path/plugins/$plugin ^ /dev/null
-      omf.log green  "√"
-      set -l installed_packages 1
-    end
-  end
-
-  # Theme
-  if [ -e $fish_path/themes/$fish_theme -o -e $fish_custom/themes/$fish_theme ]
-    #echo "$fish_theme is already installed. Skipping."
-  else
-    omf.log -n white "Installing $fish_theme... "
-    git clone "https://github.com/oh-my-fish/themes-$fish_theme" $fish_path/themes/$fish_theme ^ /dev/null
-    omf.log green  "√"
-    set -l installed_packages 1
-  end
-
-  if [ $installed_packages -eq 0 ]
-    omf.log green 'All plugins were already installed.'
+function omf.packages.install --argument-names type name -d "Install a plugin or theme"
+  switch $type
+    case '--plugin'
+      if [ -e $fish_path/plugins/$name -o -e $fish_custom/plugins/$name ]
+        #echo "$plugin is already installed. Skipping."
+      else
+        emit omf_package_installing $name
+        git clone "https://github.com/oh-my-fish/plugin-$name" $fish_path/plugins/$name ^ /dev/null
+        emit omf_package_installed $name
+      end
+    case '--theme'
+      if [ -e $fish_path/themes/$name -o -e $fish_custom/themes/$name ]
+        #echo "$fish_theme is already installed. Skipping."
+      else
+        emit omf_package_installing $name
+        git clone "https://github.com/oh-my-fish/theme-$name" $fish_path/themes/$name ^ /dev/null
+        emit omf_package_installed $name
+      end
+    case '*'
+      omf.log red 'Unknown option'
   end
 end
diff --git a/plugins/omf/omf.packages.update.fish b/plugins/omf/omf.packages.update.fish
index 7075834..995a50c 100644
--- a/plugins/omf/omf.packages.update.fish
+++ b/plugins/omf/omf.packages.update.fish
@@ -1,53 +1,52 @@
 # NAME
-#   omf.packages.update - Update all plugins and themes
+#   omf.packages.update - Update a plugin or theme
+#
+# SYNOPSIS
+#   --plugin <plugin_name>
+#   --theme  <theme_name>
 #
 # DESCRIPTION
-#   Update all plugins and themes specified on the $fish_plugins
-#   and $fish_theme variables
+#   Update a plugin or theme
 #
-function omf.packages.update -d "Update all plugins and themes"
-  set -l installed_packages 0
-  pushd
+function omf.packages.update --argument-names type name -d "Update a plugin or theme"
+  pushd # Save current dir
 
-  # Plugins
-  for plugin in $fish_plugins
-    if [ -e $fish_path/plugins/$plugin -a -e $fish_path/plugins/$plugin/.git ]
-      omf.log -n white "Updating $plugin... "
-      echo (cd $fish_path/plugins/$plugin; and git pull --rebase > /dev/null) >/dev/null
-      omf.log green  "√"
-      set -l installed_packages 1
-    else
-      if [ -e $fish_custom/plugins/$plugin -a -e $fish_custom/plugins/$plugin/.git ]
-        omf.log -n white "Updating $plugin... "
-        echo (cd $fish_custom/plugins/$plugin; and git pull --rebase > /dev/null) >/dev/null
+  switch $type
+    case '--plugin'
+      if [ -e $fish_path/plugins/$name -a -e $fish_path/plugins/$name/.git ]
+        omf.log -n white "Updating $name "
+        echo (cd $fish_path/plugins/$name; and git pull --rebase > /dev/null) >/dev/null
         omf.log green  "√"
-        set -l installed_packages 1
+        emit omf_package_updated
       else
-        #echo "$plugin is not installed or not a git repo. Skipping."
+        if [ -e $fish_custom/plugins/$name -a -e $fish_custom/plugins/$name/.git ]
+          omf.log -n white "Updating $name "
+          echo (cd $fish_custom/plugins/$name; and git pull --rebase > /dev/null) >/dev/null
+          omf.log green  "√"
+          emit omf_package_updated
+        else
+          #echo "Plugin is not installed or not a git repo. Skipping."
+        end
       end
-    end
-  end
-
-  # Theme
-  if [ -e $fish_path/themes/$fish_theme -a -e $fish_path/themes/$fish_theme/.git ]
-    omf.log -n white "Updating $fish_theme... "
-    echo (cd $fish_path/themes/$fish_theme; and git pull --rebase > /dev/null) >/dev/null
-    omf.log green  "√"
-    set -l installed_packages 1
-  else
-    if [ -e $fish_custom/themes/$fish_theme -a -e $fish_custom/themes/$fish_theme/.git ]
-      omf.log -n white "Updating $fish_theme... "
-      echo (cd $fish_custom/themes/$fish_theme; and git pull --rebase > /dev/null) >/dev/null
-      omf.log green  "√"
-      set -l installed_packages 1
-    else
-      #echo "$fish_theme is not installed or not a git repo. Skipping."
-    end
-  end
-
-  if [ $installed_packages -eq 0 ]
-    omf.log green 'No plugins to update.'
+    case '--theme'
+      if [ -e $fish_path/themes/$name -a -e $fish_path/themes/$name/.git ]
+        omf.log -n white "Updating $name "
+        echo (cd $fish_path/themes/$name; and git pull --rebase > /dev/null) >/dev/null
+        omf.log green  "√"
+        emit omf_package_updated
+      else
+        if [ -e $fish_custom/themes/$name -a -e $fish_custom/themes/$name/.git ]
+          omf.log -n white "Updating $name "
+          echo (cd $fish_custom/themes/$name; and git pull --rebase > /dev/null) >/dev/null
+          omf.log green  "√"
+          emit omf_package_updated
+        else
+          #echo "Theme is not installed or not a git repo. Skipping."
+        end
+      end
+    case '*'
+      omf.log red 'Unknown option'
   end
 
-  popd
+  popd # Restore current dir
 end
-- 
GitLab