pyhw 0.15.1__py3-none-any.whl → 0.15.2__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
pyhw/__init__.py CHANGED
@@ -1,2 +1,2 @@
1
- __version__ = '0.15.1'
1
+ __version__ = '0.15.2'
2
2
  __author__ = 'xiaoran007'
@@ -4,6 +4,7 @@ from ..pyhwUtil import getOS
4
4
  from ..pyhwException import BackendException
5
5
  import subprocess
6
6
  import re
7
+ import os
7
8
 
8
9
 
9
10
  class Printer:
@@ -40,31 +41,91 @@ class Printer:
40
41
  print("\n".join(self.__combined_lines))
41
42
 
42
43
  def __dropLongString(self):
43
- # Need more accurate way to drop long strings
44
- if getOS() == "linux":
44
+ """
45
+ Truncate lines that exceed terminal width, accounting for ANSI escape sequences.
46
+ ANSI escape sequences don't contribute to visible width but are counted by len().
47
+ Enabled on both Linux and macOS.
48
+ """
49
+ if getOS() in ["linux", "macos"]:
45
50
  fixed_lines = list()
46
51
  for line in self.__combined_lines:
47
- if len(line) > self.__columns+20:
48
- fixed_lines.append(line[:self.__columns+20])
52
+ visible_length = self.__getVisibleLength(line)
53
+ if visible_length > self.__columns:
54
+ truncated_line = self.__truncateToWidth(line, self.__columns)
55
+ fixed_lines.append(truncated_line)
49
56
  else:
50
57
  fixed_lines.append(line)
51
58
  self.__combined_lines = fixed_lines
52
59
  else:
53
60
  pass
54
61
 
62
+ @staticmethod
63
+ def __getVisibleLength(text: str) -> int:
64
+ """
65
+ Calculate the visible length of a string, excluding ANSI escape sequences.
66
+ ANSI codes follow the pattern: \033[...m
67
+ """
68
+ ansi_pattern = re.compile(r'\033\[[0-9;]*m')
69
+ clean_text = ansi_pattern.sub('', text)
70
+ return len(clean_text)
71
+
72
+ @staticmethod
73
+ def __truncateToWidth(text: str, max_width: int) -> str:
74
+ """
75
+ Truncate a string to a maximum visible width while preserving ANSI escape sequences.
76
+ Ensures proper color reset at the end if truncated.
77
+ """
78
+ ansi_pattern = re.compile(r'(\033\[[0-9;]*m)')
79
+ parts = ansi_pattern.split(text)
80
+
81
+ result = []
82
+ visible_count = 0
83
+
84
+ for part in parts:
85
+ if ansi_pattern.match(part):
86
+ result.append(part)
87
+ else:
88
+ remaining = max_width - visible_count
89
+ if remaining <= 0:
90
+ break
91
+ if len(part) <= remaining:
92
+ result.append(part)
93
+ visible_count += len(part)
94
+ else:
95
+ result.append(part[:remaining])
96
+ visible_count += remaining
97
+ break
98
+
99
+ truncated = ''.join(result)
100
+ if not truncated.endswith('\033[0m'):
101
+ truncated += '\033[0m'
102
+
103
+ return truncated
104
+
55
105
  @staticmethod
56
106
  def __getColumns() -> int:
57
- if getOS() == "linux":
58
- try:
59
- result = subprocess.run(['stty', 'size'], capture_output=True, text=True)
60
- _, columns_str = result.stdout.split()
61
- columns = int(columns_str)
62
- except:
63
- columns = 80 # default terminal size is 80 columns
64
- else:
65
- # macOS default terminal size is 80 columns
66
- columns = 80
67
- return columns
107
+ try:
108
+ columns = os.get_terminal_size().columns
109
+ return columns
110
+ except (OSError, AttributeError):
111
+ pass
112
+
113
+ try:
114
+ result = subprocess.run(['stty', 'size'], capture_output=True, text=True, check=True)
115
+ _, columns_str = result.stdout.strip().split()
116
+ columns = int(columns_str)
117
+ return columns
118
+ except (subprocess.CalledProcessError, ValueError, FileNotFoundError):
119
+ pass
120
+
121
+ try:
122
+ columns = int(os.environ.get('COLUMNS', 0))
123
+ if columns > 0:
124
+ return columns
125
+ except (ValueError, TypeError):
126
+ pass
127
+
128
+ return 80
68
129
 
69
130
  def __LogoPreprocess(self):
70
131
  global_color = self.__config.get("colors")[0]
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.4
2
2
  Name: pyhw
3
- Version: 0.15.1
3
+ Version: 0.15.2
4
4
  Summary: PyHw, a neofetch-like command line tool for fetching system information but written mostly in python.
5
5
  Author: Xiao Ran
6
6
  Maintainer-email: Xiao Ran <xiaoran.007@icloud.com>
@@ -1,4 +1,4 @@
1
- pyhw/__init__.py,sha256=HOB6zppm7om1pAT4p00ZL8jZJeZ3GXNEQS3ktRh0PQU,49
1
+ pyhw/__init__.py,sha256=BQ_mh7NYOqC-G9WWF7YHQFKIPHIM0oXNr5NntIeUTUs,49
2
2
  pyhw/__main__.py,sha256=sycXv_HBzXuNET2w0-fRgMwHaWIsMALyJ_ZCxdZulDk,7696
3
3
  pyhw/backend/__init__.py,sha256=X1D1D28lSojDrUzUolgJvmbuctwBh_UxG3FwUeL8adA,51
4
4
  pyhw/backend/backendBase.py,sha256=mloo8mPEbgbIdmyQ3I4vdEXMSSjxWi_wnwACmzvHbEo,506
@@ -74,7 +74,7 @@ pyhw/backend/uptime/uptimeBase.py,sha256=HVRFZHO-2F_UKH0ti9wR16iHCH7Q8dga0HfxFxU
74
74
  pyhw/backend/uptime/uptimeInfo.py,sha256=TobPEV3MBT3Fiv3W6TOzD3a4MNW-vz2Oi_Trlcihu7k,114
75
75
  pyhw/backend/uptime/windows.py,sha256=JP6Co-V_fljOEMEkydNKDJ7OY7LGhtWf8IRLuIb6F4I,1742
76
76
  pyhw/frontend/__init__.py,sha256=jKkL3EvL069POGesru6FUlsMhW8AxiaVAErubWRFXE8,58
77
- pyhw/frontend/frontendBase.py,sha256=qRZOF5iTVgnzKBEKr4Kat1-Oowy-bRa6oOZu40ADUxo,5032
77
+ pyhw/frontend/frontendBase.py,sha256=IiSbJwWjj4H2CQEcTmFVuzoyUWpl0lx5AIiYm1nbOJ0,7072
78
78
  pyhw/frontend/color/__init__.py,sha256=uPtOM76b_b9F85AgYDyoOGNKeUHGpZnT752g_kCUVng,192
79
79
  pyhw/frontend/color/colorConfig.py,sha256=ReCR3IR6K1c28Tkf3uFg74mSzCMqXmUniegRiyGeq_w,7752
80
80
  pyhw/frontend/color/colorSet.py,sha256=spH8PlRu7capouf-yUgDHgoPCnM5aJ_ncascISZfz2g,1421
@@ -121,9 +121,9 @@ pyhw/pyhwUtil/cliUtil.py,sha256=IUcWun5nDwQb20Qe8YefS5j3Jji8a-F41Qd9QwCf0h0,2454
121
121
  pyhw/pyhwUtil/pciUtil.py,sha256=WAluDRDb-gUbqhvSIusFzPrf6r98EkrNEAwbPyMwrTc,202
122
122
  pyhw/pyhwUtil/pyhwUtil.py,sha256=XA1sQnAZmfA3QdmCzqiNWN4CDJKtnPK7N0j_1p3shiQ,8373
123
123
  pyhw/pyhwUtil/sysctlUtil.py,sha256=S-rUvqi7ZrMyMouIhxlyHEQ4agM7sCT1Y7uzs3Hu5-o,841
124
- pyhw-0.15.1.dist-info/licenses/LICENSE,sha256=hJs6RBqSVCexbTsalkMLNFI5t06kekQEsSVaOt_-yLs,1497
125
- pyhw-0.15.1.dist-info/METADATA,sha256=3OfrFEDp6Yd09Vs06NFoEqvIVoztuj240TKTsJIT-TQ,7715
126
- pyhw-0.15.1.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
127
- pyhw-0.15.1.dist-info/entry_points.txt,sha256=q-AB8im_QahpmNrmy4aPTJRGi0LlbNlnI3kF7s6pKss,44
128
- pyhw-0.15.1.dist-info/top_level.txt,sha256=7Inxvxt1TngEricKZEex9_WJZS3DbKYFUXDz4v5WHYU,5
129
- pyhw-0.15.1.dist-info/RECORD,,
124
+ pyhw-0.15.2.dist-info/licenses/LICENSE,sha256=hJs6RBqSVCexbTsalkMLNFI5t06kekQEsSVaOt_-yLs,1497
125
+ pyhw-0.15.2.dist-info/METADATA,sha256=qRrmoj6mZiYaeyWN2T4z6eewCAli1uUeMBUEmv7W33w,7715
126
+ pyhw-0.15.2.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
127
+ pyhw-0.15.2.dist-info/entry_points.txt,sha256=q-AB8im_QahpmNrmy4aPTJRGi0LlbNlnI3kF7s6pKss,44
128
+ pyhw-0.15.2.dist-info/top_level.txt,sha256=7Inxvxt1TngEricKZEex9_WJZS3DbKYFUXDz4v5WHYU,5
129
+ pyhw-0.15.2.dist-info/RECORD,,
File without changes