Esempio n. 1
0
/*
 * Print the list of completions in a set of columns.
 */
static void
completion_print (gl_list_t l, size_t size)
{
  size_t i, j, col, max, numcols;

  max = calculate_max_length (l, size) + 5;
  numcols = (get_window_ewidth (cur_wp) - 1) / max;

  bprintf ("Possible completions are:\n");
  for (i = col = 0; i < MIN (size, gl_list_size (l)); i++)
    {
      char *s = (char *) gl_list_get_at (l, i);
      size_t len = strlen (s);
      if (col >= numcols)
        {
          col = 0;
          insert_newline ();
        }
      insert_nstring (s, len);
      for (j = max - len; j > 0; --j)
        insert_char_in_insert_mode (' ');
      ++col;
    }
}
static void
print_help (GOptionContext *context,
	    gboolean        main_help,
	    GOptionGroup   *group) 
{
  GList *list;
  gint max_length, len;
  gint i;
  GOptionEntry *entry;
  GHashTable *shadow_map;
  gboolean seen[256];
  const gchar *rest_description;
  
  rest_description = NULL;
  if (context->main_group)
    {
      for (i = 0; i < context->main_group->n_entries; i++)
	{
	  entry = &context->main_group->entries[i];
	  if (entry->long_name[0] == 0)
	    {
	      rest_description = entry->arg_description;
	      break;
	    }
	}
    }
  
  g_print ("%s\n  %s %s%s%s%s%s\n\n", 
	   _("Usage:"), g_get_prgname(), _("[OPTION...]"),
	   rest_description ? " " : "",
	   rest_description ? rest_description : "",
	   context->parameter_string ? " " : "",
	   context->parameter_string ? context->parameter_string : "");

  memset (seen, 0, sizeof (gboolean) * 256);
  shadow_map = g_hash_table_new (g_str_hash, g_str_equal);

  if (context->main_group)
    {
      for (i = 0; i < context->main_group->n_entries; i++)
	{
	  entry = &context->main_group->entries[i];
	  g_hash_table_insert (shadow_map, 
			       (gpointer)entry->long_name, 
			       entry);
	  
	  if (seen[(guchar)entry->short_name])
	    entry->short_name = 0;
	  else
	    seen[(guchar)entry->short_name] = TRUE;
	}
    }

  list = context->groups;
  while (list != NULL)
    {
      GOptionGroup *group = list->data;
      for (i = 0; i < group->n_entries; i++)
	{
	  entry = &group->entries[i];
	  if (g_hash_table_lookup (shadow_map, entry->long_name) && 
	      !(entry->flags && G_OPTION_FLAG_NOALIAS))
	    entry->long_name = g_strdup_printf ("%s-%s", group->name, entry->long_name);
	  else  
	    g_hash_table_insert (shadow_map, (gpointer)entry->long_name, entry);

	  if (seen[(guchar)entry->short_name] && 
	      !(entry->flags && G_OPTION_FLAG_NOALIAS))
	    entry->short_name = 0;
	  else
	    seen[(guchar)entry->short_name] = TRUE;
	}
      list = list->next;
    }

  g_hash_table_destroy (shadow_map);

  list = context->groups;

  max_length = g_utf8_strlen ("-?, --help", -1);

  if (list)
    {
      len = g_utf8_strlen ("--help-all", -1);
      max_length = MAX (max_length, len);
    }

  if (context->main_group)
    {
      len = calculate_max_length (context->main_group);
      max_length = MAX (max_length, len);
    }

  while (list != NULL)
    {
      GOptionGroup *group = list->data;
      
      /* First, we check the --help-<groupname> options */
      len = g_utf8_strlen ("--help-", -1) + g_utf8_strlen (group->name, -1);
      max_length = MAX (max_length, len);

      /* Then we go through the entries */
      len = calculate_max_length (group);
      max_length = MAX (max_length, len);
      
      list = list->next;
    }

  /* Add a bit of padding */
  max_length += 4;

  if (!group)
    {
      list = context->groups;
      
      g_print ("%s\n  -%c, --%-*s %s\n", 
	       _("Help Options:"), '?', max_length - 4, "help", 
	       _("Show help options"));
      
      /* We only want --help-all when there are groups */
      if (list)
	g_print ("  --%-*s %s\n", max_length, "help-all", 
		 _("Show all help options"));
      
      while (list)
	{
	  GOptionGroup *group = list->data;
	  
	  g_print ("  --help-%-*s %s\n", max_length - 5, group->name, 
		   TRANSLATE (group, group->help_description));
	  
	  list = list->next;
	}

      g_print ("\n");
    }

  if (group)
    {
      /* Print a certain group */
      
      g_print ("%s\n", TRANSLATE (group, group->description));
      for (i = 0; i < group->n_entries; i++)
	print_entry (group, max_length, &group->entries[i]);
      g_print ("\n");
    }
  else if (!main_help)
    {
      /* Print all groups */

      list = context->groups;

      while (list)
	{
	  GOptionGroup *group = list->data;

	  g_print ("%s\n", group->description);

	  for (i = 0; i < group->n_entries; i++)
	    if (!(group->entries[i].flags & G_OPTION_FLAG_IN_MAIN))
	      print_entry (group, max_length, &group->entries[i]);
	  
	  g_print ("\n");
	  list = list->next;
	}
    }
  
  /* Print application options if --help or --help-all has been specified */
  if (main_help || !group)
    {
      list = context->groups;

      g_print ("%s\n", _("Application Options:"));

      if (context->main_group)
	for (i = 0; i < context->main_group->n_entries; i++) 
	  print_entry (context->main_group, max_length, 
		       &context->main_group->entries[i]);

      while (list != NULL)
	{
	  GOptionGroup *group = list->data;

	  /* Print main entries from other groups */
	  for (i = 0; i < group->n_entries; i++)
	    if (group->entries[i].flags & G_OPTION_FLAG_IN_MAIN)
	      print_entry (group, max_length, &group->entries[i]);
	  
	  list = list->next;
	}

      g_print ("\n");
    }
  
  exit (0);
}