Re: gvfs bash completion



Here's an updated version that also prints the symlink target.

     David

$ gvfs-tree sftp://hook.local/dev/disk/
sftp://hook.local/dev/disk
|-- by-id
|  |-- ata-WDC_WD1500ADFD-00NLR1_WD-WMAP41179943 -> ../../sda
|  |-- ata-WDC_WD1500ADFD-00NLR1_WD-WMAP41179943-part1 -> ../../sda1
|  |-- ata-WDC_WD1500ADFD-00NLR1_WD-WMAP41179943-part2 -> ../../sda2
|  |-- ata-WDC_WD1500ADFD-00NLR1_WD-WMAP41179943-part3 -> ../../sda3
|  |-- ata-WDC_WD5000AAJS-00TKA0_WD-WCAPW0295169 -> ../../sdb
|  |-- ata-WDC_WD5000AAJS-00TKA0_WD-WCAPW0594845 -> ../../sdi
|  |-- ata-WDC_WD5000AAKS-00TMA0_WD-WCAPW0493929 -> ../../sdk
|  |-- ata-WDC_WD5000KS-00MNB0_WD-WCANU1914880 -> ../../sdh
|  |-- ata-WDC_WD5000KS-00MNB0_WD-WCANU2209762 -> ../../sdj
|  |-- md-uuid-c5fc394c:f33279ab:7630b1ba:099a5109 -> ../../md127
|  |-- scsi-SATA_WDC_WD1500ADFD-_WD-WMAP41179943 -> ../../sda
|  |-- scsi-SATA_WDC_WD1500ADFD-_WD-WMAP41179943-part1 -> ../../sda1
|  |-- scsi-SATA_WDC_WD1500ADFD-_WD-WMAP41179943-part2 -> ../../sda2
|  |-- scsi-SATA_WDC_WD1500ADFD-_WD-WMAP41179943-part3 -> ../../sda3
|  |-- scsi-SATA_WDC_WD5000AAJS-_WD-WCAPW0295169 -> ../../sdb
|  |-- scsi-SATA_WDC_WD5000AAJS-_WD-WCAPW0594845 -> ../../sdi
|  |-- scsi-SATA_WDC_WD5000AAKS-_WD-WCAPW0493929 -> ../../sdk
|  |-- scsi-SATA_WDC_WD5000KS-00_WD-WCANU1914880 -> ../../sdh
|  |-- scsi-SATA_WDC_WD5000KS-00_WD-WCANU2209762 -> ../../sdj
|  |-- usb-Generic_USB_CF_Reader_9205291-0:1 -> ../../sde
|  |-- usb-Generic_USB_MS_Reader_9205291-0:3 -> ../../sdg
|  |-- usb-Generic_USB_SD_Reader_9205291-0:0 -> ../../sdd
|  |-- usb-Generic_USB_SM_Reader_9205291-0:2 -> ../../sdf
|  |-- usb-WD_5000KS_External_5743414E5532313735323530-0:0 -> ../../sdc
|  `-- usb-WD_5000KS_External_5743414E5532313735323530-0:0-part1 -> ../../sdc1
|-- by-label
|  |-- Fusion500P -> ../../md127
|  |-- SWAP-sda2 -> ../../sda2
|  |-- USB500G -> ../../sdc1
|  |-- \x2f12 -> ../../sda1
|  `-- \x2fhome -> ../../sda3
|-- by-path
|  |-- pci-0000:00:02.5-scsi-1:0:0:0 -> ../../sr0
|  |-- pci-0000:00:02.5-scsi-1:0:1:0 -> ../../sr1
|  |-- pci-0000:00:03.0-usb-0:3:1.0-scsi-0:0:0:0 -> ../../sdd
|  |-- pci-0000:00:03.0-usb-0:3:1.0-scsi-0:0:0:1 -> ../../sde
|  |-- pci-0000:00:03.0-usb-0:3:1.0-scsi-0:0:0:2 -> ../../sdf
|  |-- pci-0000:00:03.0-usb-0:3:1.0-scsi-0:0:0:3 -> ../../sdg
|  |-- pci-0000:00:03.3-usb-0:5:1.0-scsi-0:0:0:0 -> ../../sdc
|  |-- pci-0000:00:03.3-usb-0:5:1.0-scsi-0:0:0:0-part1 -> ../../sdc1
|  |-- pci-0000:00:05.0-scsi-0:0:0:0 -> ../../sda
|  |-- pci-0000:00:05.0-scsi-0:0:0:0-part1 -> ../../sda1
|  |-- pci-0000:00:05.0-scsi-0:0:0:0-part2 -> ../../sda2
|  |-- pci-0000:00:05.0-scsi-0:0:0:0-part3 -> ../../sda3
|  |-- pci-0000:00:05.0-scsi-1:0:0:0 -> ../../sdb
|  |-- pci-0000:00:09.0-scsi-0:0:0:0 -> ../../sdh
|  |-- pci-0000:00:09.0-scsi-1:0:0:0 -> ../../sdi
|  |-- pci-0000:00:09.0-scsi-2:0:0:0 -> ../../sdj
|  `-- pci-0000:00:09.0-scsi-3:0:0:0 -> ../../sdk
`-- by-uuid
   |-- 689daf1e-7a07-45a7-b069-c2ab2c21d24c -> ../../sdc1
   |-- 7d999c1b-6ba9-4fb7-a6e9-0c9627dd5458 -> ../../sda3
   |-- d25c1d61-5faa-47f7-b8fb-e49f8b95d263 -> ../../sda1
   `-- e917ca55-e0bc-431e-85ad-1f5afccf93cc -> ../../md127

/* -*- 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 ","
                                          G_FILE_ATTRIBUTE_STANDARD_IS_SYMLINK ","
                                          G_FILE_ATTRIBUTE_STANDARD_SYMLINK_TARGET,
                                          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", name);
                }
              else
                {
                  g_print ("|-- %s", name);
                }
              if (g_file_info_get_is_symlink (info))
                {
                  const char *target;
                  target = g_file_info_get_symlink_target (info);
                  g_print (" -> %s", target);
                }
              g_print ("\n");

              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]