diff --git a/pgcli/main.py b/pgcli/main.py index d49c86a66..af3f8458b 100644 --- a/pgcli/main.py +++ b/pgcli/main.py @@ -1301,21 +1301,24 @@ def get_completions(self, text, cursor_positition): return self.completer.get_completions(Document(text=text, cursor_position=cursor_positition), None) def get_prompt(self, string): - # should be before replacing \\d - string = string.replace("\\dsn_alias", self.dsn_alias or "") - string = string.replace("\\t", self.now.strftime("%x %X")) - string = string.replace("\\u", self.pgexecute.user or "(none)") - string = string.replace("\\H", self.pgexecute.host or "(none)") - string = string.replace("\\h", self.pgexecute.short_host or "(none)") - string = string.replace("\\d", self.pgexecute.dbname or "(none)") + # should be before replacing \d + def _to_str(val): + return val.decode("utf-8") if isinstance(val, bytes) else val or "" + + string = string.replace("\dsn_alias", self.dsn_alias or "") + string = string.replace("\t", self.now.strftime("%x %X")) + string = string.replace("\u", _to_str(self.pgexecute.user) or "(none)") + string = string.replace("\H", _to_str(self.pgexecute.host) or "(none)") + string = string.replace("\h", _to_str(self.pgexecute.short_host) or "(none)") + string = string.replace("\d", _to_str(self.pgexecute.dbname) or "(none)") string = string.replace( - "\\p", + "\p", str(self.pgexecute.port) if self.pgexecute.port is not None else "5432", ) - string = string.replace("\\i", str(self.pgexecute.pid) or "(none)") - string = string.replace("\\#", "#" if self.pgexecute.superuser else ">") - string = string.replace("\\n", "\n") - string = string.replace("\\T", self.pgexecute.transaction_indicator) + string = string.replace("\i", str(self.pgexecute.pid) or "(none)") + string = string.replace("\#", "#" if self.pgexecute.superuser else ">") + string = string.replace("\n", "\n") + string = string.replace("\T", _to_str(self.pgexecute.transaction_indicator)) return string def get_last_query(self): diff --git a/pgcli/pgcompleter.py b/pgcli/pgcompleter.py index ced0f1687..14b70d19b 100644 --- a/pgcli/pgcompleter.py +++ b/pgcli/pgcompleter.py @@ -141,6 +141,8 @@ def __init__(self, smart_completion=True, pgspecial=None, settings=None): self.all_completions = set(self.keywords + self.functions) def escape_name(self, name): + if isinstance(name, bytes): + name = name.decode("utf-8") if name and ((not self.name_pattern.match(name)) or (name.upper() in self.reserved_words) or (name.upper() in self.functions)): name = '"%s"' % name