Skip to content

Commit

Permalink
Make the classic output span the actual width of the tty so you can s…
Browse files Browse the repository at this point in the history
…ee the actual path with a long file name. (#92)

* Make the classic output span the actual width of the tty so you can see the actual path with a long file name.

* Handle when underneath tests and no tty.

* Listed the particular exceptions to pass on
  • Loading branch information
redshodan authored and nicfit committed Sep 26, 2017
1 parent 6fd295a commit 3e99546
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 17 deletions.
15 changes: 8 additions & 7 deletions src/eyed3/plugins/classic.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -518,18 +519,18 @@ 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)
from stat import ST_SIZE
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))
Expand All @@ -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:
Expand Down Expand Up @@ -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]
Expand Down
30 changes: 20 additions & 10 deletions src/eyed3/utils/console.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 ""
Expand All @@ -497,6 +490,23 @@ def cformat(msg, fg, bg=None, styles=None):
return output


def getTtySize(fd=sys.stdout, check_tty=True):
hw = None
if check_tty:
try:
data = fcntl.ioctl(fd, termios.TIOCGWINSZ, '\0' * 4)
hw = struct.unpack("hh", data)
except (OSError, IOError):
pass
if not hw:
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)
Expand Down

0 comments on commit 3e99546

Please sign in to comment.