Browse Source

klish: Padding for completion

Serj Kalichev 1 year ago
parent
commit
3df601db39
3 changed files with 21 additions and 31 deletions
  1. 5 31
      bin/klish/interactive.c
  2. 2 0
      tinyrl/tinyrl.h
  3. 14 0
      tinyrl/tinyrl/tinyrl.c

+ 5 - 31
bin/klish/interactive.c

@@ -184,42 +184,16 @@ static void display_completions(const tinyrl_t *tinyrl, faux_list_t *completions
 	iter = faux_list_head(completions);
 	while ((node = faux_list_each_node(&iter))) {
 		char *compl = (char *)faux_list_data(node);
-		printf("%s%s ", prefix ? prefix : "", compl);
+		tinyrl_printf(tinyrl, "%*s%s",
+			(prefix_len + max + 1 - strlen(compl)),
+			prefix ? prefix : "",
+			compl);
 		cols_filled++;
 		if ((cols_filled >= cols) || (node == faux_list_tail(completions))) {
 			cols_filled = 0;
-			printf("\n");
+			tinyrl_crlf(tinyrl);
 		}
 	}
-
-/*
-	// Find out column and rows number
-	if (max < width)
-		cols = (width + 1) / (max + 1); // For a space between words
-	else
-		cols = 1;
-	rows = len / cols + 1;
-
-	assert(matches);
-	if (matches) {
-		unsigned int r, c;
-		len--, matches++; // skip the subtitution string
-		// Print out a table of completions
-		for (r = 0; r < rows && len; r++) {
-			for (c = 0; c < cols && len; c++) {
-				const char *match = *matches++;
-				len--;
-				if ((c + 1) == cols) // Last str in row
-					tinyrl_vt100_printf(this->term, "%s",
-						match);
-				else
-					tinyrl_vt100_printf(this->term, "%-*s ",
-						max, match);
-			}
-			tinyrl_crlf(this);
-		}
-	}
-*/
 }
 
 

+ 2 - 0
tinyrl/tinyrl.h

@@ -98,6 +98,8 @@ void tinyrl_reset_line(tinyrl_t *tinyrl);
 void tinyrl_crlf(const tinyrl_t *tinyrl);
 void tinyrl_multi_crlf(const tinyrl_t *tinyrl);
 size_t tinyrl_width(const tinyrl_t *tinyrl);
+int tinyrl_printf(const tinyrl_t *tinyrl, const char *fmt, ...);
+
 
 bool_t tinyrl_line_insert(tinyrl_t *tinyrl, const char *text, size_t len);
 bool_t tinyrl_line_delete(tinyrl_t *tinyrl, off_t start, size_t len);

+ 14 - 0
tinyrl/tinyrl/tinyrl.c

@@ -727,3 +727,17 @@ size_t tinyrl_width(const tinyrl_t *tinyrl)
 
 	return tinyrl->width;
 }
+
+
+// Because terminal is in raw mode and standard libc printf() can don't flush()
+int tinyrl_printf(const tinyrl_t *tinyrl, const char *fmt, ...)
+{
+	va_list args;
+	int len = 0;
+
+	va_start(args, fmt);
+	len = vt100_vprintf(tinyrl->term, fmt, args);
+	va_end(args);
+
+	return len;
+}