content.py
generate_playlist_display()
¶
Generate list of playlists.
Source code in mps_youtube/content.py
def generate_playlist_display():
""" Generate list of playlists. """
if not g.ytpls:
g.message = c.r + "No playlists found!"
return logo(c.g) + "\n\n"
g.rprompt = page_msg(g.current_page)
cw = getxy().width
fmtrow = "%s%-5s %s %-12s %-8s %-2s%s\n"
fmthd = "%s%-5s %-{}s %-12s %-9s %-5s%s\n".format(cw - 36)
head = (c.ul, "Item", "Playlist", "Author", "Updated", "Count", c.w)
out = "\n" + fmthd % head
for n, x in enumerate(g.ytpls):
col = (c.g if n % 2 == 0 else c.w)
length = x.get('size') or "?"
length = "%4s" % length
title = x.get('title') or "unknown"
author = x.get('author') or "unknown"
updated = yt_datetime(x.get('updated'))[1]
title = uea_pad(cw - 36, title)
out += (fmtrow % (col, str(n + 1), title, author[:12], updated, str(length), c.w))
return out + "\n" * (5 - len(g.ytpls))
generate_songlist_display(song=False, zeromsg=None)
¶
Generate list of choices from a song list.
Source code in mps_youtube/content.py
def generate_songlist_display(song=False, zeromsg=None):
""" Generate list of choices from a song list."""
# pylint: disable=R0914
if g.browse_mode == "ytpl":
return generate_playlist_display()
max_results = getxy().max_results
if not g.model:
g.message = zeromsg or "Enter /search-term to search or [h]elp"
return logo(c.g) + "\n\n"
g.rprompt = page_msg(g.current_page)
have_meta = all(x.ytid in g.meta for x in g.model)
user_columns = _get_user_columns() if have_meta else []
maxlength = max(x.length for x in g.model)
lengthsize = 8 if maxlength > 35999 else 7
lengthsize = 6 if maxlength < 6000 else lengthsize
reserved = 9 + lengthsize + len(user_columns)
cw = getxy().width
cw -= 1
title_size = cw - sum(1 + x['size'] for x in user_columns) - reserved
before = [{"name": "idx", "size": 3, "heading": "Num"},
{"name": "title", "size": title_size, "heading": "Title"}]
after = [{"name": "length", "size": lengthsize, "heading": "Length"}]
columns = before + user_columns + after
for n, column in enumerate(columns):
column['idx'] = n
column['sign'] = "-" if not column['name'] == "length" else ""
fmt = ["%{}{}s ".format(x['sign'], x['size']) for x in columns]
fmtrow = fmt[0:1] + ["%s "] + fmt[2:]
fmt, fmtrow = "".join(fmt).strip(), "".join(fmtrow).strip()
titles = tuple([x['heading'][:x['size']] for x in columns])
hrow = c.ul + fmt % titles + c.w
out = "\n" + hrow + "\n"
for n, x in enumerate(g.model[:max_results]):
col = (c.r if n % 2 == 0 else c.p) if not song else c.b
details = {'title': x.title, "length": fmt_time(x.length)}
details = copy.copy(g.meta[x.ytid]) if have_meta else details
otitle = details['title']
details['idx'] = "%2d" % (n + 1)
details['title'] = uea_pad(columns[1]['size'], otitle)
#cat = details.get('category') or '-'
#details['category'] = 'pafy.get_categoryname(cat)'
details['ytid'] = x.ytid
line = ''
for z in columns:
fieldsize, field, direction = z['size'], z['name'], "<" if z['sign'] == "-" else ">"
line += uea_pad(fieldsize, details[field], direction)
if not columns[-1] == z:
line += " "
col = col if not song or song != g.model[n] else c.p
line = col + line + c.w
out += line + "\n"
return out + "\n" * (5 - len(g.model)) if not song else out
logo(col=None, version='')
¶
Return text logo.
Source code in mps_youtube/content.py
def logo(col=None, version=""):
""" Return text logo. """
col = col if col else random.choice((c.g, c.r, c.y, c.b, c.p, c.w))
logo_txt = r""" _ _
| | | |
_ _ _____ _| |_ _ _| |__ ___
| | | |/ _ \ \ /\ / / __| | | | '_ \ / _ \
| |_| | __/\ V V /| |_| |_| | |_) | __/
\__, |\___| \_/\_/ \__|\__,_|_.__/ \___|
__/ |
|___/
"""
version = " v" + version if version else ""
logo_txt = col + logo_txt + c.w + version
lines = logo_txt.split("\n")
length = max(len(x) for x in lines)
x, y, _ = getxy()
indent = (x - length - 1) // 2
newlines = (y - 12) // 2
indent, newlines = (0 if x < 0 else x for x in (indent, newlines))
lines = [" " * indent + l for l in lines]
logo_txt = "\n".join(lines) + "\n" * newlines
return "" if g.debug_mode or g.no_textart else logo_txt
page_msg(page=0)
¶
Format information about currently displayed page to a string.
Source code in mps_youtube/content.py
def page_msg(page=0):
""" Format information about currently displayed page to a string. """
if isinstance(g.content, PaginatedContent):
page_count = g.content.numPages()
else:
page_count = math.ceil(g.result_count/getxy().max_results)
if page_count > 1:
pagemsg = "{}{}/{}{}"
#start_index = max_results * g.current_page
return pagemsg.format('<' if page > 0 else '[',
"%s%s%s" % (c.y, page+1, c.w),
page_count,
'>' if page + 1 < page_count else ']')
return None
playlists_display()
¶
Produce a list of all playlists.
Source code in mps_youtube/content.py
def playlists_display():
""" Produce a list of all playlists. """
if not g.userpl:
g.message = F("no playlists")
return generate_songlist_display() if g.model else (logo(c.y) + "\n\n")
maxname = max(len(a) for a in g.userpl)
out = " {0}Local Playlists{1}\n".format(c.ul, c.w)
start = " "
fmt = "%s%s%-3s %-" + str(maxname + 3) + "s%s %s%-7s%s %-5s%s"
head = (start, c.b, "ID", "Name", c.b, c.b, "Count", c.b, "Duration", c.w)
out += "\n" + fmt % head + "\n\n"
for v, z in enumerate(sorted(g.userpl)):
n, p = z, g.userpl[z]
l = fmt % (start, c.g, v + 1, n, c.w, c.y, str(len(p)), c.y,
p.duration, c.w) + "\n"
out += l
return out