Giter Site home page Giter Site logo

okbob / pspg Goto Github PK

View Code? Open in Web Editor NEW
2.4K 47.0 87.0 4.52 MB

Unix pager (with very rich functionality) designed for work with tables. Designed for PostgreSQL, but MySQL is supported too. Works well with pgcli too. Can be used as CSV or TSV viewer too. It supports searching, selecting rows, columns, or block and export selected area to clipboard.

License: BSD 2-Clause "Simplified" License

Makefile 0.43% C 88.12% Shell 0.35% M4 7.29% Roff 3.81%
c ncurses tui postgres-pager postgresql psql terminal-pager terminal-app csv-viewer mysql

pspg's Introduction

Stand With Ukraine

pspg - Postgres Pager

Everybody who uses psql also uses the less pager. Which, while it works well, has no special support for tabular data. I found a few projects, but none was good enough for this purpose. Thus I decided to write a small specialized pager to use as a psql pager.

This pager can be used from the following command line clients, too:

Main target

Installation and basic configuration

The pspg can be simply installed from Debian (Ubuntu) repositories. RedHat (Fedora) repositories contains pspg too:

# Debian (Ubuntu)
sudo apt-get install pspg

# RedHat (Fedora)
sudo dnf install pspg

Basic configuration is very simple - just set system environment variable PSQL_PAGER:

export PSQL_PAGER='pspg -X -b'

Native installation on MS Windows is not supported, but pspg works well inside wsl2. Inside wsl2 environment, the installation is same like on used Linux system.

Installation on macOS/homebrew is simple by brew install pspg.

Video presentation

Video presentation

Screenshots

Screenshot Screenshot Screenshot Screenshot Screenshot Screenshot Screenshot

Options

[pavel@localhost ~]$ pspg --help
pspg is a Unix pager designed for table browsing.

Usage:
  pspg [OPTION] [file]

General options:
  --about                  about authors
  --help                   show this help
  -V, --version            show version
  --info                   show info about libraries and system
  --direct-color           force direct-color terminal mode
  -f, --file=FILE          open file
  -F, --quit-if-one-screen
                           quit if content is one screen
  --clipboard-app=NUM      specify app used by copy to clipboard (1, 2, 3, 4)
  --esc-delay=NUM          specify escape delay in ms (-1 inf, 0 not used, )
  --interactive            force interactive mode
  --ignore_file_suffix     don't try to deduce format from file suffix
  --ni                     not interactive mode (only for csv and query)
  --no-watch-file          don't watch inotify event of file
  --no-mouse               don't use own mouse handling
  --no-progressive-load    don't use progressive data load
  --no-sigint-search-reset
                           without reset searching on sigint (CTRL C)
  --no-sleep               without waits against flickering
  --no_xterm_mouse_mode    don't use optional xterm mouse mode
  --only-for-tables        use std pager when content is not table
  --on-sigint-exit         exit on sigint(CTRL C or Escape)
  --pgcli-fix              try to fix some pgcli related issues
  --querystream            read queries from stream forever
  --quit-on-f3             exit on F3 like mc viewers
  --rr=ROWNUM              rows reserved for specific purposes
  --stream                 read input forever
  -X, --reprint-on-exit    preserve content after exit

Output format options:
  -a, --ascii              force ascii
  -b, --blackwhite         black-white style
  -s, --style=N            set color style number (0..22)
  --bold-labels            row, column labels use bold font
  --bold-cursor            cursor use bold font
  --border                 type of borders (0..2)
  --double-header          header separator uses double lines
  --force-uniborder        replace ascii borders by unicode borders
  --highlight-odd-rec      highlights odd records (when it is supported by style)
  --hide-header-line       hides header line (between column names and data)
  --ignore-short-rows        rows with wrong column numbers are ignored
  --null=STRING            STRING used instead NULL

Searching options
  -g --hlite-search, -G --HILITE-SEARCH
                           don't highlight lines for searches
  -i --ignore-case         ignore case in searches that do not contain uppercase
  -I --IGNORE-CASE         ignore case in all searches

Interface options:
  -c, --freezecols=N       freeze N columns (0..9)
  --less-status-bar        status bar like less pager
  --line-numbers           show line number column
  --menu-always            show top bar menu every time
  --no-bars, --no-commandbar, --no-topbar
                           don't show bottom, top bar or both
  --no-cursor              row cursor will be hidden
  --no-last-row-search     don't use the last pattern when starting a new search
  --no-scrollbar           don't show scrollbar
  --no-sound               don't use beep when scroll is not possible
  --tabular-cursor         cursor is visible only when data has table format
  --vertical-cursor        show vertical column cursor

Input format options:
  --csv                    input stream has csv format
  --csv-separator          char used as field separator
  --csv-header [on/off]    specify header line usage
  --skip-columns-like="SPACE SEPARATED STRING LIST"
                           columns with substr in name are ignored
  --csv-trim-width=NUM     trim value after NUM chars
  --csv-trim-rows=NUM      trim value after NUM rows
  --tsv                    input stream has tsv format

On exit options:
  --on-exit-reset          sends reset terminal sequence "\33c"
  --on-exit-clean          sends clean terminal sequence "\033[2J"
  --on-exit-erase-line     sends erase line terminal sequence "\33[2K\r"
  --on-exit-sgr0           sends sgr0 terminal sequence "\033[0;10m"

Watch mode options:
  -q, --query=QUERY        execute query
  -w, --watch time         the query (or read file) is repeated every time (sec)

Connection options:
  -d, --dbname=DBNAME      database name
  -h, --host=HOSTNAME      database server host (default: "local socket")
  -p, --port=PORT          database server port (default: "5432")
  -U, --username=USERNAME  database user name
  -W, --password           force password prompt

Debug options:
  --log=FILE               log debug info to file
  --wait=NUM               wait NUM seconds to allow attach from a debugger

pspg shares a lot of key commands with the less pager or the vi editor.

Options can be passed within the environment variable PSPG, too.

Environment variables

Name Usage
PSPG can hold same options like command line
PSPG_CONF path to configuration file
PSPG_HISTORY path to file pspg's readline history file

Example of config file

The fields names can be different than from related command line options:

ascii_menu = false
bold_labels = false
bold_cursor = false
ignore_case = false
ignore_lower_case = false
no_cursor = false
no_sound = false
no_mouse = false
less_status_bar = false
no_highlight_search = false
no_highlight_lines = false
force_uniborder = false
show_rownum = false
without_commandbar = false
without_topbar = false
vertical_cursor = false
on_sigint_exit = false
no_sigint_search_reset = false
double_header = false
quit_on_f3 = false
pgcli_fix = false
xterm_mouse_mode = true
show_scrollbar = true
menu_always = false
empty_string_is_null = true
last_row_search = true
progressive_load_mode = true
highlight_odd_rec = false
hide_header_line = false
on_exit_reset = false
on_exit_clean = false
on_exit_erase_line = false
on_exit_sgr0 = false
direct_color = false
theme = 16
border_type = 2
default_clipboard_format = 0
clipboard_app = 0
hist_size = 500
esc_delay = -1

Themes

Code Name
0 black & white
1 Midnight Commander like
2 FoxPro like
3 Pdmenu like
4 White theme
5 Mutt like
6 PCFand like
7 Green theme
8 Blue theme
9 Word Perfect like
10 Low contrast blue theme
11 Dark cyan/black mode
12 Paradox like
13 dBase IV retro style
14 dBase IV retro style (Magenta labels)
15 Red white theme
16 Simple theme
17 Solarized dark theme
18 Solarized light theme
19 Gruvbox light theme
20 Tao Light theme
21 FlatWhite theme
22 Relational pipes theme
23 Paper Color theme

see http://okbob.blogspot.cz/2017/07/i-hope-so-every-who-uses-psql-uses-less.html

Custom themes

The theme can be customized over base and menu templates referencing the built-in themes. The custom theme file should be saved in directory with pspg configuration. The name of this file should be .pspg_theme_xxx. The custom theme can be selected by command line option --custom-style=name or by command \ctheme name.

Screenshot

Example of a custom theme file (named .pspg_theme_mc2 (it can be activated by command \ctheme mc2)):

template = 1
template_menu = 3

background = black, white
data = black, white
label = black, white, italic, bold
border = #000000, white
footer = lightgray, white
cursor_data = blue, white, italic, bold, dim, reverse
cursor_border = blue, blue , italic, bold, dim, reverse
cursor_label = blue, white, italic, bold, dim, reverse
cursor_footer = blue, white, italic, bold, dim, reverse
cursor_bookmark = red, white, italic, bold, dim, reverse
cross_cursor = white, blue, italic, bold
cross_cursor_border = brightblue, blue
status_bar = black, lightgray
title = black, lightgray
scrollbar_arrows = black, white
scrollbar_background = lightgray, white
scrollbar_slider = white, gray

Some keys can be marked by symbol *. Marked keys are used for odd records.

data* = black, lightgray
label* = black, lightgray, italic, bold
border* = #000000, lightgray

template and template_menu set fallback values for any keys not specified in the custom theme. template_menu in particular is currently the only way to customize the F9 menu appearance.

key customizes
background Background color
data Data (non-header or frozen column) text
border Border color
label Label (header or frozen column) text
row_number Line numbers
record_number
selected_area
footer Results footer (non-tabular e.g. rowcount)
cursor_data Highlighted data (non-header or frozen column) text
cursor_border Highlighted border color
cursor_label Highlighted label (header or frozen column) text
cursor_row_number Highlighted line numbers
cursor_record_number
cursor_selected_area
cursor_footer Highlighted results footer (non-tabular e.g. rowcount)
scrollbar_arrows Scrollbar up and down arrows
scrollbar_background Scrollbar "empty" background
scrollbar_slider Scrollbar slider over the background
scrollbar_active_slider Scrollbar slider grabbed by mouse
title Results headline (in psql describe commands)
status_bar Top query and cursor location information
prompt_bar
info_bar Info text (e.g. "Not found" when searching)
input_bar Input prompt and text (e.g. search)
error_bar
bookmark
bookmark_border
cursor_bookmark
cross_cursor Highlighted cell at intersection of horizontal and vertical cursors
cross_cursor_border Borders at intersection of horizontal and vertical cursors
matched_pattern Search result match text
matched_pattern_nohl
matched_line Line containing search result
matched_line_border Borders in search result line
matched_pattern_cursor Highlighted search result match text
matched_line_vertical_cursor Vertically highlighted search result match text
matched_line_vertical_cursor_border Borders of vertically highlighted cell with search result match
error

ANSI colors Black, Red, Green, Brown, Blue, Magenta, Cyan, LightGray, Gray, BrightRed, BrightGreen, Yellow, BrightBlue, BrightMagenta, BrightCyan, White, and Default will display as your terminal emulator configures them. Alternatively, you can specify hex RGB values #FF00FF.

Styles are any combination of: bold, italic, underline, reverse, standout, dim.

If the format of some key is not correct, then this row is ignored. For debugging of custom theme is good to start pspg with option --log. An information about broken definitions are stored in log file.

Keyboard commands

Key(s) Command
0, 1, 2, 3, .., 9 freeze first N columns
KEY_UP, k navigate backward by one line
KEY_DOWN, j navigate forward by one line
KEY_LEFT, h scroll to left
KEY_RIGHT, l scroll to right
Ctrl+KEY_LEFT scroll one char left
Ctrl+KEY_RIGHT scroll one char right
Shift+KEY_LEFT scroll one column left
Shift+KEY_RIGHT scroll one column right
Ctrl+Home, g go to the start of file
Ctrl+End, G go to the end of file
Alt+l go to line number
H go to first line of current window
M go to half of current window
L go to end of current window
PPAGE, Ctrl+b backward one window
NPAGE, Ctrl+f, space forward one window
HOME, ^ go to begin of line, first column
END, $ go to end of line, last column
Ctrl+e scroll a window down
Ctrl+y scroll a window up
Ctrl+d forward a half window
Ctrl+u backward a half window
s save content to file
/ search for a pattern which will take you to the next occurrence
? search for a pattern which will take you to the previous occurrence
n for next match
N for next match in reverse direction
c column search
Alt+/ search for a pattern inside selected area
Alt+? backward search for a pattern inside selected area
Alt+c switch (on, off) drawing line cursor
Alt+m switch (on, off) own mouse handler
Alt+n switch (on, off) drawing line numbers
Alt+v, double click on column header switch (on, off) drawing column cursor
Mouse button wheel scroll vertical
Alt+Mouse button wheel scroll horizontal
F9 show menu
q, F10, Esc 0 quit
Alt+q quit and print raw (unformatted) content
Alt+k, Alt+double click switch bookmark
Alt+j go to next bookmark
Alt+i go to previous bookmark
Alt+o flush bookmarks
a sort ascendent
d sort descendent
u unsorted (sorted in origin order)
Space stop/continue in watch mode
R Repaint screen and refresh input file
Ins export row, column or cell to default target
shift+cursor... define range
F3 start/finish of selection rows
Shift+F3 start/finish of selection block
Ctrl+drag mouse defines rows selection, on column header defines column selection
Ctrl+o show primary screen, press any key to return to pager again
%, Ctrl+a select all

Backslash commands

Command Description
\N go to line number
\+N go to N lines forward
\-N go to N lines backward
\N+ go to line number
\N- go to line number from end
\theme N set theme number
\copy [all|selected] [nullstr "str"] [csv|tsv|insert|text|pipesep|sqlvalues] copy data to clipboard
\save [all|selected] [nullstr "str"] [csv|tsv|insert|text|pipesep|sqlvalues] copy data to clipboard
\order [N|column name] sort by column
\orderd [N|column name] desc sort by column
\sort [N|column name] sort by column
\sortd [N|column name] desc sort by column
\dsort [N|column name] desc sort by column (alias)
\rsort [N|column name] desc sort by column (alias)
\asc [N|column name] sort by column (alias)
\desc [N|column name] desc sort by column (alias)
\search [back] [selected] [column name] [string|"string"] search string in data

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

\copy csv | less

Ending

The pager can be ended by pressing keys q or F10 or Esc 0. With option --on-sigint-exit then the pager is closed by pressing keys Ctrl+c or Esc Esc.

Use Escape, key instead Alt + key

pspg supports a possibility to use a sequence of keys Esc, key instead an combination of Alt+key. The interval between pressing Esc and key is limited by interval specified by option esc-delay or by configuration's option esc_delay. This is max delay time in ms. After this interval, the single pressing Esc is interpreted as Escape. -1 meas unlimited, 0 disables this feature.

Column search

Column search is case insensitive every time. Searched column is marked by vertical cursor. Last non empty string searching pattern is used when current searching pattern is empty string. Searching is starting after visible vertical column or on first visible not freezed columns (after some horizontal scrolling) or on first column. After last column searching starts from first again.

Export & Clipboard

For clipboard support the clipboard application should be installed: 1. wl-clipboard (Wayland), 2. xclip (xwindows), 3. pbcopy (MacOS) or 4. clip.exe (WSL2).

pspg try to translate unicode symbol '∅' to NULL every time. If you don't use special setting by \pset null ..., then psql displays empty string instead NULL. pspg hasn't any special detection (in export routines) for this case. You should to check and enable or disable menu item Empty string is NULL.

pspg has automatic detection of clipboard application. Unfortunately, this detection should not to work for same cases. You can specify the application by specify number (1,2,3,4) to --clipboard-app option.

Status line description

  • V: [d/d d..d] - vertical cursor: (column number)/(columns) (char positions from) .. (char positions to)
  • FC: d - freezed columns length in chars
  • C: d..d/d - unfreezed visible data in chars (from .. to)/(total)
  • L:[d + d d/d] - lines (number of first visible line) + (number of line of display), (current line)/(lines)
  • d% - percent of already displayed data

Usage as csv viewer

It works well with miller http://johnkerl.org/miller/doc/index.html

mlr --icsv --opprint --barred put '' obce.csv | pspg --force-uniborder

New version has integrated csv support - just use --csv option.

It can be integrated into mc

  • copy file from /etc/mc/mc.ext to your ~/.config/mc directory
  • insert there
##csv

regex/\.csv
    View=pspg -f %f --csv
  • restart mc

Known issues

  • When you use pspg on Cygwin, then some temporary freezing of scrolling was reported In this case, please, use an option --no-sleep. I see slow scrolling (via scrollbar) inside konsole (KDE terminal). The option --no-sleep helps too.

Usage in watch mode

The result of query can be refreshed every n seconds. pspg remembers cursor row, possible vertical cursor, possible ordering. The refreshing should be paused by pressing space key. Repeated pressing of this key enables refreshing again.

pspg uses inotify API when it is available, and when input file is changed, then pspg reread file immediately. This behave can be disabled by option --no-watch-file or by specification watch time by option --watch.

Streaming modes

pspg can read a continuous stream of tabular data from pipe, named pipe or from file (with an option --stream or it can read a stream of queries from pipe or from file (with an option --querystream). In stream mode, only data in table format can be processed, because pspg uses empty line as separator between tables.

The query stream mode is an sequence of SQL statements separated by char GS (Group separator - 0x1D on separated line.

pavel@localhost ~]$ cat < /dev/pts/3 > ~/pipe
select 10
^] 
select 20
^]
select *
from
pg_class
^]

Recommended psql configuration

you should to add to your profile:

#for Postgres 10 and older
export PAGER="pspg"

#for postgres 11 and newer
export PSQL_PAGER="pspg"

#or "\setenv PAGER pspg" to .psqlrc

and .psqlrc

\set QUIET 1
\pset linestyle unicode
\pset border 2
\pset null ∅
\unset QUIET

some possible configuration:

-- Switch pagers with :x and :xx commands
\set x '\\setenv PAGER less'
\set xx '\\setenv PAGER \'pspg -bX --no-mouse\''
:xx

LC_CTYPE should be correct. Mainly when you use unicode borders. ncurses doesn't display unicode borders (produced by psql) without correct setting of this variable. Is possible to check a value 'C.UTF8'.

Attention

When you use a option --only-for-tables, then

  • set PAGER to pspg and PSQL_PAGER to less or
  • set PAGER to less and PSQL_PAGER to pspg

MySQL usage

MariaDB [sakila]> pager pspg -s 14 -X --force-uniborder --quit-if-one-screen
PAGER set to 'pspg -s 14 -X --force-uniborder --quit-if-one-screen'
MariaDB [sakila]> select now();
MariaDB [sakila]> select * from nicer_but_slower_film_list limit 100;

SQLite

SQLite native client doesn't produce well formatted output, but can be forced to generate CSV format - and this format is well readable for pspg

sqlite3 -csv -header testdb.db 'select * from foo2' | pspg --csv --csv-header=on --double-header

pgcli

pgcli needs the following configuration options (~/.config/pgcli/config):

pager = /usr/bin/pspg --csv --rr=2 --quit-if-one-screen --ignore-case --csv-header on --pgcli-fix
table_format = csv

Older version of pgcli had very slow output in tabular format. An workaround was using csv format. This should not be necessary on current versions when the performance issue was fixed. An option --pgcli-fix fixed import of partially broken csv format generated by pgcli. Modern version of pgcli doesn't need csv format, and doesn't need --pgcli-fix option.

pager = /usr/bin/pspg --rr=2 --quit-if-one-screen --ignore-case

sqlcl

As sqlcl doesn't currently support a pager option directly, you can either use a tool like qsh to work around this issue, or use the pspg.sql script from this repo.

To use the script, start sqlcl as shown below (it's important to pass in the details of your current tty):

$ TTY=$(tty) sqlcl system/system @/path/to/pspg.sql

You can now have the results of a query sent to pspg like this:

SQL> pspg select * from user_tables;

nushell

The pspg supports default table_mode: rounded and table_mode: heavy.

The conversion to csv can be used too.

sys | get cpu | to csv | pspg --csv

Note: footer_mode should be disabled

nushell configuration:

$env.config.footer_mode = never
$env.config.table.header_on_separator = false
$env.config.ls.clickable_links = false
$env.config.table.mode = rounded 

Note - mouse

pspg try to use xterm mouse mode 1002, when terminal and ncurses are not too antique. If there are problems with usage - unwanted visual artefacts when you move with mouse when some mouse button is pressed, then 1. please, report issue (please, attach log file), 2. use an option --no-xterm-mouse-mode and pspg will not try to activate this mode.

Note - true color themes on KDE konsole terminal

On my Fedora this terminal doesn't correctly display true color themes. The basic problem is in default TERM setting, that is xterm-256color. Unfortunately, the konsole terminal is not fully compatible with xterm, and doesn't allow color changing. You can force direct colors by using the option --direct-color or by setting TERM=xterm-direct. Second option is more correct setting of TERM variable to konsole-256color. In this case the pspg will map the true rgb colors to supported 256 colors.

Note - compilation issue

Some linker issues can be fixed by:

I changed 
gcc -lncursesw pager.c -o pspg -ggdb
to
gcc pager.c -o pspg -ggdb -lncursesw

If you want to use pspg as Postgres client, then you need run configure --with-postgresql=yes. On Fedora with own Postgres build I had to install openssl-devel package and I had to set export PKG_CONFIG_PATH="/usr/local/pgsql/master/lib/pkgconfig/".

On FreeBsd you should to use gmake instead make.

Note - Installation details

When you compile code from source, run ./configure first. Sometimes ./autogen.sh first

If you would to display UTF-8 characters, then pspg should be linked with ncursesw library. UTF-8 characters are displayed badly when library ncursesw is used. You can see broken characters with incorrect locale setting too.

You can check wide chars support by pspg --version. Row ncurses with wide char support is expected. Re-run configure with --with-ncursesw option. When this command fails check if development package for ncursesw library is installed.

Homebrew (for Linux & MacOS)

# brew install pspg

You can compile easily pspg without brew, but you need gnu readline library. MacOS uses by default readline emulated over libedit, but pspg requires full gnu readline library.

LDFLAGS="-L/usr/local/opt/readline/lib" CPPFLAGS="-I/usr/local/opt/readline/include" ./configure
LDFLAGS="-L/usr/local/opt/readline/lib" CPPFLAGS="-I/usr/local/opt/readline/include" make

Debian

# apt-cache search pspg
# apt-get install pspg

Fedora (28 and later)

# dnf install pspg

RPM (CentOS/openSUSE/…)

The pspg is available from community repository https://yum.postgresql.org/packages.php

Alpine Linux

# apk add pspg

Gentoo

# emerge -av dev-db/pspg

Arch Linux

The Arch User Repository contains two versions:

  • pspg is a fixed release.
  • pspg-git tracks the master branch.

Use the AUR helper of your choice or git and makepkg to install pspg.

FreeBSD

# pkg install pspg

OpenBSD

# pkg_add pspg

More about it

Using MacPorts (MacOS only)

# port install pspg

MS Windows

pspg can be simply used on MS Windows by using wsl2. I tested it, and it is working without problems.

  • In terminal execute wsl --install -d Ubuntu-22.04

  • In terminal open Ubuntu session

sudo apt-get update
sudo apt-get install pspg
sudo apt-get install postgresql postgresql-contrib

# set password for user postgres
sudo passwd postgres
su - postgres
psql postgres
>> create role pavel login;
\q
exit
touch ~/.psqlrc
mcedit .psqlrc
\pset linestyle unicode
\pset border 2
\setenv PSQL_PAGER 'pspg -b -X'
# press F2 and F10
psql postgres

there is not any difference from installation and work on Ubuntu (Debian)

pspg is not ported to MS Windows yet. There is the dependency on ncurses and correctly (fully) implemented function newterm (pdcurses does this only on Unix platforms). It can work with WSL2 maybe (I didn't test it). An alternative can be using less pager, that is ported to some MS Win enviroments. less depends on termcap, and it is little bit more portable than pspg (termcal is low layer of ncurses). less supports fixed rows and with --chop-long-lines option or just -S can be used as pager for pspg.

export PSQL_PAGER="less --chop-long-lines --header 1"

Solaris

There are few issues requires manual code changes for successful compilation - we successfully tested pspg, but although pspg was linked with ncursesw libraries, the utf8 encoding support didn't work fully correctly - probably due some issues in libc library. There are problems with chars encoded to 3bytes - unicode borders, .. Two bytes unicode chars should be displayed well.

You can use pspg with usual accented chars, but unicode borders should not be used. Replacement ascii borders by special borders chars (by ncurses technology) works well - looks on Options|Force unicode borders option.

  • Solaris make doesn't support conditional statements - should be removed So, remove unsupported functionality from Makefile (ifdef,endif), replace -include by include first.

  • After running configure remove link on termcap library from config.make. It is garbage produced by readline automake script. Combination with ncurses libraries makes some linking issues.

builtin libraries

export CURSES_CFLAGS="-I/usr/include/ncurses/"
export PANEL_LIBS="-lpanelw"
./configure

OpenCSW development

export CFLAGS="-m64 -I/opt/csw/include"
export LDFLAGS="-L/opt/csw/lib/64 -R/opt/csw/lib/64"
export PKG_CONFIG_PATH="/opt/csw/lib/64/pkgconfig"
./configure

Possible ToDo

  • Store data in some column format (now data are stored like array of rows). With this change can be possible to operate over columns - hide columns, change width, cyclic iteration over columns, change order of columns, mark columns and export only selected columns (selected rows).

  • Replace printing document directly to ncurses window by some smarter structure. Internally there are lot of checks and fixes to support complex dynamic layout. The possibly views should to remember first row, last row, current row. Now, these data are in global variables or in DataDesc and ScrDesc structures.

st_menu

This project uses st_menu library - implementation of CUA menubar and pulldown menu for ncurses https://github.com/okbob/ncurses-st-menu

Note

If you like it, send a postcard from your home country to my address, please:

Pavel Stehule
Skalice 12
256 01 Benesov u Prahy
Czech Republic

I invite any questions, comments, bug reports, patches on mail address [email protected]

pspg's People

Contributors

df7cb avatar dk avatar dmfay avatar dmgk avatar ekremkaraca avatar jirutka avatar jonashaag avatar josser avatar jwilk avatar kassandry avatar lbschenkel avatar martint avatar mcclurmc avatar mjbnz avatar mnencia avatar nineinchnick avatar okbob avatar omar-polo avatar pjanx avatar pmav99 avatar rjuju avatar shiar avatar simi avatar sipc-gus avatar talek avatar thedrow avatar theosotr avatar timgates42 avatar titanofold avatar tshirtman avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

pspg's Issues

Merge top and bottom status line?

Hi,
I'm wondering if the top and bottom status lines couldn't be merged into one line at the bottom. The current top line is right-aligned, and the bottom line is left-aligned, so the content wouldn't have to change much (except when -f is used). This would save one line on the screen and would also match more closely the look of "less".
Thanks!

/, Enter to repeat search

In less, if you press / and type a string to search for, you can later press / and then Enter and it will search for the same string again. Pspg should behave this way as well.

supporting non-color terminals

To support non-color terminals, I would suggest to evaluate the 'b' command line switch (black-withe style):

Line 2414: if(!has_colors()) -> if(!has_colors() && style != 0)

history in search

Hi!

I should very much like to ask to add history in searches, like "less" does: one types '/', then can use keys up and down. Thanks for the great product!

/dk

Building on macOS

I was able to build on macOS Sierra by linking against ncurses, instead of ncursesw.

I don't know how to make this a conditional in the Makefile.

Value width miscalculated

PAGER="./pspg -s 11 -g -i -F" psql -c "SELECT (CASE WHEN random()<0.5 then 'ó' else '-' END),md5(random()::text) FROM generate_series(1,80)"

generates following output (note the misalignments):

 ó    |973263148f6c1da850c5b82f44bedd
 -    | 98d546af5b4f08861a8e0291b3117c22
 ó    |3e7a73ef2ae53719d7a5eaa88b6f41
 ó    |9a48e5cb0b378cbc9c72721c983adb
 -    | 0d5c73047d42b16a3ab7f09a845f1fe2
 -    | 087f7bc9a684617fc9c5522c0ee79fef
 -    | 81db99d85a7c63dc447b5675a0774bbb
 ó    |f00ab7b5e450eb314d8b9f1e4875db
 -    | 2ca1b394e873b1168c0e4eac829ac7ca

This happens with 8-bit (non unicode) locale (ISO8859-2 in my case).

Edit: it's better visible with constant value in 2nd column ("abcdef"):

 -    | abcdef
 ó    |cdef
 -    | abcdef
 ó    |cdef
 -    | abcdef
 -    | abcdef
 ó    |cdef

Mangled output with `--quit-if-one-screen`

Output gets mangled when --quit-if-one-screen it's in effect:

$ mysql
[…]
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

user@host/(none)> select now();
+---------------------+
| now()               |
+---------------------+
| 2018-01-28 23:59:24 |
+---------------------+
1 row in set (0.00 sec)

user@host/(none)> ^DBye
$ mysql --pager='pspg --quit-if-one-screen'
[…]
Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

user@host/(none)> select now();
+---------------------+| now()               |+---------------------+| 2018-01-28 23:59:46 |+---------------------+1 row in set (0.00 sec)

user@host/(none)> select now()\G
*************************** 1. row ***************************now(): 2018-01-28 23:59:481 row in set (0.00 sec)

user@host/(none)> ^DBye

Thanks

Header detection doesn't work with unicode_header_linestyle=single

Example output:

┌────────┬─────────────────────┬───────┬────────┐
│ Schema │        Name         │ Type  │ Owner  │
├────────┼─────────────────────┼───────┼────────┤
│ public │ FROM                │ table │ ilmari │
│ public │ JOIN                │ table │ ilmari │
│ public │ arraytest           │ table │ ilmari │
│ public │ bars                │ table │ ilmari │
└────────┴─────────────────────┴───────┴────────┘

how to compile

hi there, great project!

However, when I tried to compile:

git clone https://github.com/okbob/pspg.git postgres-pager
cd postgres-pager
make

I got errors about missing files:

pager.c:1:20: fatal error: curses.h: No such file or directory
/usr/bin/ld: cannot find -lncursesw

Some research suggested

sudo apt-get install build-essential libreadline-dev libncurses5-dev libncursesw5-dev

Which somewhat helps, but now I get

gcc -lncursesw pager.c -o pager
/tmp/ccih2jFq.o: In function `initialize_color_pairs':
pager.c:(.text+0x419): undefined reference to `use_default_colors'
pager.c:(.text+0x42d): undefined reference to `init_pair'
pager.c:(.text+0x441): undefined reference to `init_pair'
pager.c:(.text+0x455): undefined reference to `init_pair'
pager.c:(.text+0x469): undefined reference to `init_pair'
pager.c:(.text+0x47d): undefined reference to `init_pair'
/tmp/ccih2jFq.o:pager.c:(.text+0x491): more undefined references to `init_pair' follow
/tmp/ccih2jFq.o: In function `initialize_color_pairs':
pager.c:(.text+0x4af): undefined reference to `assume_default_colors'
pager.c:(.text+0x4c3): undefined reference to `init_pair'
... ... ... ...
pager.c:(.text+0x2a3b): undefined reference to `wrefresh'
pager.c:(.text+0x2aff): undefined reference to `endwin'
collect2: error: ld returned 1 exit status
Makefile:4: recipe for target 'compile' failed
make: *** [compile] Error 1

Any suggestions?

first column can't be wide

Hi,

i found problem with wide column (output of public key), can be tested with:
--not working (empty screen)
SELECT repeat('A','1000');
--works
SELECT 'a', repeat('A','1000');
--not working
SELECT repeat('A','1000'), 'a';

but maybe caused by some local environment settings...

David

Killing PSQL session borks terminal

I had a long-running query that I stopped, using Ctrl-C.

It seems, even though I can't find any processes running pspg that the terminal session is totally bunk. Even typing reset doesn't fix it: the cursor is stuck in column 0.

Crashes on strings with newlines

# select * from newline;
  t  
─────
 foo↵
 bar
(1 Zeile)
# \pset pager always

... then do the select again, and search for /foo:

Segmentation fault (core dumped)

Core was generated by `pspg -b --less-status-bar --ignore-case'.
Program terminated with signal SIGSEGV, Segmentation fault.
(gdb) bt
#0  __strstr_sse2_unaligned () at ../sysdeps/x86_64/multiarch/strstr-sse2-unaligned.S:40
#1  0x0000557cecc9fedb in window_fill (window_identifier=3, srcy=<optimized out>, srcx=0, cursor_row=0, desc=0x7ffe2ed3f740, 
    scrdesc=0x7ffe2ed3f200, opts=0x7ffe2ed3f1d4) at src/print.c:136
#2  0x0000557cecc9b540 in main (argc=<optimized out>, argv=<optimized out>) at src/pspg.c:1571

Please let me know if you need more details to reproduce. Thanks!

explain parameters

entering a invalid parameter
Usage: ./pspg [-b] [-s n] [-c n] [-f file] [-X]
is printed on the screen.

What are this for parameters and what are they good for. I couldn't find anything in the README.md

Error on RedHat 6.8

# ./configure
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
checking whether gcc accepts -g... yes
checking for gcc option to accept ISO C89... none needed
checking how to run the C preprocessor... gcc -E
checking for grep that handles long lines and -e... /bin/grep
checking for egrep... /bin/grep -E
checking for ANSI C header files... yes
checking for gcc... (cached) gcc
checking whether we are using the GNU C compiler... (cached) yes
checking whether gcc accepts -g... (cached) yes
checking for gcc option to accept ISO C89... (cached) none needed
checking for pkg-config... /usr/bin/pkg-config
checking pkg-config is at least version 0.9.0... yes
checking for ncursesw via pkg-config... yes
checking for working ncursesw/curses.h... yes
checking for working ncursesw.h... no
checking for working ncurses.h... yes
configure: creating ./config.status
config.status: creating config.make
# make
gcc -O3 src/pspg.c src/print.c src/unicode.c src/themes.c -o pspg -g -O2      -lncursesw
src/pspg.c: In function ‘get_code’:
src/pspg.c:1388: warning: passing argument 1 of ‘tigetstr’ discards qualifiers from pointer target type
/usr/include/curses.h:792: note: expected ‘char *’ but argument is of type ‘const char *’
#

What to do?

Problems with certain commands

If used with \?, the screen is blank (except for the Quit 0..4 C.Freeze) at the bottom. If I press PageDown it's still blank, but when I press it again, I get to see the last 2/3 or 3/4 of the output. I also get a mostly blank screen when using \d on some tables with many columns and constraints/triggers (for example, it only shows the first line of a textsearch trigger, but it does show a blank inverse video, cursor line, that can be moved with the arrow keys).

describe what an "alternate screen" is for -X option

Thanks for pspg. I've just started using it and it looks promising.

I was reading through the man page and also the recommended .psqlrc configs and noticed "-X" in the configuration. I don't know what alternate screen the documentation is referring to. I've looked at the source code and can't make sense of it either.

Would you consider expanding the documentation a bit with regards to "alternate screen" ?

Thanks again!

-m

error during make

Downloaded 0.6 version.
My linux OS is CentOS 5.11.

I ran configure, no errors there.
Then I ran make and get the following error:

gcc src/pspg.c -o pspg -g -O2   -D_GNU_SOURCE   -lncursesw
/tmp/ccIdvgCz.o: In function `main':
/home/user/Src/pspg-0.6/src/pspg.c:2403: undefined reference to `set_escdelay'
collect2: ld returned 1 exit status
make: *** [pspg] Error 1

An initial google search pointed to ncurses.
I have ncurses version 5.5-24 installed.

Yes, I know my OS is old but it's what I have to deal with.

Segfault on \sf

I always get "Segmentation fault" when trying to show SQL function source code using \sf.

"Save content to file" command creates zero-byte files

PSPG works beautifully for me except for the "Save content to file" command. After entering a filename, it returns the message "Cannot write to [filename]" and creates a zero-byte file at the specified path.

Actually, this has been the case for me for all builds since 2017/09 (I just git-pulled and recompiled it again today).

I'm invoking PSPG in my .psqlrc like so:

\setenv PAGER 'pspg -s 11 -X --no-mouse --no-sound'

...and running it on OSX 10.12.16 with psql 9.4.0.

Any ideas? Thanks!

Compilation again

I saw you released a new version with resizing implemented, so I got excited and tried

git clone https://github.com/okbob/pspg.git postgres-pager
cd postgres-pager
sudo apt-get install build-essential libreadline-dev libncurses5-dev libncursesw5-dev
gcc pager.c -o pager -L. -lncursesw
./pager -d

as per #1, but got

pager.c: In function ‘main’:
pager.c:1641:26: error: ‘BUTTON5_PRESSED’ undeclared (first use in this function)
       if (event.bstate & BUTTON5_PRESSED)
                          ^
pager.c:1641:26: note: each undeclared identifier is reported only once for each function it appears in

instead. I'm using commit e0a0aba17 here. Any suggestions?

Compilation again (sorry)

Cloning the newest sources (7ed50cb), I sadly get pager.c:20:26: fatal error: ncurses/term.h: No such file or directory:

$ sudo apt-get update
$ sudo apt-get install build-essential libreadline-dev libncurses5-dev libncursesw5-dev
build-essential is already the newest version (12.1ubuntu2).
libncurses5-dev is already the newest version (6.0+20160213-1ubuntu1).
libncursesw5-dev is already the newest version (6.0+20160213-1ubuntu1).
libreadline-dev is already the newest version (6.3-8ubuntu2).

$ gcc pager.c -o pager -L. -lncursesw                                                  
pager.c:20:26: fatal error: ncurses/term.h: No such file or directory
compilation terminated.

Update Looks like I can comment out line 20, which compiles...

Feature request: disable mouse support

It's nice to be able to mark text for copy. On my linux workstation, mouse control only selects the active row which is not a good tradeoff on balance; it would be nice to have command switch to let O/S handle all mouse activity...thanks

Column management

Just an idea of feature request: since pspg is aware of columns in psql output, it could as well allow to:

  1. hide columns from the view - especially usefull for SELECT * queries for wide tables, since there is no "SELECT all_except_specified_columns",
  2. reorder columns - also saves typing of all the column names instead of "*", since PostgreSQL itself doesn't allow to change default order and on-disk structure might be subject of on-purpose alignment,
  3. ellipse specified columns,
  4. show expanded (psql -x) version of the record when selected and (e.g.) ENTER pressed.
  5. provide maximum column width as a command line argument.

User interface:
c - enter column management mode, cursor goes to top-line header, hidden columns are displayed dimmed
left/right arrows - selecting column
h - toggle "hide"
</> - shrink/expand column
m - mark column for "move" operation, left/right arrows to position, enter to accept
c/enter - exit column management mode

Brew formula question

Hi! I've created brew formula for your awesome project but there is one small thing I don't know how to handle.

Brew is a package manager for OS X like portage in gentoo or ports in freebsd.
They have a lot of acceptance criteria and one of them is having a test of application for running after compiling.
For small programs they usually check for --help or --version, but this is not considered as a good practice. Better to have some 'real' cases of application usage.
Also, those tests should be non-interactive so CI can run them

So, I've started to write a formula but it's turned out that I can't write any type of test for your app.
I've checked how they usually test other pagers, i.e vimpager: https://github.com/Homebrew/homebrew-core/blob/93a8200c17986038585a140cce4a9bc93e4a3f86/Formula/vimpager.rb

but this approach is not fit for your pager because it bring up interactive interface even if there is not enough content for paging.
Also, you don't have --version or --help.

So, i've created this #27

I have never write any line of C, but I hope there is nothing wrong with this few lines.
If you accept PR, please, do a release so I can update brew formula with new version (0.3?)

Thank you and sorry if my english is not good )

Add more key commands like less: ^E ^Y ^D ^U

More key commands that many less users will naturally reach for:

  • ^E scroll window down one line, keeping cursor in place
  • ^Y scroll window up one line, keeping cursor in place
  • ^D forward a half window
  • ^U backward a half window

Feature request: better handling of zero row results

unpaged result
postgres=# select pid, usename from pg_stat_activity limit 0;
pid │ usename
─────┼─────────
(0 rows)

Time: 1.419 ms
postgres=# select pid, usename from pg_stat_activity limit 1;
pid │ usename
───────┼─────────
22436 │ rms
(1 row)

paged result
postgres=# select pid, usename from pg_stat_activity limit 0;
─────┼─────────
─────┼─────────
(0 rows)
Time: 1.002 ms
postgres=# select pid, usename from pg_stat_activity limit 1;
pid │ usename
───────┼─────────
22436 │ rms
(1 row)

The zero row paged result does not show column names, just the UTF8 border characters. The column names are present in the textual results, and presumably ought to be shown.

Mention setting PAGER variable in README

Hi,
I installed this, but then of course nothing happened. It would be great to mention setting PAGER is needed to use this (or maybe there is something else I don't know about ?)

Cheers
S

Sementation fault on 'G'

pspg segfaults when pressing G for this input:

 id 
----
 0
 0
  
 0
 0
 0
 0
 0
 0
 0
(10 rows)

Used ncurses version is 6.1.

cannot to reopen stdin: No such device or address

When running psql with su:

su postgres -c "psql test"

I get the following error:

Line style is unicode.
Border style is 2.
Pager is used for long output.
Timing is on.
Null display is "¤".
psql (10.1)
Type "help" for help.

test=# select * from test;
cannot to reopen stdin: No such device or address
Time: 14.278 ms
test=#

Failed to compile

Version : Linux 1ffdea63e0b2 4.13.0-36-generic #40-Ubuntu

gcc -O3 src/pspg.c src/print.c src/unicode.c src/themes.c -o pspg -g -O2   -D_GNU_SOURCE -D_DEFAULT_SOURCE -I/usr/include/ncursesw  -lncursesw -ltinfo
src/pspg.c:157:1: error: conflicting types for 'strcasestr'
 strcasestr(const char *s1, const char *s2)
 ^~~~~~~~~~
In file included from src/pspg.c:19:0:
/usr/include/string.h:368:14: note: previous declaration of 'strcasestr' was here
 extern char *strcasestr (const char *__haystack, const char *__needle)
              ^~~~~~~~~~
Makefile:10: recipe for target 'pspg' failed
make: *** [pspg] Error 1

Grepping the source code revealed that the strcasestr function is never used, but:

  • I am not a real C user
  • I do not want to break things I do not understand

Locally, removing the declaration made the build successful

Versioning problems

The 0.4 tag appears to output 0.3devel when --version is passed.

Additionally, the spec file lists the version as 1.0.0, which seems weird.

Highlighting search results

Hi,
it would be nice if pspg would highlight search results, both in the current line where the cursor is, but also in other lines, so it's immediately obvious if there are more hits on screen (with possibly more hits to the left or right, but assuming the hits are in one column only, that won't be a problem in practise). less does this by default, see -g and -G, but I'm not sure it needs to be configurable.
Thanks!

Problems using UTF8 encoding

It seems that pspg is not capable of printing some special characters such as "á" which is printed as "M-CM-!". The symbol is properly shown using "less".

LANG is set to ca_ES.utf8 and psql encoding to utf8.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.