Browse Source

klish: Display completions

Serj Kalichev 1 year ago
parent
commit
f3101d181c
3 changed files with 33 additions and 6 deletions
  1. 22 6
      bin/klish/interactive.c
  2. 1 0
      tinyrl/tinyrl.h
  3. 10 0
      tinyrl/tinyrl/tinyrl.c

+ 22 - 6
bin/klish/interactive.c

@@ -165,17 +165,33 @@ static bool_t tinyrl_key_tab(tinyrl_t *tinyrl, unsigned char key)
 static void display_completions(const tinyrl_t *tinyrl, faux_list_t *completions,
 	const char *prefix, size_t max)
 {
-//	size_t width = tinyrl_width(tinyrl);
+	size_t width = tinyrl_width(tinyrl);
 	size_t cols = 0;
 	faux_list_node_t *iter = NULL;
-	char *compl = NULL;
+	faux_list_node_t *node = NULL;
+	size_t prefix_len = 0;
+	size_t cols_filled = 0;
+
+	if (prefix)
+		prefix_len = strlen(prefix);
+
+	// Find out column and rows number
+	if (max < width)
+		cols = (width + 1) / (prefix_len + max + 1); // For a space between words
+	else
+		cols = 1;
 
-/*
 	iter = faux_list_head(completions);
-	while ((compl = (char *)faux_list_each(&iter))) {
-		printf("%s%s\n", prefix ? prefix : "", compl);
+	while ((node = faux_list_each_node(&iter))) {
+		char *compl = (char *)faux_list_data(node);
+		printf("%s%s ", prefix ? prefix : "", compl);
+		cols_filled++;
+		if ((cols_filled >= cols) || (node == faux_list_tail(completions))) {
+			cols_filled = 0;
+			printf("\n");
+		}
 	}
-*/
+
 /*
 	// Find out column and rows number
 	if (max < width)

+ 1 - 0
tinyrl/tinyrl.h

@@ -97,6 +97,7 @@ void tinyrl_reset_line_state(tinyrl_t *tinyrl);
 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);
 
 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);

+ 10 - 0
tinyrl/tinyrl/tinyrl.c

@@ -717,3 +717,13 @@ void tinyrl_reset_hist_pos(tinyrl_t *tinyrl)
 {
 	hist_pos_reset(tinyrl->hist);
 }
+
+
+size_t tinyrl_width(const tinyrl_t *tinyrl)
+{
+	assert(tinyrl);
+	if (!tinyrl)
+		return 80;
+
+	return tinyrl->width;
+}