Standardize function calls

This commit is contained in:
Nguyễn Gia Phong 2017-03-26 15:26:37 +07:00
parent 082cdc3222
commit 861c2e4612
3 changed files with 79 additions and 62 deletions

2
.gitignore vendored Normal file
View file

@ -0,0 +1,2 @@
build
__pycache__

107
comp.py
View file

@ -1,5 +1,21 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
# comp - Curses Online Media Player
# Copyright (C) 2017 Raphael McSinyx
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
import curses import curses
import json import json
from argparse import ArgumentParser from argparse import ArgumentParser
@ -22,8 +38,8 @@ def mpv_wrapper(media, video=True):
del player del player
def reattr(stdscr, y): def reattr(stdscr, y, track):
track = DATA[start + y - 1] track = data[start + y - 1]
invert = 8 if track['highlight'] else 0 invert = 8 if track['highlight'] else 0
if track['error']: if track['error']:
stdscr.chgat(y, 0, curses.color_pair(1 + invert) | curses.A_BOLD) stdscr.chgat(y, 0, curses.color_pair(1 + invert) | curses.A_BOLD)
@ -37,15 +53,16 @@ def reattr(stdscr, y):
stdscr.chgat(y, 0, curses.color_pair(0) | curses.A_NORMAL) stdscr.chgat(y, 0, curses.color_pair(0) | curses.A_NORMAL)
def reprint(stdscr): def reprint(stdscr, data2print):
stdscr.clear() stdscr.clear()
stdscr.addstr(0, curses.COLS-12, 'URL') stdscr.addstr(0, curses.COLS-12, 'URL')
stdscr.addstr(0, 0, 'Title') stdscr.addstr(0, 0, 'Title')
stdscr.chgat(0, 0, curses.color_pair(10) | curses.A_BOLD) stdscr.chgat(0, 0, curses.color_pair(10) | curses.A_BOLD)
for i, d in enumerate(DATA[start : start+curses.LINES-3]): for i, d in enumerate(data2print):
stdscr.addstr(i + 1, 0, d['url'].rjust(curses.COLS - 1)) y = i + 1
stdscr.addstr(i + 1, 0, d['title'][:curses.COLS-12]) stdscr.addstr(y, 0, d['url'].rjust(curses.COLS - 1))
reattr(stdscr, i + 1) stdscr.addstr(y, 0, d['title'][:curses.COLS-12])
reattr(stdscr, y, data[start + i])
stdscr.addstr( stdscr.addstr(
curses.LINES - 2, curses.LINES - 2,
curses.COLS - 16, curses.COLS - 16,
@ -55,41 +72,41 @@ def reprint(stdscr):
stdscr.refresh() stdscr.refresh()
def move(stdscr, y, delta): def move(stdscr, data, y, delta):
global start global start
reattr(stdscr, y) reattr(stdscr, y, data[start + y - 1])
if start + y + delta < 1: if start + y + delta < 1:
start = 0 start = 0
reprint(stdscr) reprint(stdscr, data[:curses.LINES-3])
stdscr.move(stdscr, 1, 0) stdscr.move(1, 0)
DATA[0]['highlight'] = True data[0]['highlight'] = True
reattr(stdscr, 1) reattr(stdscr, 1, data[start])
DATA[0]['highlight'] = False data[0]['highlight'] = False
return 1 return 1
elif start + y + delta > len(DATA): elif start + y + delta > len(data):
start = len(DATA) - curses.LINES + 3 start = len(data) - curses.LINES + 3
reprint(stdscr) reprint(stdscr, data[-curses.LINES+3:])
y = curses.LINES - 3 y = curses.LINES - 3
stdscr.move(stdscr, y, 0) stdscr.move(y, 0)
DATA[-1]['highlight'] = True data[-1]['highlight'] = True
reattr(stdscr, y) reattr(stdscr, y, data[start + y - 1])
DATA[-1]['highlight'] = False data[-1]['highlight'] = False
return y return y
if 0 < y + delta < curses.LINES - 2: if 0 < y + delta < curses.LINES - 2:
y = y + delta y = y + delta
elif y + delta < 1: elif y + delta < 1:
start += y + delta - 1 start += y + delta - 1
reprint(stdscr) reprint(stdscr, data[start : start+curses.LINES-3])
y = 1 y = 1
else: else:
start += y + delta - curses.LINES + 3 start += y + delta - curses.LINES + 3
reprint(stdscr) reprint(stdscr, data[start : start+curses.LINES-3])
y = curses.LINES - 3 y = curses.LINES - 3
stdscr.move(stdscr, y, 0) stdscr.move(y, 0)
DATA[start + y - 1]['highlight'] = True data[start + y - 1]['highlight'] = True
reattr(stdscr, y) reattr(stdscr, y, data[start + y - 1])
DATA[start + y - 1]['highlight'] = False data[start + y - 1]['highlight'] = False
stdscr.refresh() stdscr.refresh()
return y return y
@ -107,8 +124,8 @@ selected = config.getboolean('Runtime', 'play-selected-only', fallback=False)
video = config.getboolean('Runtime', 'video', fallback=True) video = config.getboolean('Runtime', 'video', fallback=True)
with open(args.json_playlist) as f: with open(args.json_playlist) as f:
DATA = json.load(f) data = json.load(f)
for i in DATA: for i in data:
i['error'] = False i['error'] = False
i['playing'] = False i['playing'] = False
i['selected'] = False i['selected'] = False
@ -139,37 +156,37 @@ curses.init_pair(14, -1, 6)
# Print initial content # Print initial content
start = 0 start = 0
reprint(stdscr) reprint(stdscr, data[:curses.LINES-3])
y = 1 y = 1
DATA[0]['highlight'] = True data[0]['highlight'] = True
stdscr.move(stdscr, 1, 0) stdscr.move(1, 0)
reattr(stdscr, 1) reattr(stdscr, 1, data[start])
DATA[0]['highlight'] = False data[0]['highlight'] = False
c = stdscr.getch() c = stdscr.getch()
while c != 113: # letter q while c != 113: # letter q
if c == curses.KEY_RESIZE: if c == curses.KEY_RESIZE:
curses.update_lines_cols() curses.update_lines_cols()
reprint(stdscr) reprint(stdscr, data[start : start+curses.LINES-3])
y = 1 y = 1
reattr(stdscr, y) reattr(stdscr, 1, data[start])
elif c in (ord('j'), curses.KEY_DOWN): elif c in (ord('j'), curses.KEY_DOWN):
y = move(stdscr, y, 1) y = move(stdscr, data, y, 1)
elif c in (ord('k'), curses.KEY_UP): elif c in (ord('k'), curses.KEY_UP):
y = move(stdscr, y, -1) y = move(stdscr, data, y, -1)
elif c == curses.KEY_PPAGE: elif c == curses.KEY_PPAGE:
y = move(stdscr, y, -curses.LINES) y = move(stdscr, data, y, -curses.LINES)
elif c == curses.KEY_NPAGE: elif c == curses.KEY_NPAGE:
y = move(stdscr, y, curses.LINES) y = move(stdscr, data, y, curses.LINES)
elif c == curses.KEY_HOME: elif c == curses.KEY_HOME:
y = move(stdscr, y, -len(DATA)) y = move(stdscr, data, y, -len(data))
elif c == curses.KEY_END: elif c == curses.KEY_END:
y = move(stdscr, y, len(DATA)) y = move(stdscr, data, y, len(data))
elif c == ord(' '): elif c == ord(' '):
DATA[start + y - 1]['selected'] = not DATA[start + y - 1]['selected'] data[start + y - 1]['selected'] = not data[start + y - 1]['selected']
y = move(stdscr, y, 1) y = move(stdscr, data, y, 1)
elif c == ord('x'): # temporally behavior elif c == ord('x'): # temporally behavior
mpv_wrapper('https://youtu.be/' + DATA[start + y - 1]['url'], video) mpv_wrapper('https://youtu.be/' + data[start + y - 1]['url'], video)
stdscr.refresh() stdscr.refresh()
c = stdscr.getch() c = stdscr.getch()

View file

@ -5,23 +5,21 @@ from distutils.core import setup
with open('README.rst') as f: with open('README.rst') as f:
long_description = f.read() long_description = f.read()
setup(name = 'comp', setup(name = 'comp', version = '0.1.0a1',
version = '0.1.0a1',
url = 'https://github.com/McSinyx/comp', url = 'https://github.com/McSinyx/comp',
description = ('Curses Online Media Player'), description = ('Curses Online Media Player'),
long_description=long_description, long_description=long_description,
author = 'McSinyx', author = 'McSinyx', author_email = 'vn.mcsinyx@gmail.com',
author_email = 'vn.mcsinyx@gmail.com', py_modules = ['mpv'], scripts=['comp.py'],
py_modules = ['mpv'], classifiers = [
scripts=['comp.py'], 'Development Status :: 1 - Planning',
classifiers = ['Development Status :: 1 - Planning', 'Environment :: Console :: Curses',
'Environment :: Console :: Curses', 'Intended Audience :: End Users/Desktop',
'Intended Audience :: End Users/Desktop', 'License :: OSI Approved :: GNU Affero General Public License v3',
'Natural Language :: English', 'Natural Language :: English',
'Natural Language :: Vietnamese', # planned 'Natural Language :: Vietnamese', # planned
'Operating System :: POSIX', 'Operating System :: POSIX',
'Programming Language :: Python :: 3.5', 'Programming Language :: Python :: 3.5',
'Topic :: Multimedia :: Sound/Audio :: Players', 'Topic :: Multimedia :: Sound/Audio :: Players',
'Topic :: Multimedia :: Video :: Display'], 'Topic :: Multimedia :: Video :: Display'
license = 'AGPLv2' ], license = 'AGPLv3')
)