Browse Source

Implementation of <cr> in help for completed commands

git-svn-id: https://klish.googlecode.com/svn/trunk@417 0eaa4687-2ee9-07dd-09d9-bcdd2d2dd5fb
Serj Kalichev 13 years ago
parent
commit
8c45b93dd4
3 changed files with 31 additions and 27 deletions
  1. 1 1
      clish/command.h
  2. 11 16
      clish/command/command.c
  3. 19 10
      clish/shell/shell_help.c

+ 1 - 1
clish/command.h

@@ -45,7 +45,7 @@ void clish_command_delete(clish_command_t * instance);
 void
 clish_command_insert_param(clish_command_t * instance, clish_param_t * param);
 int clish_command_help(const clish_command_t * instance, clish_help_t *help,
-	const char *viewid, const char *line);
+	const char *viewid, const char *line, size_t *max_width);
 void clish_command_dump(const clish_command_t * instance);
 
 /*-----------------

+ 11 - 16
clish/command/command.c

@@ -205,7 +205,7 @@ void clish_command_insert_param(clish_command_t * this, clish_param_t * param)
 
 /*--------------------------------------------------------- */
 int clish_command_help(const clish_command_t * this, clish_help_t *help,
-	const char * viewid, const char * line)
+	const char * viewid, const char * line, size_t *max_width)
 {
 	const char *name = clish_command__get_name(this);
 	unsigned index = lub_argv_wordcount(line);
@@ -214,14 +214,11 @@ int clish_command_help(const clish_command_t * this, clish_help_t *help,
 	clish_pargv_t *last, *pargv;
 	unsigned i;
 	unsigned cnt = 0;
-	unsigned longest = 0;
-	clish_pargv_status_t status;
+	clish_pargv_status_t status = CLISH_LINE_OK;
 
+	/* Empty line */
 	if (0 == index)
-		return 0;
-	/* Line has no any parameters */
-	if (strlen(line) <= strlen(name))
-		return 0;
+		return -1;
 
 	if (line[strlen(line) - 1] != ' ')
 		index--;
@@ -249,21 +246,19 @@ int clish_command_help(const clish_command_t * this, clish_help_t *help,
 			name = clish_ptype__get_text(clish_param__get_ptype(param));
 		if (name)
 			clen = strlen(name);
-		if (clen > longest)
-			longest = clen;
+		if (max_width && (clen > *max_width))
+			*max_width = clen;
 		clish_param_help(param, help);
 	}
 	clish_pargv_delete(last);
 	lub_argv_delete(argv);
 
-	/* Add <cr> if command is completed */
-	if (CLISH_LINE_OK == status) {
-		lub_argv_add(help->name, "<cr>");
-		lub_argv_add(help->help, NULL);
-		lub_argv_add(help->detail, NULL);
-	}
+	/* It's a completed command */
+	if (CLISH_LINE_OK == status)
+		return 0;
 
-	return longest;
+	/* Incompleted command */
+	return -1;
 }
 
 /*--------------------------------------------------------- */

+ 19 - 10
clish/shell/shell_help.c

@@ -13,27 +13,28 @@
 /*
  * Provide a detailed list of the possible command completions
  */
-static int available_commands(clish_shell_t * this,
-	clish_help_t *help, const char *line)
+static void available_commands(clish_shell_t * this,
+	clish_help_t *help, const char *line, size_t *max_width)
 {
-	size_t max_width = 0;
 	const clish_command_t *cmd;
 	clish_shell_iterator_t iter;
 
+	if (max_width)
+		*max_width = 0;
 	/* Search for COMMAND completions */
 	clish_shell_iterator_init(&iter, CLISH_NSPACE_HELP);
 	while ((cmd = clish_shell_find_next_completion(this, line, &iter))) {
 		size_t width;
 		const char *name = clish_command__get_suffix(cmd);
-		width = strlen(name);
-		if (width > max_width)
-			max_width = width;
+		if (max_width) {
+			width = strlen(name);
+			if (width > *max_width)
+				*max_width = width;
+		}
 		lub_argv_add(help->name, name);
 		lub_argv_add(help->help, clish_command__get_text(cmd));
 		lub_argv_add(help->detail, clish_command__get_detail(cmd));
 	}
-
-	return max_width;
 }
 
 /*--------------------------------------------------------- */
@@ -49,16 +50,24 @@ void clish_shell_help(clish_shell_t *this, const char *line)
 	help.detail = lub_argv_new(NULL, 0);
 
 	/* Get COMMAND completions */
-	max_width = available_commands(this, &help, line);
+	available_commands(this, &help, line, &max_width);
 
 	/* Resolve a command */
 	cmd = clish_shell_resolve_command(this, line);
 	/* Search for PARAM completion */
 	if (cmd) {
 		size_t width = 0;
-		width = clish_command_help(cmd, &help, this->viewid, line);
+		int status;
+		status = clish_command_help(cmd, &help, this->viewid,
+			line, &width);
 		if (width > max_width)
 			max_width = width;
+		/* Add <cr> if command is completed */
+		if (!status) {
+			lub_argv_add(help.name, "<cr>");
+			lub_argv_add(help.help, NULL);
+			lub_argv_add(help.detail, NULL);
+		}
 	}
 	if (lub_argv__get_count(help.name) == 0)
 		goto end;