diff --git a/src/eyed3/plugins/classic.py b/src/eyed3/plugins/classic.py index 36242ad8..de733c6c 100644 --- a/src/eyed3/plugins/classic.py +++ b/src/eyed3/plugins/classic.py @@ -28,7 +28,7 @@ from eyed3 import core, id3, mp3, utils, compat from eyed3.utils import makeUniqueFileName from eyed3.utils.console import (printMsg, printError, printWarning, boldText, - HEADER_COLOR, Fore) + HEADER_COLOR, Fore, getTtySize) from eyed3.id3.frames import ImageFrame from eyed3.utils.log import getLogger @@ -459,8 +459,9 @@ def handleFile(self, f): if not self.audio_file: return + self.terminal_width = getTtySize()[1] self.printHeader(f) - printMsg("-" * 79) + printMsg("-" * self.terminal_width) new_tag = False if (not self.audio_file.tag or @@ -518,7 +519,7 @@ def handleFile(self, f): except IOError as ex: printError(ex.message) - printMsg("-" * 79) + printMsg("-" * self.terminal_width) def printHeader(self, file_path): file_len = len(file_path) @@ -526,10 +527,10 @@ def printHeader(self, file_path): file_size = os.stat(file_path)[ST_SIZE] size_str = utils.formatSize(file_size) size_len = len(size_str) + 5 - if file_len + size_len >= 79: + if file_len + size_len >= self.terminal_width: file_path = "..." + file_path[-(75 - size_len):] file_len = len(file_path) - pat_len = 79 - file_len - size_len + pat_len = self.terminal_width - file_len - size_len printMsg("%s%s%s[ %s ]%s" % (boldText(file_path, c=HEADER_COLOR()), HEADER_COLOR(), " " * pat_len, size_str, Fore.RESET)) @@ -543,7 +544,7 @@ def printAudioInfo(self, info): "I" * info.mp3_header.layer, info.bit_rate_str, info.mp3_header.sample_freq, info.mp3_header.mode)) - printMsg("-" * 79) + printMsg("-" * self.terminal_width) def _getDefaultNameForObject(self, obj_frame, suffix=""): if obj_frame.filename: @@ -733,7 +734,7 @@ def printTag(self, tag): tag.terms_of_use)) if self.args.verbose: - printMsg("-" * 79) + printMsg("-" * self.terminal_width) printMsg("%d ID3 Frames:" % len(tag.frame_set)) for fid in tag.frame_set: frames = tag.frame_set[fid] diff --git a/src/eyed3/utils/console.py b/src/eyed3/utils/console.py index 3bf3829e..ee8bb40c 100644 --- a/src/eyed3/utils/console.py +++ b/src/eyed3/utils/console.py @@ -278,15 +278,8 @@ def __init__(self, total_or_items, file=None): self.update(0) def _handle_resize(self, signum=None, frame=None): - if self._should_handle_resize: - data = fcntl.ioctl(self._file, termios.TIOCGWINSZ, '\0' * 8) - terminal_width = struct.unpack("HHHH", data)[1] - else: - try: - terminal_width = int(os.environ.get('COLUMNS')) - except (TypeError, ValueError): - terminal_width = 78 - self._terminal_width = terminal_width + self._terminal_width = getTtySize(self._file, + self._should_handle_resize)[1] def __enter__(self): return self @@ -485,7 +478,7 @@ def _printWithColor(s, color, file): def cformat(msg, fg, bg=None, styles=None): - '''Formatt ``msg`` with foreground and optional background. Optional + '''Format ``msg`` with foreground and optional background. Optional ``styles`` lists will also be applied. The formatted string is returned.''' fg = fg or "" bg = bg or "" @@ -497,6 +490,19 @@ def cformat(msg, fg, bg=None, styles=None): return output +def getTtySize(fd=sys.stdout, check_tty=True): + if check_tty: + data = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 4) + hw = struct.unpack("hh", data) + else: + try: + hw = (int(os.environ.get('LINES')), + int(os.environ.get('COLUMNS'))) + except (TypeError, ValueError): + hw = (78, 25) + return hw + + def cprint(msg, fg, bg=None, styles=None, file=sys.stdout): '''Calls ``cformat`` and prints the result to output stream ``file``.''' print(cformat(msg, fg, bg=bg, styles=styles), file=file)