summaryrefslogtreecommitdiffstats
path: root/debian/patches/0002-vendorized-cli_helpers.patch
blob: 815039742181933128b638baba136dc423b64576 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
From: Lennart Weller <lhw@ring0.de>
Date: Fri, 8 May 2020 10:29:09 +0200
Subject: vendorized cli_helpers

---
 pgcli/main.py                                      |   4 +-
 pgcli/packages/cli_helpers/__init__.py             |   1 +
 pgcli/packages/cli_helpers/compat.py               |  42 ++++
 pgcli/packages/cli_helpers/config.py               | 270 +++++++++++++++++++++
 .../cli_helpers/tabular_output/__init__.py         |  13 +
 .../tabular_output/delimited_output_adapter.py     |  48 ++++
 .../cli_helpers/tabular_output/output_formatter.py | 228 +++++++++++++++++
 .../cli_helpers/tabular_output/preprocessors.py    | 266 ++++++++++++++++++++
 .../cli_helpers/tabular_output/tabulate_adapter.py |  98 ++++++++
 .../tabular_output/terminaltables_adapter.py       |  96 ++++++++
 .../tabular_output/tsv_output_adapter.py           |  16 ++
 .../tabular_output/vertical_table_adapter.py       |  66 +++++
 pgcli/packages/cli_helpers/utils.py                |  70 ++++++
 pgcli/pgcompleter.py                               |   2 +-
 14 files changed, 1217 insertions(+), 3 deletions(-)
 create mode 100644 pgcli/packages/cli_helpers/__init__.py
 create mode 100644 pgcli/packages/cli_helpers/compat.py
 create mode 100644 pgcli/packages/cli_helpers/config.py
 create mode 100644 pgcli/packages/cli_helpers/tabular_output/__init__.py
 create mode 100644 pgcli/packages/cli_helpers/tabular_output/delimited_output_adapter.py
 create mode 100644 pgcli/packages/cli_helpers/tabular_output/output_formatter.py
 create mode 100644 pgcli/packages/cli_helpers/tabular_output/preprocessors.py
 create mode 100644 pgcli/packages/cli_helpers/tabular_output/tabulate_adapter.py
 create mode 100644 pgcli/packages/cli_helpers/tabular_output/terminaltables_adapter.py
 create mode 100644 pgcli/packages/cli_helpers/tabular_output/tsv_output_adapter.py
 create mode 100644 pgcli/packages/cli_helpers/tabular_output/vertical_table_adapter.py
 create mode 100644 pgcli/packages/cli_helpers/utils.py

diff --git a/pgcli/main.py b/pgcli/main.py
index 08c6c49..a1ad8ed 100644
--- a/pgcli/main.py
+++ b/pgcli/main.py
@@ -20,8 +20,8 @@ from codecs import open
 
 keyring = None  # keyring will be loaded later
 
-from cli_helpers.tabular_output import TabularOutputFormatter
-from cli_helpers.tabular_output.preprocessors import align_decimals, format_numbers
+from .packages.cli_helpers.tabular_output import TabularOutputFormatter
+from .packages.cli_helpers.tabular_output.preprocessors import align_decimals, format_numbers
 import click
 
 try:
diff --git a/pgcli/packages/cli_helpers/__init__.py b/pgcli/packages/cli_helpers/__init__.py
new file mode 100644
index 0000000..3f262a6
--- /dev/null
+++ b/pgcli/packages/cli_helpers/__init__.py
@@ -0,0 +1 @@
+__version__ = '1.2.1'
diff --git a/pgcli/packages/cli_helpers/compat.py b/pgcli/packages/cli_helpers/compat.py
new file mode 100644
index 0000000..3f67c62
--- /dev/null
+++ b/pgcli/packages/cli_helpers/compat.py
@@ -0,0 +1,42 @@
+# -*- coding: utf-8 -*-
+"""OS and Python compatibility support."""
+
+from decimal import Decimal
+import sys
+
+PY2 = sys.version_info[0] == 2
+WIN = sys.platform.startswith('win')
+MAC = sys.platform == 'darwin'
+
+
+if PY2:
+    text_type = unicode
+    binary_type = str
+    long_type = long
+    int_types = (int, long)
+
+    from UserDict import UserDict
+    from backports import csv
+
+    from StringIO import StringIO
+    from itertools import izip_longest as zip_longest
+else:
+    text_type = str
+    binary_type = bytes
+    long_type = int
+    int_types = (int,)
+
+    from collections import UserDict
+    import csv
+    from io import StringIO
+    from itertools import zip_longest
+
+
+HAS_PYGMENTS = True
+try:
+    from pygments.formatters.terminal256 import Terminal256Formatter
+except ImportError:
+    HAS_PYGMENTS = False
+    Terminal256Formatter = None
+
+float_types = (float, Decimal)
diff --git a/pgcli/packages/cli_helpers/config.py b/pgcli/packages/cli_helpers/config.py
new file mode 100644
index 0000000..b75a8c9
--- /dev/null
+++ b/pgcli/packages/cli_helpers/config.py
@@ -0,0 +1,270 @@
+# -*- coding: utf-8 -*-
+"""Read and write an application's config files."""
+
+from __future__ import unicode_literals
+import io
+import logging
+import os
+
+from configobj import ConfigObj, ConfigObjError
+from validate import ValidateError, Validator
+
+from compat import MAC, text_type, UserDict, WIN
+
+logger = logging.getLogger(__name__)
+
+
+class ConfigError(Exception):
+    """Base class for exceptions in this module."""
+    pass
+
+
+class DefaultConfigValidationError(ConfigError):
+    """Indicates the default config file did not validate correctly."""
+    pass
+
+
+class Config(UserDict, object):
+    """Config reader/writer class.
+
+    :param str app_name: The application's name.
+    :param str app_author: The application author/organization.
+    :param str filename: The config filename to look for (e.g. ``config``).
+    :param dict/str default: The default config values or absolute path to
+                             config file.
+    :param bool validate: Whether or not to validate the config file.
+    :param bool write_default: Whether or not to write the default config
+                               file to the user config directory if it doesn't
+                               already exist.
+    :param tuple additional_dirs: Additional directories to check for a config
+                                  file.
+    """
+
+    def __init__(self, app_name, app_author, filename, default=None,
+                 validate=False, write_default=False, additional_dirs=()):
+        super(Config, self).__init__()
+        #: The :class:`ConfigObj` instance.
+        self.data = ConfigObj()
+
+        self.default = {}
+        self.default_file = self.default_config = None
+        self.config_filenames = []
+
+        self.app_name, self.app_author = app_name, app_author
+        self.filename = filename
+        self.write_default = write_default
+        self.validate = validate
+        self.additional_dirs = additional_dirs
+
+        if isinstance(default, dict):
+            self.default = default
+            self.update(default)
+        elif isinstance(default, text_type):
+            self.default_file = default
+        elif default is not None:
+            raise TypeError(
+                '"default" must be a dict or {}, not {}'.format(
+                    text_type.__name__, type(default)))
+
+        if self.write_default and not self.default_file:
+            raise ValueError('Cannot use "write_default" without specifying '
+                             'a default file.')
+
+        if self.validate and not self.default_file:
+            raise ValueError('Cannot use "validate" without specifying a '
+                             'default file.')
+
+    def read_default_config(self):
+        """Read the default config file.
+
+        :raises DefaultConfigValidationError: There was a validation error with
+                                              the *default* file.
+        """
+        if self.validate:
+            self.default_config = ConfigObj(configspec=self.default_file,
+                                            list_values=False, _inspec=True,
+                                            encoding='utf8')
+            valid = self.default_config.validate(Validator(), copy=True,
+                                                 preserve_errors=True)
+            if valid is not True:
+                for name, section in valid.items():
+                    if section is True:
+                        continue
+                    for key, value in section.items():
+                        if isinstance(value, ValidateError):
+                            raise DefaultConfigValidationError(
+                                'section [{}], key "{}": {}'.format(
+                                    name, key, value))
+        elif self.default_file:
+            self.default_config, _ = self.read_config_file(self.default_file)
+
+        self.update(self.default_config)
+
+    def read(self):
+        """Read the default, additional, system, and user config files.
+
+        :raises DefaultConfigValidationError: There was a validation error with
+                                              the *default* file.
+        """
+        if self.default_file:
+            self.read_default_config()
+        return self.read_config_files(self.all_config_files())
+
+    def user_config_file(self):
+        """Get the absolute path to the user config file."""
+        return os.path.join(
+            get_user_config_dir(self.app_name, self.app_author),
+            self.filename)
+
+    def system_config_files(self):
+        """Get a list of absolute paths to the system config files."""
+        return [os.path.join(f, self.filename) for f in get_system_config_dirs(
+            self.app_name, self.app_author)]
+
+    def additional_files(self):
+        """Get a list of absolute paths to the additional config files."""
+        return [os.path.join(f, self.filename) for f in self.additional_dirs]
+
+    def all_config_files(self):
+        """Get a list of absolute paths to all the config files."""
+        return (self.additional_files() + self.system_config_files() +
+                [self.user_config_file()])
+
+    def write_default_config(self, overwrite=False):
+        """Write the default config to the user's config file.
+
+        :param bool overwrite: Write over an existing config if it exists.
+        """
+        destination = self.user_config_file()
+        if not overwrite and os.path.exists(destination):
+            return
+
+        with io.open(destination, mode='wb') as f:
+            self.default_config.write(f)
+
+    def write(self, outfile=None, section=None):
+        """Write the current config to a file (defaults to user config).
+
+        :param str outfile: The path to the file to write to.
+        :param None/str section: The config section to write, or :data:`None`
+                                 to write the entire config.
+        """
+        with io.open(outfile or self.user_config_file(), 'wb') as f:
+            self.data.write(outfile=f, section=section)
+
+    def read_config_file(self, f):
+        """Read a config file *f*.
+
+        :param str f: The path to a file to read.
+        """
+        configspec = self.default_file if self.validate else None
+        try:
+            config = ConfigObj(infile=f, configspec=configspec,
+                               interpolation=False, encoding='utf8')
+        except ConfigObjError as e:
+            logger.warning(
+                'Unable to parse line {} of config file {}'.format(
+                    e.line_number, f))
+            config = e.config
+
+        valid = True
+        if self.validate:
+            valid = config.validate(Validator(), preserve_errors=True,
+                                    copy=True)
+        if bool(config):
+            self.config_filenames.append(config.filename)
+
+        return config, valid
+
+    def read_config_files(self, files):
+        """Read a list of config files.
+
+        :param iterable files: An iterable (e.g. list) of files to read.
+        """
+        errors = {}
+        for _file in files:
+            config, valid = self.read_config_file(_file)
+            self.update(config)
+            if valid is not True:
+                errors[_file] = valid
+        return errors or True
+
+
+def get_user_config_dir(app_name, app_author, roaming=True, force_xdg=True):
+    """Returns the config folder for the application.  The default behavior
+    is to return whatever is most appropriate for the operating system.
+
+    For an example application called ``"My App"`` by ``"Acme"``,
+    something like the following folders could be returned:
+
+    macOS (non-XDG):
+      ``~/Library/Application Support/My App``
+    Mac OS X (XDG):
+      ``~/.config/my-app``
+    Unix:
+      ``~/.config/my-app``
+    Windows 7 (roaming):
+      ``C:\\Users\<user>\AppData\Roaming\Acme\My App``
+    Windows 7 (not roaming):
+      ``C:\\Users\<user>\AppData\Local\Acme\My App``
+
+    :param app_name: the application name. This should be properly capitalized
+                     and can contain whitespace.
+    :param app_author: The app author's name (or company). This should be
+                       properly capitalized and can contain whitespace.
+    :param roaming: controls if the folder should be roaming or not on Windows.
+                    Has no effect on non-Windows systems.
+    :param force_xdg: if this is set to `True`, then on macOS the XDG Base
+                      Directory Specification will be followed. Has no effect
+                      on non-macOS systems.
+
+    """
+    if WIN:
+        key = 'APPDATA' if roaming else 'LOCALAPPDATA'
+        folder = os.path.expanduser(os.environ.get(key, '~'))
+        return os.path.join(folder, app_author, app_name)
+    if MAC and not force_xdg:
+        return os.path.join(os.path.expanduser(
+            '~/Library/Application Support'), app_name)
+    return os.path.join(
+        os.path.expanduser(os.environ.get('XDG_CONFIG_HOME', '~/.config')),
+        _pathify(app_name))
+
+
+def get_system_config_dirs(app_name, app_author, force_xdg=True):
+    r"""Returns a list of system-wide config folders for the application.
+
+    For an example application called ``"My App"`` by ``"Acme"``,
+    something like the following folders could be returned:
+
+    macOS (non-XDG):
+      ``['/Library/Application Support/My App']``
+    Mac OS X (XDG):
+      ``['/etc/xdg/my-app']``
+    Unix:
+      ``['/etc/xdg/my-app']``
+    Windows 7:
+      ``['C:\ProgramData\Acme\My App']``
+
+    :param app_name: the application name. This should be properly capitalized
+                     and can contain whitespace.
+    :param app_author: The app author's name (or company). This should be
+                       properly capitalized and can contain whitespace.
+    :param force_xdg: if this is set to `True`, then on macOS the XDG Base
+                      Directory Specification will be followed. Has no effect
+                      on non-macOS systems.
+
+    """
+    if WIN:
+        folder = os.environ.get('PROGRAMDATA')
+        return [os.path.join(folder, app_author, app_name)]
+    if MAC and not force_xdg:
+        return [os.path.join('/Library/Application Support', app_name)]
+    dirs = os.environ.get('XDG_CONFIG_DIRS', '/etc/xdg')
+    paths = [os.path.expanduser(x) for x in dirs.split(os.pathsep)]
+    return [os.path.join(d, _pathify(app_name)) for d in paths]
+
+
+def _pathify(s):
+    """Convert spaces to hyphens and lowercase a string."""
+    return '-'.join(s.split()).lower()
diff --git a/pgcli/packages/cli_helpers/tabular_output/__init__.py b/pgcli/packages/cli_helpers/tabular_output/__init__.py
new file mode 100644
index 0000000..de2f62f
--- /dev/null
+++ b/pgcli/packages/cli_helpers/tabular_output/__init__.py
@@ -0,0 +1,13 @@
+# -*- coding: utf-8 -*-
+"""CLI Helper's tabular output module makes it easy to format your data using
+various formatting libraries.
+
+When formatting data, you'll primarily use the
+:func:`~cli_helpers.tabular_output.format_output` function and
+:class:`~cli_helpers.tabular_output.TabularOutputFormatter` class.
+
+"""
+
+from .output_formatter import format_output, TabularOutputFormatter
+
+__all__ = ['format_output', 'TabularOutputFormatter']
diff --git a/pgcli/packages/cli_helpers/tabular_output/delimited_output_adapter.py b/pgcli/packages/cli_helpers/tabular_output/delimited_output_adapter.py
new file mode 100644
index 0000000..b13d008
--- /dev/null
+++ b/pgcli/packages/cli_helpers/tabular_output/delimited_output_adapter.py
@@ -0,0 +1,48 @@
+# -*- coding: utf-8 -*-
+"""A delimited data output adapter (e.g. CSV, TSV)."""
+
+from __future__ import unicode_literals
+import contextlib
+
+from ..compat import csv, StringIO
+from ..utils import filter_dict_by_key
+from .preprocessors import bytes_to_string, override_missing_value
+
+supported_formats = ('csv', 'csv-tab')
+preprocessors = (override_missing_value, bytes_to_string)
+
+
+class linewriter(object):
+    def __init__(self):
+        self.reset()
+
+    def reset(self):
+        self.line = None
+
+    def write(self, d):
+        self.line = d
+
+
+def adapter(data, headers, table_format='csv', **kwargs):
+    """Wrap the formatting inside a function for TabularOutputFormatter."""
+    keys = ('dialect', 'delimiter', 'doublequote', 'escapechar',
+            'quotechar', 'quoting', 'skipinitialspace', 'strict')
+    if table_format == 'csv':
+        delimiter = ','
+    elif table_format == 'csv-tab':
+        delimiter = '\t'
+    else:
+        raise ValueError('Invalid table_format specified.')
+
+    ckwargs = {'delimiter': delimiter, 'lineterminator': ''}
+    ckwargs.update(filter_dict_by_key(kwargs, keys))
+
+    l = linewriter()
+    writer = csv.writer(l, **ckwargs)
+    writer.writerow(headers)
+    yield l.line
+
+    for row in data:
+        l.reset()
+        writer.writerow(row)
+        yield l.line
diff --git a/pgcli/packages/cli_helpers/tabular_output/output_formatter.py b/pgcli/packages/cli_helpers/tabular_output/output_formatter.py
new file mode 100644
index 0000000..471a2ea
--- /dev/null
+++ b/pgcli/packages/cli_helpers/tabular_output/output_formatter.py
@@ -0,0 +1,228 @@
+# -*- coding: utf-8 -*-
+"""A generic tabular data output formatter interface."""
+
+from __future__ import unicode_literals
+from collections import namedtuple
+
+from ..compat import (text_type, binary_type, int_types, float_types,
+                                zip_longest)
+from ..utils import unique_items
+from . import (delimited_output_adapter, vertical_table_adapter,
+               tabulate_adapter, terminaltables_adapter, tsv_output_adapter)
+from decimal import Decimal
+
+import itertools
+
+MISSING_VALUE = '<null>'
+MAX_FIELD_WIDTH = 500
+
+TYPES = {
+    type(None): 0,
+    bool: 1,
+    int: 2,
+    float: 3,
+    Decimal: 3,
+    binary_type: 4,
+    text_type: 5
+}
+
+OutputFormatHandler = namedtuple(
+    'OutputFormatHandler',
+    'format_name preprocessors formatter formatter_args')
+
+
+class TabularOutputFormatter(object):
+    """An interface to various tabular data formatting libraries.
+
+    The formatting libraries supported include:
+      - `tabulate <https://bitbucket.org/astanin/python-tabulate>`_
+      - `terminaltables <https://robpol86.github.io/terminaltables/>`_
+      - a CLI Helper vertical table layout
+      - delimited formats (CSV and TSV)
+
+    :param str format_name: An optional, default format name.
+
+    Usage::
+
+      >>> from cli_helpers.tabular_output import TabularOutputFormatter
+      >>> formatter = TabularOutputFormatter(format_name='simple')
+      >>> data = ((1, 87), (2, 80), (3, 79))
+      >>> headers = ('day', 'temperature')
+      >>> print(formatter.format_output(data, headers))
+        day    temperature
+      -----  -------------
+          1             87
+          2             80
+          3             79
+
+    You can use any :term:`iterable` for the data or headers::
+
+      >>> data = enumerate(('87', '80', '79'), 1)
+      >>> print(formatter.format_output(data, headers))
+        day    temperature
+      -----  -------------
+          1             87
+          2             80
+          3             79
+
+    """
+
+    _output_formats = {}
+
+    def __init__(self, format_name=None):
+        """Set the default *format_name*."""
+        self._format_name = None
+
+        if format_name:
+            self.format_name = format_name
+
+    @property
+    def format_name(self):
+        """The current format name.
+
+        This value must be in :data:`supported_formats`.
+
+        """
+        return self._format_name
+
+    @format_name.setter
+    def format_name(self, format_name):
+        """Set the default format name.
+
+        :param str format_name: The display format name.
+        :raises ValueError: if the format is not recognized.
+
+        """
+        if format_name in self.supported_formats:
+            self._format_name = format_name
+        else:
+            raise ValueError('unrecognized format_name "{}"'.format(
+                format_name))
+
+    @property
+    def supported_formats(self):
+        """The names of the supported output formats in a :class:`tuple`."""
+        return tuple(self._output_formats.keys())
+
+    @classmethod
+    def register_new_formatter(cls, format_name, handler, preprocessors=(),
+                               kwargs=None):
+        """Register a new output formatter.
+
+        :param str format_name: The name of the format.
+        :param callable handler: The function that formats the data.
+        :param tuple preprocessors: The preprocessors to call before
+            formatting.
+        :param dict kwargs: Keys/values for keyword argument defaults.
+
+        """
+        cls._output_formats[format_name] = OutputFormatHandler(
+            format_name, preprocessors, handler, kwargs or {})
+
+    def format_output(self, data, headers, format_name=None,
+                      preprocessors=(), column_types=None, **kwargs):
+        """Format the headers and data using a specific formatter.
+
+        *format_name* must be a supported formatter (see
+        :attr:`supported_formats`).
+
+        :param iterable data: An :term:`iterable` (e.g. list) of rows.
+        :param iterable headers: The column headers.
+        :param str format_name: The display format to use (optional, if the
+            :class:`TabularOutputFormatter` object has a default format set).
+        :param tuple preprocessors: Additional preprocessors to call before
+                                    any formatter preprocessors.
+        :param \*\*kwargs: Optional arguments for the formatter.
+        :return: The formatted data.
+        :rtype: str
+        :raises ValueError: If the *format_name* is not recognized.
+
+        """
+        format_name = format_name or self._format_name
+        if format_name not in self.supported_formats:
+            raise ValueError('unrecognized format "{}"'.format(format_name))
+
+        (_, _preprocessors, formatter,
+         fkwargs) = self._output_formats[format_name]
+        fkwargs.update(kwargs)
+        if column_types is None:
+            data = list(data)
+            column_types = self._get_column_types(data)
+        for f in unique_items(preprocessors + _preprocessors):
+            data, headers = f(data, headers, column_types=column_types,
+                              **fkwargs)
+        return formatter(list(data), headers, column_types=column_types, **fkwargs)
+
+    def _get_column_types(self, data):
+        """Get a list of the data types for each column in *data*."""
+        columns = list(zip_longest(*data))
+        return [self._get_column_type(column) for column in columns]
+
+    def _get_column_type(self, column):
+        """Get the most generic data type for iterable *column*."""
+        type_values = [TYPES[self._get_type(v)] for v in column]
+        inverse_types = {v: k for k, v in TYPES.items()}
+        return inverse_types[max(type_values)]
+
+    def _get_type(self, value):
+        """Get the data type for *value*."""
+        if value is None:
+            return type(None)
+        elif type(value) in int_types:
+            return int
+        elif type(value) in float_types:
+            return float
+        elif isinstance(value, binary_type):
+            return binary_type
+        else:
+            return text_type
+
+
+def format_output(data, headers, format_name, **kwargs):
+    """Format output using *format_name*.
+
+    This is a wrapper around the :class:`TabularOutputFormatter` class.
+
+    :param iterable data: An :term:`iterable` (e.g. list) of rows.
+    :param iterable headers: The column headers.
+    :param str format_name: The display format to use.
+    :param \*\*kwargs: Optional arguments for the formatter.
+    :return: The formatted data.
+    :rtype: str
+
+    """
+    formatter = TabularOutputFormatter(format_name=format_name)
+    return formatter.format_output(data, headers, **kwargs)
+
+
+for vertical_format in vertical_table_adapter.supported_formats:
+    TabularOutputFormatter.register_new_formatter(
+        vertical_format, vertical_table_adapter.adapter,
+        vertical_table_adapter.preprocessors,
+        {'table_format': vertical_format, 'missing_value': MISSING_VALUE, 'max_field_width': None})
+
+for delimited_format in delimited_output_adapter.supported_formats:
+    TabularOutputFormatter.register_new_formatter(
+        delimited_format, delimited_output_adapter.adapter,
+        delimited_output_adapter.preprocessors,
+        {'table_format': delimited_format, 'missing_value': '', 'max_field_width': None})
+
+for tabulate_format in tabulate_adapter.supported_formats:
+    TabularOutputFormatter.register_new_formatter(
+        tabulate_format, tabulate_adapter.adapter,
+        tabulate_adapter.preprocessors +
+        (tabulate_adapter.style_output_table(tabulate_format),),
+        {'table_format': tabulate_format, 'missing_value': MISSING_VALUE, 'max_field_width': MAX_FIELD_WIDTH})
+
+for terminaltables_format in terminaltables_adapter.supported_formats:
+    TabularOutputFormatter.register_new_formatter(
+        terminaltables_format, terminaltables_adapter.adapter,
+        terminaltables_adapter.preprocessors +
+        (terminaltables_adapter.style_output_table(terminaltables_format),),
+        {'table_format': terminaltables_format, 'missing_value': MISSING_VALUE, 'max_field_width': MAX_FIELD_WIDTH})
+
+for tsv_format in tsv_output_adapter.supported_formats:
+    TabularOutputFormatter.register_new_formatter(
+        tsv_format, tsv_output_adapter.adapter,
+        tsv_output_adapter.preprocessors,
+        {'table_format': tsv_format, 'missing_value': '', 'max_field_width': None})
diff --git a/pgcli/packages/cli_helpers/tabular_output/preprocessors.py b/pgcli/packages/cli_helpers/tabular_output/preprocessors.py
new file mode 100644
index 0000000..37bb788
--- /dev/null
+++ b/pgcli/packages/cli_helpers/tabular_output/preprocessors.py
@@ -0,0 +1,266 @@
+# -*- coding: utf-8 -*-
+"""These preprocessor functions are used to process data prior to output."""
+
+import string
+
+from .. import utils
+from ..compat import (text_type, int_types, float_types,
+                                HAS_PYGMENTS, Terminal256Formatter, StringIO)
+
+
+def truncate_string(data, headers, max_field_width=None, skip_multiline_string=True, **_):
+    """Truncate very long strings. Only needed for tabular
+    representation, because trying to tabulate very long data
+    is problematic in terms of performance, and does not make any
+    sense visually.
+
+    :param iterable data: An :term:`iterable` (e.g. list) of rows.
+    :param iterable headers: The column headers.
+    :param int max_field_width: Width to truncate field for display
+    :return: The processed data and headers.
+    :rtype: tuple
+    """
+    return (([utils.truncate_string(v, max_field_width, skip_multiline_string) for v in row] for row in data),
+            [utils.truncate_string(h, max_field_width, skip_multiline_string) for h in headers])
+
+
+def convert_to_string(data, headers, **_):
+    """Convert all *data* and *headers* to strings.
+
+    Binary data that cannot be decoded is converted to a hexadecimal
+    representation via :func:`binascii.hexlify`.
+
+    :param iterable data: An :term:`iterable` (e.g. list) of rows.
+    :param iterable headers: The column headers.
+    :return: The processed data and headers.
+    :rtype: tuple
+
+    """
+    return (([utils.to_string(v) for v in row] for row in data),
+            [utils.to_string(h) for h in headers])
+
+
+def override_missing_value(data, headers, missing_value='', **_):
+    """Override missing values in the *data* with *missing_value*.
+
+    A missing value is any value that is :data:`None`.
+
+    :param iterable data: An :term:`iterable` (e.g. list) of rows.
+    :param iterable headers: The column headers.
+    :param missing_value: The default value to use for missing data.
+    :return: The processed data and headers.
+    :rtype: tuple
+
+    """
+    return (([missing_value if v is None else v for v in row] for row in data),
+            headers)
+
+
+def override_tab_value(data, headers, new_value='    ', **_):
+    """Override tab values in the *data* with *new_value*.
+
+    :param iterable data: An :term:`iterable` (e.g. list) of rows.
+    :param iterable headers: The column headers.
+    :param new_value: The new value to use for tab.
+    :return: The processed data and headers.
+    :rtype: tuple
+
+    """
+    return (([v.replace('\t', new_value) if isinstance(v, text_type) else v
+              for v in row] for row in data),
+            headers)
+
+
+def bytes_to_string(data, headers, **_):
+    """Convert all *data* and *headers* bytes to strings.
+
+    Binary data that cannot be decoded is converted to a hexadecimal
+    representation via :func:`binascii.hexlify`.
+
+    :param iterable data: An :term:`iterable` (e.g. list) of rows.
+    :param iterable headers: The column headers.
+    :return: The processed data and headers.
+    :rtype: tuple
+
+    """
+    return (([utils.bytes_to_string(v) for v in row] for row in data),
+            [utils.bytes_to_string(h) for h in headers])
+
+
+def align_decimals(data, headers, column_types=(), **_):
+    """Align numbers in *data* on their decimal points.
+
+    Whitespace padding is added before a number so that all numbers in a
+    column are aligned.
+
+    Outputting data before aligning the decimals::
+
+        1
+        2.1
+        10.59
+
+    Outputting data after aligning the decimals::
+
+         1
+         2.1
+        10.59
+
+    :param iterable data: An :term:`iterable` (e.g. list) of rows.
+    :param iterable headers: The column headers.
+    :param iterable column_types: The columns' type objects (e.g. int or float).
+    :return: The processed data and headers.
+    :rtype: tuple
+
+    """
+    pointpos = len(headers) * [0]
+    data = list(data)
+    for row in data:
+        for i, v in enumerate(row):
+            if column_types[i] is float and type(v) in float_types:
+                v = text_type(v)
+                pointpos[i] = max(utils.intlen(v), pointpos[i])
+
+    def results(data):
+        for row in data:
+            result = []
+            for i, v in enumerate(row):
+                if column_types[i] is float and type(v) in float_types:
+                    v = text_type(v)
+                    result.append((pointpos[i] - utils.intlen(v)) * " " + v)
+                else:
+                    result.append(v)
+            yield result
+
+    return results(data), headers
+
+
+def quote_whitespaces(data, headers, quotestyle="'", **_):
+    """Quote leading/trailing whitespace in *data*.
+
+    When outputing data with leading or trailing whitespace, it can be useful
+    to put quotation marks around the value so the whitespace is more
+    apparent. If one value in a column needs quoted, then all values in that
+    column are quoted to keep things consistent.
+
+    .. NOTE::
+       :data:`string.whitespace` is used to determine which characters are
+       whitespace.
+
+    :param iterable data: An :term:`iterable` (e.g. list) of rows.
+    :param iterable headers: The column headers.
+    :param str quotestyle: The quotation mark to use (defaults to ``'``).
+    :return: The processed data and headers.
+    :rtype: tuple
+
+    """
+    whitespace = tuple(string.whitespace)
+    quote = len(headers) * [False]
+    data = list(data)
+    for row in data:
+        for i, v in enumerate(row):
+            v = text_type(v)
+            if v.startswith(whitespace) or v.endswith(whitespace):
+                quote[i] = True
+
+    def results(data):
+        for row in data:
+            result = []
+            for i, v in enumerate(row):
+                quotation = quotestyle if quote[i] else ''
+                result.append('{quotestyle}{value}{quotestyle}'.format(
+                    quotestyle=quotation, value=v))
+            yield result
+    return results(data), headers
+
+
+def style_output(data, headers, style=None,
+                 header_token='Token.Output.Header',
+                 odd_row_token='Token.Output.OddRow',
+                 even_row_token='Token.Output.EvenRow', **_):
+    """Style the *data* and *headers* (e.g. bold, italic, and colors)
+
+    .. NOTE::
+        This requires the `Pygments <http://pygments.org/>`_ library to
+        be installed. You can install it with CLI Helpers as an extra::
+            $ pip install cli_helpers[styles]
+
+    Example usage::
+
+        from cli_helpers.tabular_output.preprocessors import style_output
+        from pygments.style import Style
+        from pygments.token import Token
+
+        class YourStyle(Style):
+            default_style = ""
+            styles = {
+                Token.Output.Header: 'bold ansibrightred',
+                Token.Output.OddRow: 'bg:#eee #111',
+                Token.Output.EvenRow: '#0f0'
+            }
+
+        headers = ('First Name', 'Last Name')
+        data = [['Fred', 'Roberts'], ['George', 'Smith']]
+
+        data, headers = style_output(data, headers, style=YourStyle)
+
+    :param iterable data: An :term:`iterable` (e.g. list) of rows.
+    :param iterable headers: The column headers.
+    :param str/pygments.style.Style style: A Pygments style. You can `create
+        your own styles <http://pygments.org/docs/styles/#creating-own-styles>`_.
+    :param str header_token: The token type to be used for the headers.
+    :param str odd_row_token: The token type to be used for odd rows.
+    :param str even_row_token: The token type to be used for even rows.
+    :return: The styled data and headers.
+    :rtype: tuple
+
+    """
+    if style and HAS_PYGMENTS:
+        formatter = Terminal256Formatter(style=style)
+
+        def style_field(token, field):
+            """Get the styled text for a *field* using *token* type."""
+            s = StringIO()
+            formatter.format(((token, field),), s)
+            return s.getvalue()
+
+        headers = [style_field(header_token, header) for header in headers]
+        data = ([style_field(odd_row_token if i % 2 else even_row_token, f)
+                 for f in r] for i, r in enumerate(data, 1))
+
+    return iter(data), headers
+
+
+def format_numbers(data, headers, column_types=(), integer_format=None,
+                   float_format=None, **_):
+    """Format numbers according to a format specification.
+
+    This uses Python's format specification to format numbers of the following
+    types: :class:`int`, :class:`py2:long` (Python 2), :class:`float`, and
+    :class:`~decimal.Decimal`. See the :ref:`python:formatspec` for more
+    information about the format strings.
+
+    .. NOTE::
+       A column is only formatted if all of its values are the same type
+       (except for :data:`None`).
+
+    :param iterable data: An :term:`iterable` (e.g. list) of rows.
+    :param iterable headers: The column headers.
+    :param iterable column_types: The columns' type objects (e.g. int or float).
+    :param str integer_format: The format string to use for integer columns.
+    :param str float_format: The format string to use for float columns.
+    :return: The processed data and headers.
+    :rtype: tuple
+
+    """
+    if (integer_format is None and float_format is None) or not column_types:
+        return iter(data), headers
+
+    def _format_number(field, column_type):
+        if integer_format and column_type is int and type(field) in int_types:
+            return format(field, integer_format)
+        elif float_format and column_type is float and type(field) in float_types:
+            return format(field, float_format)
+        return field
+
+    data = ([_format_number(v, column_types[i]) for i, v in enumerate(row)] for row in data)
+    return data, headers
diff --git a/pgcli/packages/cli_helpers/tabular_output/tabulate_adapter.py b/pgcli/packages/cli_helpers/tabular_output/tabulate_adapter.py
new file mode 100644
index 0000000..3e66594
--- /dev/null
+++ b/pgcli/packages/cli_helpers/tabular_output/tabulate_adapter.py
@@ -0,0 +1,98 @@
+# -*- coding: utf-8 -*-
+"""Format adapter for the tabulate module."""
+
+from __future__ import unicode_literals
+
+from ..utils import filter_dict_by_key
+from .preprocessors import (convert_to_string, truncate_string, override_missing_value,
+                            style_output, HAS_PYGMENTS, Terminal256Formatter, StringIO)
+
+import tabulate
+
+supported_markup_formats = ('mediawiki', 'html', 'latex', 'latex_booktabs',
+                            'textile', 'moinmoin', 'jira')
+supported_table_formats = ('plain', 'simple', 'grid', 'fancy_grid', 'pipe',
+                           'orgtbl', 'psql', 'rst')
+supported_formats = supported_markup_formats + supported_table_formats
+
+preprocessors = (override_missing_value, convert_to_string, truncate_string, style_output)
+
+
+def style_output_table(format_name=""):
+    def style_output(data, headers, style=None,
+                     table_separator_token='Token.Output.TableSeparator', **_):
+        """Style the *table* a(e.g. bold, italic, and colors)
+
+        .. NOTE::
+            This requires the `Pygments <http://pygments.org/>`_ library to
+            be installed. You can install it with CLI Helpers as an extra::
+                $ pip install cli_helpers[styles]
+
+        Example usage::
+
+            from cli_helpers.tabular_output import tabulate_adapter
+            from pygments.style import Style
+            from pygments.token import Token
+
+            class YourStyle(Style):
+                default_style = ""
+                styles = {
+                    Token.Output.TableSeparator: '#ansigray'
+                }
+
+            headers = ('First Name', 'Last Name')
+            data = [['Fred', 'Roberts'], ['George', 'Smith']]
+            style_output_table = tabulate_adapter.style_output_table('psql')
+            style_output_table(data, headers, style=CliStyle)
+
+            data, headers = style_output(data, headers, style=YourStyle)
+            output = tabulate_adapter.adapter(data, headers, style=YourStyle)
+
+        :param iterable data: An :term:`iterable` (e.g. list) of rows.
+        :param iterable headers: The column headers.
+        :param str/pygments.style.Style style: A Pygments style. You can `create
+            your own styles <http://pygments.org/docs/styles/#creating-own-styles>`_.
+        :param str table_separator_token: The token type to be used for the table separator.
+        :return: data and headers.
+        :rtype: tuple
+
+        """
+        if style and HAS_PYGMENTS and format_name in supported_table_formats:
+            formatter = Terminal256Formatter(style=style)
+
+            def style_field(token, field):
+                """Get the styled text for a *field* using *token* type."""
+                s = StringIO()
+                formatter.format(((token, field),), s)
+                return s.getvalue()
+
+            def addColorInElt(elt):
+                if not elt:
+                    return elt
+                if elt.__class__ == tabulate.Line:
+                    return tabulate.Line(*(style_field(table_separator_token, val) for val in elt))
+                if elt.__class__ == tabulate.DataRow:
+                    return tabulate.DataRow(*(style_field(table_separator_token, val) for val in elt))
+                return elt
+
+            srcfmt = tabulate._table_formats[format_name]
+            newfmt = tabulate.TableFormat(
+                *(addColorInElt(val) for val in srcfmt))
+            tabulate._table_formats[format_name] = newfmt
+
+        return iter(data), headers
+    return style_output
+
+def adapter(data, headers, table_format=None, preserve_whitespace=False,
+            **kwargs):
+    """Wrap tabulate inside a function for TabularOutputFormatter."""
+    keys = ('floatfmt', 'numalign', 'stralign', 'showindex', 'disable_numparse')
+    tkwargs = {'tablefmt': table_format}
+    tkwargs.update(filter_dict_by_key(kwargs, keys))
+
+    if table_format in supported_markup_formats:
+        tkwargs.update(numalign=None, stralign=None)
+
+    tabulate.PRESERVE_WHITESPACE = preserve_whitespace
+
+    return iter(tabulate.tabulate(data, headers, **tkwargs).split('\n'))
diff --git a/pgcli/packages/cli_helpers/tabular_output/terminaltables_adapter.py b/pgcli/packages/cli_helpers/tabular_output/terminaltables_adapter.py
new file mode 100644
index 0000000..270e2f4
--- /dev/null
+++ b/pgcli/packages/cli_helpers/tabular_output/terminaltables_adapter.py
@@ -0,0 +1,96 @@
+# -*- coding: utf-8 -*-
+"""Format adapter for the terminaltables module."""
+
+from __future__ import unicode_literals
+
+import terminaltables
+import itertools
+
+from ..utils import filter_dict_by_key
+from .preprocessors import (convert_to_string, truncate_string, override_missing_value,
+                            style_output, HAS_PYGMENTS, Terminal256Formatter, StringIO,
+                            override_tab_value)
+
+supported_formats = ('ascii', 'double', 'github')
+preprocessors = (
+    override_missing_value, convert_to_string, override_tab_value,
+    truncate_string, style_output
+)
+
+table_format_handler = {
+    'ascii': terminaltables.AsciiTable,
+    'double': terminaltables.DoubleTable,
+    'github': terminaltables.GithubFlavoredMarkdownTable,
+}
+
+
+def style_output_table(format_name=""):
+    def style_output(data, headers, style=None,
+                     table_separator_token='Token.Output.TableSeparator', **_):
+        """Style the *table* (e.g. bold, italic, and colors)
+
+        .. NOTE::
+            This requires the `Pygments <http://pygments.org/>`_ library to
+            be installed. You can install it with CLI Helpers as an extra::
+                $ pip install cli_helpers[styles]
+
+        Example usage::
+
+            from cli_helpers.tabular_output import terminaltables_adapter
+            from pygments.style import Style
+            from pygments.token import Token
+
+            class YourStyle(Style):
+                default_style = ""
+                styles = {
+                    Token.Output.TableSeparator: '#ansigray'
+                }
+
+            headers = ('First Name', 'Last Name')
+            data = [['Fred', 'Roberts'], ['George', 'Smith']]
+            style_output_table = terminaltables_adapter.style_output_table('psql')
+            style_output_table(data, headers, style=CliStyle)
+
+            output = terminaltables_adapter.adapter(data, headers, style=YourStyle)
+
+        :param iterable data: An :term:`iterable` (e.g. list) of rows.
+        :param iterable headers: The column headers.
+        :param str/pygments.style.Style style: A Pygments style. You can `create
+            your own styles <http://pygments.org/docs/styles/#creating-own-styles>`_.
+        :param str table_separator_token: The token type to be used for the table separator.
+        :return: data and headers.
+        :rtype: tuple
+
+        """
+        if style and HAS_PYGMENTS and format_name in supported_formats:
+            formatter = Terminal256Formatter(style=style)
+
+            def style_field(token, field):
+                """Get the styled text for a *field* using *token* type."""
+                s = StringIO()
+                formatter.format(((token, field),), s)
+                return s.getvalue()
+
+            clss = table_format_handler[format_name]
+            for char in [char for char in terminaltables.base_table.BaseTable.__dict__ if char.startswith("CHAR_")]:
+                setattr(clss, char, style_field(
+                    table_separator_token, getattr(clss, char)))
+
+        return iter(data), headers
+    return style_output
+
+
+def adapter(data, headers, table_format=None, **kwargs):
+    """Wrap terminaltables inside a function for TabularOutputFormatter."""
+    keys = ('title', )
+
+    table = table_format_handler[table_format]
+
+    t = table([headers] + list(data), **filter_dict_by_key(kwargs, keys))
+
+    dimensions = terminaltables.width_and_alignment.max_dimensions(
+        t.table_data,
+        t.padding_left,
+        t.padding_right)[:3]
+    for r in t.gen_table(*dimensions):
+        yield u''.join(r)
diff --git a/pgcli/packages/cli_helpers/tabular_output/tsv_output_adapter.py b/pgcli/packages/cli_helpers/tabular_output/tsv_output_adapter.py
new file mode 100644
index 0000000..69714f1
--- /dev/null
+++ b/pgcli/packages/cli_helpers/tabular_output/tsv_output_adapter.py
@@ -0,0 +1,16 @@
+# -*- coding: utf-8 -*-
+"""A tsv data output adapter"""
+
+from __future__ import unicode_literals
+
+from .preprocessors import bytes_to_string, override_missing_value, convert_to_string
+from itertools import chain
+from ..utils import replace
+
+supported_formats = ('tsv',)
+preprocessors = (override_missing_value, bytes_to_string, convert_to_string)
+
+def adapter(data, headers, **kwargs):
+    """Wrap the formatting inside a function for TabularOutputFormatter."""
+    for row in chain((headers,), data):
+        yield "\t".join((replace(r, (('\n', r'\n'), ('\t', r'\t'))) for r in row))
diff --git a/pgcli/packages/cli_helpers/tabular_output/vertical_table_adapter.py b/pgcli/packages/cli_helpers/tabular_output/vertical_table_adapter.py
new file mode 100644
index 0000000..4a44521
--- /dev/null
+++ b/pgcli/packages/cli_helpers/tabular_output/vertical_table_adapter.py
@@ -0,0 +1,66 @@
+# -*- coding: utf-8 -*-
+"""Format data into a vertical table layout."""
+
+from __future__ import unicode_literals
+
+from ..utils import filter_dict_by_key
+from .preprocessors import (convert_to_string, override_missing_value,
+                            style_output)
+
+supported_formats = ('vertical', )
+preprocessors = (override_missing_value, convert_to_string, style_output)
+
+
+def _get_separator(num, sep_title, sep_character, sep_length):
+    """Get a row separator for row *num*."""
+    left_divider_length = right_divider_length = sep_length
+    if isinstance(sep_length, tuple):
+        left_divider_length, right_divider_length = sep_length
+    left_divider = sep_character * left_divider_length
+    right_divider = sep_character * right_divider_length
+    title = sep_title.format(n=num + 1)
+
+    return "{left_divider}[ {title} ]{right_divider}\n".format(
+        left_divider=left_divider, right_divider=right_divider, title=title)
+
+
+def _format_row(headers, row):
+    """Format a row."""
+    formatted_row = [' | '.join(field) for field in zip(headers, row)]
+    return '\n'.join(formatted_row)
+
+
+def vertical_table(data, headers, sep_title='{n}. row', sep_character='*',
+                   sep_length=27):
+    """Format *data* and *headers* as an vertical table.
+
+    The values in *data* and *headers* must be strings.
+
+    :param iterable data: An :term:`iterable` (e.g. list) of rows.
+    :param iterable headers: The column headers.
+    :param str sep_title: The title given to each row separator. Defaults to
+                          ``'{n}. row'``. Any instance of ``'{n}'`` is
+                          replaced by the record number.
+    :param str sep_character: The character used to separate rows. Defaults to
+                              ``'*'``.
+    :param int/tuple sep_length: The number of separator characters that should
+                                 appear on each side of the *sep_title*. Use
+                                 a tuple to specify the left and right values
+                                 separately.
+    :return: The formatted data.
+    :rtype: str
+
+    """
+    header_len = max([len(x) for x in headers])
+    padded_headers = [x.ljust(header_len) for x in headers]
+    formatted_rows = [_format_row(padded_headers, row) for row in data]
+
+    output = []
+    for i, result in enumerate(formatted_rows):
+        yield _get_separator(i, sep_title, sep_character, sep_length) + result
+
+
+def adapter(data, headers, **kwargs):
+    """Wrap vertical table in a function for TabularOutputFormatter."""
+    keys = ('sep_title', 'sep_character', 'sep_length')
+    return vertical_table(data, headers, **filter_dict_by_key(kwargs, keys))
diff --git a/pgcli/packages/cli_helpers/utils.py b/pgcli/packages/cli_helpers/utils.py
new file mode 100644
index 0000000..5a5c032
--- /dev/null
+++ b/pgcli/packages/cli_helpers/utils.py
@@ -0,0 +1,70 @@
+# -*- coding: utf-8 -*-
+"""Various utility functions and helpers."""
+
+import binascii
+import re
+
+from .compat import binary_type, text_type
+
+
+def bytes_to_string(b):
+    """Convert bytes *b* to a string.
+
+    Hexlify bytes that can't be decoded.
+
+    """
+    if isinstance(b, binary_type):
+        try:
+            return b.decode('utf8')
+        except UnicodeDecodeError:
+            return '0x' + binascii.hexlify(b).decode('ascii')
+    return b
+
+
+def to_string(value):
+    """Convert *value* to a string."""
+    if isinstance(value, binary_type):
+        return bytes_to_string(value)
+    else:
+        return text_type(value)
+
+
+def truncate_string(value, max_width=None, skip_multiline_string=True):
+    """Truncate string values."""
+    if skip_multiline_string and isinstance(value, text_type) and '\n' in value:
+        return value
+    elif isinstance(value, text_type) and max_width is not None and len(value) > max_width:
+        return value[:max_width-3] + "..."
+    return value
+
+
+def intlen(n):
+    """Find the length of the integer part of a number *n*."""
+    pos = n.find('.')
+    return len(n) if pos < 0 else pos
+
+
+def filter_dict_by_key(d, keys):
+    """Filter the dict *d* to remove keys not in *keys*."""
+    return {k: v for k, v in d.items() if k in keys}
+
+
+def unique_items(seq):
+    """Return the unique items from iterable *seq* (in order)."""
+    seen = set()
+    return [x for x in seq if not (x in seen or seen.add(x))]
+
+
+_ansi_re = re.compile('\033\[((?:\d|;)*)([a-zA-Z])')
+
+
+def strip_ansi(value):
+    """Strip the ANSI escape sequences from a string."""
+    return _ansi_re.sub('', value)
+
+
+def replace(s, replace):
+    """Replace multiple values in a string"""
+    for r in replace:
+        s = s.replace(*r)
+    return s
diff --git a/pgcli/pgcompleter.py b/pgcli/pgcompleter.py
index b2a5783..3f1bbcf 100644
--- a/pgcli/pgcompleter.py
+++ b/pgcli/pgcompleter.py
@@ -3,7 +3,7 @@ import re
 from itertools import count, repeat, chain
 import operator
 from collections import namedtuple, defaultdict, OrderedDict
-from cli_helpers.tabular_output import TabularOutputFormatter
+from .packages.cli_helpers.tabular_output import TabularOutputFormatter
 from pgspecial.namedqueries import NamedQueries
 from prompt_toolkit.completion import Completer, Completion, PathCompleter
 from prompt_toolkit.document import Document