| 1857 | |
| 1858 | |
| 1859 | def format_output(title, cur, headers, status, settings, explain_mode=False): |
| 1860 | output = [] |
| 1861 | expanded = settings.expanded or settings.table_format == "vertical" |
| 1862 | table_format = "vertical" if settings.expanded else settings.table_format |
| 1863 | max_width = settings.max_width |
| 1864 | case_function = settings.case_function |
| 1865 | if explain_mode: |
| 1866 | formatter = ExplainOutputFormatter(max_width or 100) |
| 1867 | else: |
| 1868 | formatter = TabularOutputFormatter(format_name=table_format) |
| 1869 | |
| 1870 | def format_array(val): |
| 1871 | if val is None: |
| 1872 | return settings.missingval |
| 1873 | if not isinstance(val, list): |
| 1874 | return val |
| 1875 | return "{" + ",".join(str(format_array(e)) for e in val) + "}" |
| 1876 | |
| 1877 | def format_arrays(data, headers, **_): |
| 1878 | data = list(data) |
| 1879 | for row in data: |
| 1880 | row[:] = [format_array(val) if isinstance(val, list) else val for val in row] |
| 1881 | |
| 1882 | return data, headers |
| 1883 | |
| 1884 | def format_status(cur, status): |
| 1885 | # redshift does not return rowcount as part of status. |
| 1886 | # See https://github.com/dbcli/pgcli/issues/1320 |
| 1887 | if cur and hasattr(cur, "rowcount") and cur.rowcount is not None: |
| 1888 | if status and not status.endswith(str(cur.rowcount)): |
| 1889 | status += " %s" % cur.rowcount |
| 1890 | return status |
| 1891 | |
| 1892 | output_kwargs = { |
| 1893 | "sep_title": "RECORD {n}", |
| 1894 | "sep_character": "-", |
| 1895 | "sep_length": (1, 25), |
| 1896 | "missing_value": settings.missingval, |
| 1897 | "integer_format": settings.dcmlfmt, |
| 1898 | "float_format": settings.floatfmt, |
| 1899 | "column_date_formats": settings.column_date_formats, |
| 1900 | "preprocessors": (format_numbers, format_arrays), |
| 1901 | "disable_numparse": True, |
| 1902 | "preserve_whitespace": True, |
| 1903 | "style": settings.style_output, |
| 1904 | "max_field_width": settings.max_field_width, |
| 1905 | } |
| 1906 | if not settings.floatfmt: |
| 1907 | output_kwargs["preprocessors"] = (align_decimals,) |
| 1908 | |
| 1909 | if settings.column_date_formats: |
| 1910 | output_kwargs["preprocessors"] += (format_timestamps,) |
| 1911 | |
| 1912 | if table_format == "csv": |
| 1913 | # The default CSV dialect is "excel" which is not handling newline values correctly |
| 1914 | # Nevertheless, we want to keep on using "excel" on Windows since it uses '\r\n' |
| 1915 | # as the line terminator |
| 1916 | # https://github.com/dbcli/pgcli/issues/1102 |