summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorpgen <p.gen.progs@gmail.com>2016-11-24 22:44:51 +0100
committerpgen <p.gen.progs@gmail.com>2016-11-24 23:59:34 +0100
commitbc7f8d6f9a9a22efd293759edf3d5171196693ad (patch)
tree479839230b602a006a005af65ae8797f888355aa
parentcb006dcc8d4ec13aafecf1a06ffb1ca0397016ba (diff)
Add a -k option
When in column or tabular mode, the default was to keep the trailing spaces in the selected word. This commit change that and the default action is now to remove them. The -k option is now available to tell smenu to restore the old behaviour and to keep them.
-rw-r--r--smenu.18
-rw-r--r--smenu.c39
2 files changed, 40 insertions, 7 deletions
diff --git a/smenu.1 b/smenu.1
index 3afc26f..37e363f 100644
--- a/smenu.1
+++ b/smenu.1
@@ -5,8 +5,8 @@ and outputs the selection to stdout.
.SH SYNOPSIS
.nf
\f(CRsmenu [\fB-h\fP] [\fB-n\fP \fIlines\fP] [\fB-t\fP [\fIcols\fP]] \\
- [\fB-s\fP \fIpattern\fP] [\fB-m\fP \fImessage\fP] [\fB-w\fP] [\fB-d\fP] \
-[\fB-M\fP] [\fB-c\fP] [\fB-l\fP] [\fB-r\fP] [\fB-b\fP] \\
+ [\fB-k\fP] [\fB-s\fP \fIpattern\fP] [\fB-m\fP \fImessage\fP] [\fB-w\fP] \\
+ [\fB-d\fP] [\fB-M\fP] [\fB-c\fP] [\fB-l\fP] [\fB-r\fP] [\fB-b\fP] \\
[\fB-i\fP \fIregex\fP] [\fB-e\fP \fIregex\fP] \\
[\fB-C\fP \
[\fIa\fP|\fIA\fP|\fIs\fP|\fIS\fP|\fIr\fP|\fIR\fP|\fId\fP|\fID\fP]\
@@ -240,6 +240,10 @@ and \fB-Z\fP can nevertheless be used to force words to appear in the first
.RS
Note that if this mode each column has the same width.
.RE
+.IP \fB-k\fP
+in column or tabulate mode, the selected word may be smaller than the
+column width. By default these trailing spaces will be removed from the
+output string. This option force them to be kept.
.IP "\fB-s\fP \fIpattern\fP"
Pre-Position the cursor to the first word matching the specified pattern.
diff --git a/smenu.c b/smenu.c
index cc0c5db..d3574f7 100644
--- a/smenu.c
+++ b/smenu.c
@@ -97,6 +97,7 @@ static void ll_init(ll_t * list);
static ll_node_t *ll_new_node(void);
static ll_t *ll_new(void);
+static void rtrim(char *str, const char *trim);
static int my_stricmp(const char *str1, const char *str2);
static int isprint7(int i);
@@ -262,6 +263,8 @@ struct toggle_s
int no_scrollbar; /* 1 to disable the scrollbar display else 0 */
int blank_nonprintable; /* 1 to try to display non-blanks in *
* symbolic form else 0 */
+ int keep_spaces; /* 1 to keep the trailing spaces in columne *
+ * and tabulate mode. */
};
/* Structure to store the default or imposed smenu limits */
@@ -446,8 +449,9 @@ short_usage(void)
{
fprintf(stderr, "Usage: smenu [-h] [-n lines] [-c] [-l] [-s pattern] ");
fprintf(stderr, "[-m message] [-w] [-d] \\\n");
- fprintf(stderr, " [-M] [-t [cols]] [-r] [-b] [-i regex] [-e regex]");
- fprintf(stderr, " \\\n");
+ fprintf(stderr,
+ " [-M] [-t [cols]] [-k] [-r] [-b] [-i regex] [-e regex]");
+ fprintf(stderr, " \\\n");
fprintf(stderr, " [-C [a|A|s|S|r|R|d|D]col1[-col2],[col1[-col2]]...]");
fprintf(stderr, " \\\n");
fprintf(stderr, " [-R [a|A|s|S|r|R|d|D]row1[-row2],[row1[-row2]]...] ");
@@ -480,6 +484,9 @@ usage(void)
fprintf(stderr, "-t tabulates the items. The number of columns can be "
"limited with\n");
fprintf(stderr, " an optional number.\n");
+ fprintf(stderr, "-k do not trim the trailing space from the output string "
+ "when in column and\n");
+ fprintf(stderr, " tabulate mode.\n");
fprintf(stderr, "-s sets the initial cursor position (read the manual for "
"more details).\n");
fprintf(stderr, "-m displays a one-line message above the window\n");
@@ -1642,6 +1649,17 @@ get_cursor_position(int *const r, int *const c)
/* Strings and multibyte strings utility functions */
/* *********************************************** */
+/* ======================== */
+/* trim trailing characters */
+/* ======================== */
+static void
+rtrim(char *str, const char *trim_str)
+{
+ size_t len = strlen(str);
+ while (len > 0 && strchr(trim_str, str[len - 1]))
+ str[--len] = '\0';
+}
+
/* ========================================= */
/* Case insensitive strcmp */
/* from http://c.snippets.org/code/stricmp.c */
@@ -3238,8 +3256,6 @@ build_metadata(word_t * word_a, term_t * term, int count, win_t * win)
size_t word_len;
int len = 0;
int last = 0;
- int cur_line;
- int end_line;
int word_width;
int tab_count; /* Current number of words in the line, *
* used in tab_mode */
@@ -4460,6 +4476,7 @@ main(int argc, char *argv[])
toggle.enter_val_in_search = 0;
toggle.no_scrollbar = 0;
toggle.blank_nonprintable = 0;
+ toggle.keep_spaces = 0;
/* Columns selection variables */
/* """"""""""""""""""""""""""" */
@@ -4520,7 +4537,7 @@ main(int argc, char *argv[])
/* """"""""""""""""""""""""""""" */
while ((opt = egetopt(argc, argv,
"VhqdMbi:e:S:I:E:A:Z:1:2:3:4:5:C:R:"
- "clwrgn:t%m:s:W:L:1:2:3:4:")) != -1)
+ "kclwrgn:t%m:s:W:L:1:2:3:4:")) != -1)
{
switch (opt)
{
@@ -4558,6 +4575,10 @@ main(int argc, char *argv[])
win.line_mode = 0;
break;
+ case 'k':
+ toggle.keep_spaces = 1;
+ break;
+
case 'c':
win.tab_mode = 0;
win.col_mode = 1;
@@ -6369,6 +6390,14 @@ main(int argc, char *argv[])
else
output_str = word_a[current].str;
+ /* Trim the trailing spaces if -k is given in tabular or */
+ /* column mode. Leading spaces are alwaye preserved because I */
+ /* consider their presence intentional as the only way to have */
+ /* them is to use quotes in the command line. */
+ /* """"""""""""""""""""""""""""""""""""""""""""""""""""""""""" */
+ if (!toggle.keep_spaces || (!win.tab_mode && !win.col_mode))
+ rtrim(output_str, " ");
+
/* And print it. */
/* """"""""""""" */
fprintf(old_stdout, "%s", output_str);