Re: gvfs bash completion



Hi again,

Here's a gvfs version of tree(1), an extremely useful tool. Not with as
many options as it's POSIX brethren but it's a start. No patch this time
(a C file is attached instead) as SVN doesn't have local commits (we
should use git already!) so you need to add the obvious stuff to
Makefile.am and gvfs-bash-completion.sh manually.

    David

$ gvfs-tree sftp://hook.local/dev/bus/
sftp://hook.local/dev/bus
`-- usb
   |-- 001
   |  |-- 001
   |  |-- 003
   |  |-- 004
   |  `-- 006
   |-- 002
   |  |-- 001
   |  `-- 002
   |-- 003
   |  `-- 001
   `-- 004
      |-- 001
      `-- 002

/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */

/* GIO - GLib Input, Output and Streaming Library
 * 
 * Copyright (C) 2006-2007 Red Hat, Inc.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General
 * Public License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: David Zeuthen <davidz redhat com>
 */

#include <config.h>

#include <glib.h>
#include <locale.h>
#include <string.h>
#include <gio/gio.h>

static gboolean show_hidden = FALSE;

static GOptionEntry entries[] = 
{
	{ "hidden", 'h', 0, G_OPTION_ARG_NONE, &show_hidden, "Show hidden files", NULL },
};

static gint
sort_info_by_name (GFileInfo *a, GFileInfo *b)
{
  const char *na;
  const char *nb;

  na = g_file_info_get_name (a);
  nb = g_file_info_get_name (b);

  if (na == NULL)
    na = "";
  if (nb == NULL)
    nb = "";

  return strcmp (na, nb);
}

static void
do_tree (GFile *f, int level, guint64 pattern)
{
  GFileEnumerator *enumerator;
  GError *error = NULL;
  unsigned int n;

  enumerator = g_file_enumerate_children (f, 
                                          G_FILE_ATTRIBUTE_STANDARD_NAME ","
                                          G_FILE_ATTRIBUTE_STANDARD_TYPE ","
                                          G_FILE_ATTRIBUTE_STANDARD_IS_HIDDEN,
                                          0, 
                                          NULL, 
                                          &error);
  if (enumerator != NULL)
    {
      GList *l;
      GList *info_list;
      GFileInfo *info;

      info_list = NULL;
      while ((info = g_file_enumerator_next_file (enumerator, NULL, NULL)) != NULL)
        {
          if (g_file_info_get_is_hidden (info) && !show_hidden)
            {
              g_object_unref (info);
            }
          else
            {
              info_list = g_list_prepend (info_list, info);
            }
        }
      g_file_enumerator_close (enumerator, NULL, NULL);

      info_list = g_list_sort (info_list, (GCompareFunc) sort_info_by_name);

      for (l = info_list; l != NULL; l = l->next)
        {
          const char *name;
          GFileType type;
          gboolean is_last_item;

          info = l->data;
          is_last_item = (l->next == NULL);

          name = g_file_info_get_name (info);
          type = g_file_info_get_attribute_uint32 (info, G_FILE_ATTRIBUTE_STANDARD_TYPE);
          if (name != NULL)
            {

              for (n = 0; n < level; n++)
                {
                  if (pattern & (1<<n))
                    {
                      g_print ("|  ");
                    }
                  else
                    {
                      g_print ("   ");
                    }
                }

              if (is_last_item)
                {
                  g_print ("`-- %s\n", name);
                }
              else
                {
                  g_print ("|-- %s\n", name);
                }

              if (type & G_FILE_TYPE_DIRECTORY)
                {
                  guint64 new_pattern;
                  GFile *child;

                  if (is_last_item)
                    new_pattern = pattern;
                  else
                    new_pattern = pattern | (1<<level);

                  child = g_file_get_child (f, name);
                  do_tree (child, level + 1, new_pattern);
                  g_object_unref (child);
                }
            }
          g_object_unref (info);
        }
      g_list_free (info_list);
    }
  else
    {
      for (n = 0; n < level; n++)
        {
          if (pattern & (1<<n))
            {
              g_print ("|  ");
            }
          else
            {
              g_print ("   ");
            }
        }

      g_print ("    [%s]\n", error->message);

      g_error_free (error);
    }
}

static void
tree (GFile *f)
{
  char *uri;

  uri = g_file_get_uri (f);
  g_print ("%s\n", uri);
  g_free (uri);

  do_tree (f, 0, 0);
}

int
main (int argc, char *argv[])
{
  GError *error;
  GOptionContext *context;
  GFile *file;
  
  setlocale (LC_ALL, "");

  g_type_init ();
  
  error = NULL;
  context = g_option_context_new ("- list contents of directories in a tree-like format");
  g_option_context_add_main_entries (context, entries, GETTEXT_PACKAGE);
  g_option_context_parse (context, &argc, &argv, &error);
  g_option_context_free (context);

  if (argc > 1)
    {
      int i;
      
      for (i = 1; i < argc; i++) {
	file = g_file_new_for_commandline_arg (argv[i]);
	tree (file);
	g_object_unref (file);
      }
    }
  else
    {
      char *cwd;
      
      cwd = g_get_current_dir ();
      file = g_file_new_for_path (cwd);
      g_free (cwd);
      tree (file);
      g_object_unref (file);
    }

  return 0;
}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]