Skip to content

Commit

Permalink
possibility to specify nullstr inside \copy and \safe commands
Browse files Browse the repository at this point in the history
  • Loading branch information
okbob committed May 30, 2021
1 parent 5c1403d commit afec693
Show file tree
Hide file tree
Showing 4 changed files with 71 additions and 21 deletions.
24 changes: 12 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -232,18 +232,18 @@ see http://okbob.blogspot.cz/2017/07/i-hope-so-every-who-uses-psql-uses-less.htm


## Backslash commands
| Command | Description |
|------------------------------------------------------------|----------------------------|
| `\[+]N` | go to line number |
| `\-N` | go to line number from end |
| `\N+` | go to N lines forward |
| `\N-` | go to N lines backward |
| `\theme N` | set theme number |
| `\copy [all|selected] [csv|tsv|insert|text]` | copy data to clipboard |
| `\save [all|selected] [csv|tsv|insert|text]` | copy data to clipboard |
| `\order [N|colum name]` | sort by colum |
| `\orderd [N|colum name]` | desc sort by column |
| `\search [back] [selected] [colum name] [string|"string"]` | search string in data |
| Command | Description |
|--------------------------------------------------------------|----------------------------|
| `\[+]N` | go to line number |
| `\-N` | go to line number from end |
| `\N+` | go to N lines forward |
| `\N-` | go to N lines backward |
| `\theme N` | set theme number |
| `\copy [all|selected] [nullstr "str"] [csv|tsv|insert|text]` | copy data to clipboard |
| `\save [all|selected] [nullstr "str"] [csv|tsv|insert|text]` | copy data to clipboard |
| `\order [N|colum name]` | sort by colum |
| `\orderd [N|colum name]` | desc sort by column |
| `\search [back] [selected] [colum name] [string|"string"]` | search string in data |

The output can be redirected to any command when the name starts with pipe symbol:

Expand Down
1 change: 0 additions & 1 deletion ToDo
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
ToDo
====


Known bugs
==========

Expand Down
9 changes: 8 additions & 1 deletion pspg.spec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

Summary: pspg: a unix pager optimized for psql
Name: pspg
Version: 4.6.3
Version: 5.0.0
Release: 0%{?dist}
License: BSD
Group: Development/Tools
Expand Down Expand Up @@ -43,6 +43,13 @@ CFLAGS="$RPM_OPT_FLAGS"
%{_bindir}/*

%changelog
* Sun May 30 2021 Pavel Stehule <[email protected]>
- new light PaperColor theme
- possibility to set nullstr used by export routines
- introduction interactive command line and backslash commands
- \save, \copy, \theme, \quit, \order, \orderd and \search commands
- possibility to push export output to some program by using pipe

* Sat Apr 17 2021 Pavel Stehule <[email protected]>
- --menu-always ensure active top bar menu all time
- modify theme 9
Expand Down
58 changes: 51 additions & 7 deletions src/pspg.c
Original file line number Diff line number Diff line change
Expand Up @@ -2766,6 +2766,7 @@ const char *export_opts[] = {
"text",
"insert",
"cinsert",
"nullstr",
NULL
};

Expand Down Expand Up @@ -3144,6 +3145,7 @@ typedef struct
ClipboardFormat format;
int rows;
double percent;
char *nullstr;
char *pipecmd;
} ExportedSpec;

Expand All @@ -3159,6 +3161,7 @@ parse_exported_spec(Options *opts,
bool format_specified = false;
bool range_is_specified_already = false;
bool range_specified = false;
bool null_is_specified_already = false;

bool next_token_shouldbe_number = false;
char *token;
Expand All @@ -3168,6 +3171,7 @@ parse_exported_spec(Options *opts,
spec->format = CLIPBOARD_FORMAT_TEXT;
spec->rows = 0;
spec->percent = 0.0;
spec->nullstr = NULL;
spec->pipecmd = NULL;

*is_valid = false;
Expand Down Expand Up @@ -3250,6 +3254,45 @@ parse_exported_spec(Options *opts,
spec->format = CLIPBOARD_FORMAT_INSERT_WITH_COMMENTS;
format_specified = true;
}
else if (IS_TOKEN(token, n, "null") ||
IS_TOKEN(token, n, "nullstr"))
{
char *ident;
int ident_len;

if (null_is_specified_already)
{
*next_event_keycode = show_info_wait(opts, scrdesc,
" Syntax error (null is specified already)",
NULL, NULL, true, false, true);
return NULL;
}

while (*instr == ' ')
instr += 1;

if (*instr != '"')
{
*next_event_keycode = show_info_wait(opts, scrdesc,
" Syntax error (expected '\"')",
NULL, NULL, true, false, true);
return NULL;
}

instr = get_identifier(instr, &ident, &ident_len);
if (!ident)
{
*next_event_keycode = show_info_wait(opts, scrdesc,
" Syntax error (expected closed quoted string)",
NULL, NULL, true, false, true);
return NULL;
}

ident = trim_quoted_str(ident, &ident_len, opts->force8bit);

if (ident_len > 0)
spec->nullstr = sstrndup(ident, ident_len);
}
else
{
char buffer[255];
Expand Down Expand Up @@ -7390,8 +7433,6 @@ main(int argc, char *argv[])
else
next_command = cmd_ForwardSearch;
}

break;
}
else if (IS_TOKEN(cmdline_ptr, n, "ord") ||
IS_TOKEN(cmdline_ptr, n, "order") ||
Expand Down Expand Up @@ -7449,8 +7490,6 @@ main(int argc, char *argv[])
next_event_keycode = show_info_wait(&opts, &scrdesc,
" Invalid identifier (expected column name)",
NULL, true, true, false, true);

break;
}
else if (IS_TOKEN(cmdline_ptr, n, "save"))
{
Expand All @@ -7470,6 +7509,8 @@ main(int argc, char *argv[])
memcpy(&loc_opts, &opts, sizeof(Options));

loc_opts.copy_target = COPY_TARGET_FILE;
loc_opts.nullstr = expspec.nullstr;
loc_opts.empty_string_is_null = !expspec.nullstr;

export_to_file(expspec.command,
expspec.format,
Expand All @@ -7481,8 +7522,8 @@ main(int argc, char *argv[])
expspec.pipecmd,
&refresh_clear);
}
else
break;

free(expspec.nullstr);
}
else if (IS_TOKEN(cmdline_ptr, n, "copy"))
{
Expand All @@ -7502,6 +7543,8 @@ main(int argc, char *argv[])
memcpy(&loc_opts, &opts, sizeof(Options));

loc_opts.copy_target = COPY_TARGET_CLIPBOARD;
loc_opts.nullstr = expspec.nullstr;
loc_opts.empty_string_is_null = !expspec.nullstr;

export_to_file(expspec.command,
expspec.format,
Expand All @@ -7513,7 +7556,8 @@ main(int argc, char *argv[])
expspec.pipecmd,
&refresh_clear);
}
break;

free(expspec.nullstr);
}
else
{
Expand Down

0 comments on commit afec693

Please sign in to comment.