Skip to contentSkip to conversation

lib/arg.c: dynamic newline in help

PATCH Carles Pina i Estany 1 participant 3 messages Dec 27, 2009

lore
Share & tools
lore
#0 lore raw

Hello,

Until now the options for the commands has been splitted (using a \n) by the developer.

For example, commands/hdparm.c:

static const struct grub_arg_option options[] = {
{"apm", 'B', 0, "Set Advanced Power Management\n"

                              "(1=low, ..., 254=high, 255=off).",
                              0, ARG_TYPE_INT},

This is a problem for the translations: translators should be aware that they need to cut the string after some number of characters, or will not appear correctly formatted.

Attached patch splits it dynamically.

Note: the attached patch goes forward and then backward because some strings, like above one, must have a \n to split some lines. But other strings could be just be too long and could not have the \n line.

The number of characters per line is fixed as a constant because lib/argc.c doesn't know anything about consoles. If I remember correctly is used in two
places: form help command and fstest. I think that the attached solution is good enough for the current status.

I implemented it using another approach and changed to the current one, so feel free to tell me any problem or things that you would improve. My limited tests has been fine.

I think that after comitting it I will be able to gettext all options strings.

Thanks,

Signature · 2 lines
Carles Pina i Estany
	http://pinux.info

=== modified file 'ChangeLog'

ChangeLog +5 -0
@@ -1,5 +1,10 @@
 2009-12-27  Carles Pina i Estany  <[email protected]>
 
+	* lib/arg.c (AVAILABLE_CHARS_LINE): New macro.
+	(grub_arg_show_help): Cut the help string dynamically.
+
+2009-12-27  Carles Pina i Estany  <[email protected]>
+
 	* normal/cmdline.c (grub_cmdline_get): Print a space after prompt.
 	* normal/main.c (grub_normal_read_line): Remove a space from the
 	default prompt.

=== modified file 'lib/arg.c'

arg.c +39 -1
@@ -28,6 +28,8 @@
 #define SHORT_ARG_HELP	-100
 #define SHORT_ARG_USAGE	-101
 
+#define AVAILABLE_CHARS_LINE (80 - 25)	
+
 static const struct grub_arg_option help_options[] =
   {
     {"help", SHORT_ARG_HELP, 0,
@@ -144,7 +146,41 @@ grub_arg_show_help (grub_extcmd_t cmd)
 		}
 	    }
 
-	  const char *doc = _(opt->doc);
+	  char *doc_orig = grub_strdup (_(opt->doc));
+	  char *doc = doc_orig;
+	  
+	  char* next_newline = doc;
+	  char* line;
+	  char* doc_last = doc + grub_strlen (doc);
+	  
+	  while (next_newline + AVAILABLE_CHARS_LINE < doc_last)
+	    {
+	      line = next_newline;
+
+	      /* Move forward.  */
+	      while (*next_newline != '\n' && 
+	             next_newline - line < AVAILABLE_CHARS_LINE &&
+		     *next_newline != '\0')
+	        {
+	          next_newline++;
+		}
+
+              /* Move backward.  */
+              while (*next_newline != '\n' &&
+	      	     *next_newline != ' ' && next_newline != line &&
+		     *next_newline != '\0')
+	        {
+		  next_newline--;
+		}
+
+		*next_newline = '\n';
+		if (next_newline == line)
+		  {
+		    break;
+		  }
+		next_newline++;
+	    }
+
 	  for (;;)
 	    {
 	      while (spacing-- > 0)
@@ -159,6 +195,8 @@ grub_arg_show_help (grub_extcmd_t cmd)
 	      doc++;
 	      spacing = 4 + 20;
 	    }
+	  
+	  grub_free (doc_orig);
 
 	  switch (opt->shortarg)
 	    {
#1 lore raw

Hi,

Quoted 2 lines · Carles Pina i Estany

Until now the options for the commands has been splitted (using a \n) by the developer.

if you want to test the patch, check the output of:
insmod acpi
help acpi

before and after the patch.

Before (at least with the standard terminal text): there are some lines that doesn't fit in one line and appears not very nice formatted (e.g. no-ebda).

With the patch appears like it should appear.

Signature · 2 lines
Carles Pina i Estany
	http://pinux.info
#2 lore raw

Hi,

Following a suggestion of Vladimir, now it uses GRUB_TERMINAL_WIDTH if it's not compiling in util way.

Signature · 2 lines
Carles Pina i Estany
	http://pinux.info

=== modified file 'ChangeLog'

ChangeLog +5 -0
@@ -1,5 +1,10 @@
 2009-12-27  Carles Pina i Estany  <[email protected]>
 
+	* lib/arg.c (AVAILABLE_CHARS_LINE): New macro.
+	(grub_arg_show_help): Cut the help string dynamically.
+
+2009-12-27  Carles Pina i Estany  <[email protected]>
+
 	* normal/cmdline.c (grub_cmdline_get): Print a space after prompt.
 	* normal/main.c (grub_normal_read_line): Remove a space from the
 	default prompt.

=== modified file 'lib/arg.c'

arg.c +44 -1

57 lines not on this page · raw message