]> git.wh0rd.org Git - gnudebbugs.git/blob - src/gnudebbugs/ui/cli.py
initial release
[gnudebbugs.git] / src / gnudebbugs / ui / cli.py
1 # Copyright (C) 2021 Free Software Foundation, Inc.
2 #
3 # This program is free software: you can redistribute it and/or modify it under
4 # the terms of the GNU Lesser General Public License as published by the Free
5 # Software Foundation, either version 3 of the License, or at your option) any
6 # later version.
7 #
8 # This program is distributed in the hope that it will be useful, but WITHOUT
9 # ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
10 # FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
11 # details.
12 #
13 # You should have received a copy of the GNU Lesser General Public License
14 # along with this program.  If not, see <https://www.gnu.org/licenses/>.
15
16 """Command line interface UI."""
17
18 import os
19 from typing import Optional
20
21 from . import UIBase
22
23
24 class UI(UIBase):
25     """Command line interfacee."""
26
27     CSI_ERASE_LINE = "\x1b[2K"
28
29     def __init__(self):
30         self._clear_line_text = self.CSI_ERASE_LINE + "\r" if os.isatty(1) else "\n"
31
32     def osc8_link(self, url: str, text: str) -> str:
33         return f"\x1b]8;;{url}\x07{text}\x1b]8;;\x07"
34
35     def _clear_line(self, flush: bool = False):
36         print(self._clear_line_text, end='', flush=flush)
37
38     def status(self, text: Optional[str] = None) -> None:
39         """Print status information."""
40         self._clear_line()
41         if text:
42             print(text, end='', flush=True)
43
44     def interface_query(self, client, statuses, bug_width, summary_tags, tags_width, summary_states, states_width) -> None:
45         """Run the query interface."""
46         for bug, status in sorted(statuses.items()):
47             # Severity levels all happen to start with a diff first letter.
48             sev = status["severity"][0]
49             print(
50                 self.osc8_link(client.link(bug), f"{bug:{bug_width}}"),
51                 f"{sev}{summary_tags[bug]:{tags_width}}{summary_states[bug]:{states_width}}",
52                 status["subject"],
53             )