_terminal.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114
  1. # Copyright 2025 The HuggingFace Team. All rights reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. """Contains utilities to print stuff to the terminal (styling, helpers)."""
  15. import os
  16. import shutil
  17. import sys
  18. class StatusLine:
  19. """Minimal TTY status line for sync progress (stderr, single-line overwrite)."""
  20. def __init__(self, enabled: bool = True):
  21. self._active = enabled and sys.stderr.isatty()
  22. def update(self, msg: str) -> None:
  23. if not self._active:
  24. return
  25. width = shutil.get_terminal_size().columns
  26. if len(msg) > width - 1:
  27. msg = msg[: width - 4] + "..."
  28. sys.stderr.write(f"\r\033[K\033[90m{msg}\033[0m")
  29. sys.stderr.flush()
  30. def done(self, msg: str) -> None:
  31. if not self._active:
  32. return
  33. width = shutil.get_terminal_size().columns
  34. if len(msg) > width - 1:
  35. msg = msg[: width - 4] + "..."
  36. sys.stderr.write(f"\r\033[K\033[90m{msg}\033[0m\n")
  37. sys.stderr.flush()
  38. class ANSI:
  39. """
  40. Helper for en.wikipedia.org/wiki/ANSI_escape_code
  41. """
  42. _blue = "\u001b[34m"
  43. _bold = "\u001b[1m"
  44. _gray = "\u001b[90m"
  45. _green = "\u001b[32m"
  46. _red = "\u001b[31m"
  47. _reset = "\u001b[0m"
  48. _yellow = "\u001b[33m"
  49. @classmethod
  50. def blue(cls, s: str) -> str:
  51. return cls._format(s, cls._blue)
  52. @classmethod
  53. def bold(cls, s: str) -> str:
  54. return cls._format(s, cls._bold)
  55. @classmethod
  56. def gray(cls, s: str) -> str:
  57. return cls._format(s, cls._gray)
  58. @classmethod
  59. def green(cls, s: str) -> str:
  60. return cls._format(s, cls._green)
  61. @classmethod
  62. def red(cls, s: str) -> str:
  63. return cls._format(s, cls._bold + cls._red)
  64. @classmethod
  65. def yellow(cls, s: str) -> str:
  66. return cls._format(s, cls._yellow)
  67. @classmethod
  68. def _format(cls, s: str, code: str) -> str:
  69. if os.environ.get("NO_COLOR"):
  70. # See https://no-color.org/
  71. return s
  72. return f"{code}{s}{cls._reset}"
  73. def tabulate(
  74. rows: list[list[str | int]],
  75. headers: list[str],
  76. alignments: dict[str, str] | None = None,
  77. ) -> str:
  78. """
  79. Inspired by:
  80. - stackoverflow.com/a/8356620/593036
  81. - stackoverflow.com/questions/9535954/printing-lists-as-tabular-data
  82. """
  83. _ALIGN_MAP = {"left": "<", "right": ">"}
  84. for row in rows:
  85. if len(row) < len(headers):
  86. raise IndexError(f"Row has {len(row)} values but expected {len(headers)} (headers: {headers})")
  87. col_widths = [max(len(str(x)) for x in col) for col in zip(*rows, headers)]
  88. col_aligns = [_ALIGN_MAP.get((alignments or {}).get(h, "left"), "<") for h in headers]
  89. row_format = " ".join(f"{{:{a}{w}}}" for a, w in zip(col_aligns, col_widths))
  90. lines = []
  91. lines.append(row_format.format(*headers))
  92. lines.append(row_format.format(*["-" * w for w in col_widths]))
  93. for row in rows:
  94. lines.append(row_format.format(*row))
  95. return "\n".join(lines)