Skip to content

Commit

Permalink
possibility to select columns by ctrl-mouse over column headers
Browse files Browse the repository at this point in the history
  • Loading branch information
okbob committed Feb 25, 2021
1 parent 23dac8d commit fefdf44
Show file tree
Hide file tree
Showing 5 changed files with 177 additions and 55 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,13 @@ This pager can be used from the following clients command line clients too:
- `monetdb`
- [`Trino (formerly Presto SQL)`](https://trino.io/)


## Main target
* possibility to freeze first few rows, first few columns
* possibility to sort data by specified numeric column
* possibility to use fancy colors - like `mcview` or `FoxPro` - http://okbob.blogspot.com/2019/12/pspg-themes-what-you-use-it.html
* mouse is supported and used
* possibility to copy selected range to clipboard


## Screenshots
Expand Down Expand Up @@ -83,6 +86,7 @@ This pager can be used from the following clients command line clients too:

Options can be passed inside env variable `PSPG` too.


## Themes
0. black & white
1. Midnight Commander like
Expand Down Expand Up @@ -159,7 +163,7 @@ see http://okbob.blogspot.cz/2017/07/i-hope-so-every-who-uses-psql-uses-less.htm
* <kbd>shift</kbd>+<kbd>cursor...</kbd> - define range
* <kbd>F3</kbd> - start/finish of selection rows
* <kbd>shift</kbd>+<kbd>F3</kbd> - start/finish of selection block
* <kbd>Ctrl</kbd> + <kbd>drag mouse</kbd> - define range
* <kbd>Ctrl</kbd> + <kbd>drag mouse</kbd> - defines rows selection, on column header defines column selection


## Ending
Expand Down
18 changes: 12 additions & 6 deletions src/export.c
Original file line number Diff line number Diff line change
Expand Up @@ -680,6 +680,7 @@ export_data(Options *opts,
bool print_border = true;
bool print_header_line = true;
bool save_column_names = false;
bool has_selection;

int min_row = desc->first_data_row;
int max_row = desc->last_row;
Expand All @@ -700,6 +701,10 @@ export_data(Options *opts,

current_state->errstr = NULL;

has_selection =
((scrdesc->selected_first_row != -1 && scrdesc->selected_rows > 0 ) ||
(scrdesc->selected_first_column != -1 && scrdesc->selected_columns > 0));

if (cmd == cmd_CopyLineExtended && !DSV_FORMAT_TYPE(format))
format = CLIPBOARD_FORMAT_CSV;

Expand All @@ -720,7 +725,7 @@ export_data(Options *opts,

if (cmd == cmd_CopyLine ||
cmd == cmd_CopyLineExtended ||
(cmd == cmd_Copy && !opts->no_cursor))
(cmd == cmd_Copy && !opts->no_cursor && !has_selection))
{
min_row = max_row = cursor_row + desc->first_data_row;
print_footer = false;
Expand Down Expand Up @@ -771,13 +776,14 @@ export_data(Options *opts,
if (cmd == cmd_CopyMarkedLines || cmd == cmd_CopySearchedLines)
print_footer = false;

if ((cmd == cmd_Copy &&
((scrdesc->selected_first_row != -1 && scrdesc->selected_rows > 0 ) ||
(scrdesc->selected_first_column != -1 && scrdesc->selected_columns > 0))) ||
if ((cmd == cmd_Copy && has_selection) ||
cmd == cmd_CopySelected)
{
min_row = scrdesc->selected_first_row + desc->first_data_row;
max_row = min_row + scrdesc->selected_rows - 1;
if (scrdesc->selected_first_row != -1)
{
min_row = scrdesc->selected_first_row + desc->first_data_row;
max_row = min_row + scrdesc->selected_rows - 1;
}

if (scrdesc->selected_first_column != -1 && scrdesc->selected_columns > 0)
{
Expand Down
53 changes: 50 additions & 3 deletions src/print.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,8 @@ print_column_names(WINDOW *win,
int srcx, /* offset to displayed data */
int vcursor_xmin, /* xmin in display coordinates */
int vcursor_xmax, /* xmax in display coordinates */
int selected_xmin,
int selected_xmax,
DataDesc *desc,
Options *opts,
Theme *t)
Expand Down Expand Up @@ -239,11 +241,24 @@ print_column_names(WINDOW *win,
/* position starts from zero again to be comparable with maxx */
pos -= srcx;

if (selected_xmin != -1)
{
selected_xmin -= srcx;
selected_xmax -= srcx;

if (selected_xmin < 0)
selected_xmin = 0;
}

/* for each visible column (position) and defined colum */
while (pos < maxx && headline_ptr < headline_end_ptr)
{
char column_format = *headline_ptr;
bool is_cursor = vcursor_xmin <= pos && pos <= vcursor_xmax;
bool is_in_range = false;

is_in_range = selected_xmin != -1 && pos != -1 &&
pos >= selected_xmin && pos <= selected_xmax;

if (!force8bit)
{
Expand All @@ -256,7 +271,9 @@ print_column_names(WINDOW *win,
chars = 1;
}

if (is_cursor )
if (is_in_range)
new_attr = is_cursor ? t->selection_cursor_attr : t->selection_attr;
else if (is_cursor)
new_attr = column_format == 'd' ? t->cursor_data_attr : t->cursor_line_attr;
else
new_attr = column_format == 'd' ? t->data_attr : t->line_attr;
Expand Down Expand Up @@ -304,6 +321,9 @@ print_column_names(WINDOW *win,
int act_xmin, act_xmax, act_width;
int overlap_left = 0, overlap_right = 0;

bool is_cursor;
bool is_in_range;

get_column_data_dim(desc, i, &data_x, &data_width);

if (data_x + data_width < srcx)
Expand Down Expand Up @@ -332,7 +352,14 @@ print_column_names(WINDOW *win,

act_width = act_xmax - act_xmin + 1;

new_attr = (vcursor_xmin <= act_xmin && act_xmin <= vcursor_xmax) ? t->cursor_data_attr : t->data_attr;
is_cursor = vcursor_xmin <= act_xmin && act_xmin <= vcursor_xmax;
is_in_range = selected_xmin != -1 &&
act_xmin >= selected_xmin && act_xmin <= selected_xmax;

if (is_in_range)
new_attr = is_cursor ? t->selection_cursor_attr : t->selection_attr;
else
new_attr = is_cursor ? t->cursor_data_attr : t->data_attr;

if (colname_width <= act_width)
{
Expand Down Expand Up @@ -600,7 +627,9 @@ window_fill(int window_identifier,
bool is_fix_rows_only = window_identifier == WINDOW_FIX_ROWS;
bool is_scrollbar = window_identifier == WINDOW_VSCROLLBAR;
bool is_selectable = window_identifier == WINDOW_ROWS ||
window_identifier == WINDOW_LUC ||
window_identifier == WINDOW_FIX_COLS ||
window_identifier == WINDOW_FIX_ROWS ||
window_identifier == WINDOW_FOOTER;

win = scrdesc->wins[window_identifier];
Expand Down Expand Up @@ -811,7 +840,20 @@ window_fill(int window_identifier,
*/
if (is_fix_rows_only && rowstr == desc->namesline )
{
print_column_names(win, srcx, vcursor_xmin, vcursor_xmax, desc, opts, t);
int loc_selected_xmin = -1;
int loc_selected_xmax = -1;

/* mark columns names only when columns are selected */
if (selected_xmin != -1 && scrdesc->selected_first_row == -1)
{
loc_selected_xmin = selected_xmin;
loc_selected_xmax = selected_xmax;
}

print_column_names(win, srcx,
vcursor_xmin, vcursor_xmax,
loc_selected_xmin, loc_selected_xmax,
desc, opts, t);
continue;
}

Expand Down Expand Up @@ -902,6 +944,11 @@ window_fill(int window_identifier,
is_in_range = true;
}
}
else if (is_selectable && selected_xmin != -1 && pos != -1)
{
if (pos >= selected_xmin && pos <= selected_xmax)
is_in_range = true;
}

if (i != -1 && vcursor_xmin <= i && i <= vcursor_xmax)
{
Expand Down
Loading

0 comments on commit fefdf44

Please sign in to comment.