diff options
| author | Glenn L McGrath <bug1@ihug.co.nz> | 2003-01-06 01:11:50 +0000 |
|---|---|---|
| committer | Glenn L McGrath <bug1@ihug.co.nz> | 2003-01-06 01:11:50 +0000 |
| commit | 4d00129d0ff85a4e437212f2a6840eb932017890 (patch) | |
| tree | c8ba60b9937d0b9d347a8c77633194c7f45f4f67 /shell | |
| parent | 58c708af23d6ad855ea0cd1b61c2b87f26fc6988 (diff) | |
| download | busybox-w32-4d00129d0ff85a4e437212f2a6840eb932017890.tar.gz busybox-w32-4d00129d0ff85a4e437212f2a6840eb932017890.tar.bz2 busybox-w32-4d00129d0ff85a4e437212f2a6840eb932017890.zip | |
Correct column width for tab completion and ls
Diffstat (limited to 'shell')
| -rw-r--r-- | shell/cmdedit.c | 58 |
1 files changed, 39 insertions, 19 deletions
diff --git a/shell/cmdedit.c b/shell/cmdedit.c index da2b017e1..2ea61614d 100644 --- a/shell/cmdedit.c +++ b/shell/cmdedit.c | |||
| @@ -951,6 +951,44 @@ static int find_match(char *matchBuf, int *len_with_quotes) | |||
| 951 | return command_mode; | 951 | return command_mode; |
| 952 | } | 952 | } |
| 953 | 953 | ||
| 954 | /* | ||
| 955 | display by column original ideas from ls applet, | ||
| 956 | very optimize by my :) | ||
| 957 | */ | ||
| 958 | static void showfiles(char **matches, int nfiles) | ||
| 959 | { | ||
| 960 | int ncols, row; | ||
| 961 | int column_width = 0; | ||
| 962 | int nrows = nfiles; | ||
| 963 | |||
| 964 | /* find the longest file name- use that as the column width */ | ||
| 965 | for (row = 0; row < nrows; row++) { | ||
| 966 | int l = strlen(matches[row]); | ||
| 967 | |||
| 968 | if (column_width < l) | ||
| 969 | column_width = l; | ||
| 970 | } | ||
| 971 | column_width += 2; /* min space for columns */ | ||
| 972 | ncols = cmdedit_termw / column_width; | ||
| 973 | |||
| 974 | if (ncols > 1) { | ||
| 975 | nrows /= ncols; | ||
| 976 | if(nfiles % ncols) | ||
| 977 | nrows++; /* round up fractionals */ | ||
| 978 | column_width = -column_width; /* for printf("%-Ns", ...); */ | ||
| 979 | } else { | ||
| 980 | ncols = 1; | ||
| 981 | } | ||
| 982 | for (row = 0; row < nrows; row++) { | ||
| 983 | int n = row; | ||
| 984 | int nc; | ||
| 985 | |||
| 986 | for(nc = 1; nc < ncols && n+nrows < nfiles; n += nrows, nc++) | ||
| 987 | printf("%*s", column_width, matches[n]); | ||
| 988 | printf("%s\n", matches[n]); | ||
| 989 | } | ||
| 990 | } | ||
| 991 | |||
| 954 | 992 | ||
| 955 | static void input_tab(int *lastWasTab) | 993 | static void input_tab(int *lastWasTab) |
| 956 | { | 994 | { |
| @@ -1078,29 +1116,11 @@ static void input_tab(int *lastWasTab) | |||
| 1078 | * just hit TAB again, print a list of all the | 1116 | * just hit TAB again, print a list of all the |
| 1079 | * available choices... */ | 1117 | * available choices... */ |
| 1080 | if (matches && num_matches > 0) { | 1118 | if (matches && num_matches > 0) { |
| 1081 | int i, col, l; | ||
| 1082 | int sav_cursor = cursor; /* change goto_new_line() */ | 1119 | int sav_cursor = cursor; /* change goto_new_line() */ |
| 1083 | 1120 | ||
| 1084 | /* Go to the next line */ | 1121 | /* Go to the next line */ |
| 1085 | goto_new_line(); | 1122 | goto_new_line(); |
| 1086 | for (i = 0, col = 0; i < num_matches; i++) { | 1123 | showfiles(matches, num_matches); |
| 1087 | l = strlen(matches[i]); | ||
| 1088 | if (l < 14) | ||
| 1089 | l = 14; | ||
| 1090 | printf("%-14s ", matches[i]); | ||
| 1091 | col+=l; | ||
| 1092 | if ((l += 2) > 16) | ||
| 1093 | while (l % 16) { | ||
| 1094 | putchar(' '); | ||
| 1095 | l++; | ||
| 1096 | } | ||
| 1097 | if (col > (cmdedit_termw-l-l) && matches[i + 1] != NULL) { | ||
| 1098 | putchar('\n'); | ||
| 1099 | col = 0; | ||
| 1100 | } | ||
| 1101 | } | ||
| 1102 | /* Go to the next line and rewrite */ | ||
| 1103 | putchar('\n'); | ||
| 1104 | redraw(0, len - sav_cursor); | 1124 | redraw(0, len - sav_cursor); |
| 1105 | } | 1125 | } |
| 1106 | } | 1126 | } |
