Skip to content

Commit c0927e4

Browse files
committed
Merge pull request coderholic#16 from stac47/experimental
Cleanup + rolling station selection
2 parents 8092a02 + b31ce3f commit c0927e4

File tree

1 file changed

+28
-29
lines changed

1 file changed

+28
-29
lines changed

pyradio/radio.py

+28-29
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
from . import player
1414

1515
import locale
16-
locale.setlocale(locale.LC_ALL,"")
16+
locale.setlocale(locale.LC_ALL, "")
1717

1818

1919
logger = logging.getLogger(__name__)
@@ -82,7 +82,7 @@ def setupAndDrawScreen(self):
8282
def initHead(self):
8383
from pyradio import version
8484

85-
info = " PyRadio %s " % version
85+
info = " PyRadio {0} ".format(version)
8686
self.headWin.addstr(0, 0, info, curses.color_pair(4))
8787
rightStr = "www.coderholic.com/pyradio"
8888
self.headWin.addstr(0, self.maxX - len(rightStr) - 1, rightStr,
@@ -106,35 +106,30 @@ def initFooter(self):
106106
def refreshBody(self):
107107
self.bodyWin.erase()
108108
self.bodyWin.box()
109-
110109
self.bodyWin.move(1, 1)
111110
maxDisplay = self.bodyMaxY - 1
112-
for idx in range(maxDisplay - 1):
113-
if(idx > maxDisplay):
114-
break
115-
try:
116-
station = self.stations[idx + self.startPos]
117-
col = curses.color_pair(5)
118-
119-
if idx + self.startPos == self.selection and \
120-
self.selection == self.playing:
121-
col = curses.color_pair(9)
122-
self.bodyWin.hline(idx + 1, 1, ' ', self.bodyMaxX - 2, col)
123-
elif idx + self.startPos == self.selection:
124-
col = curses.color_pair(6)
125-
self.bodyWin.hline(idx + 1, 1, ' ', self.bodyMaxX - 2, col)
126-
elif idx + self.startPos == self.playing:
127-
col = curses.color_pair(4)
128-
self.bodyWin.hline(idx + 1, 1, ' ', self.bodyMaxX - 2, col)
129-
self.bodyWin.addstr(idx + 1, 1,
130-
"%2.d. %s" % (idx + self.startPos + 1,
131-
station[0]), col)
132-
133-
except IndexError:
134-
break
135-
111+
for lineNum in range(maxDisplay - 1):
112+
i = lineNum + self.startPos
113+
if i < len(self.stations):
114+
self.__displayBodyLine(lineNum, self.stations[i])
136115
self.bodyWin.refresh()
137116

117+
def __displayBodyLine(self, lineNum, station):
118+
col = curses.color_pair(5)
119+
120+
if lineNum + self.startPos == self.selection and \
121+
self.selection == self.playing:
122+
col = curses.color_pair(9)
123+
self.bodyWin.hline(lineNum + 1, 1, ' ', self.bodyMaxX - 2, col)
124+
elif lineNum + self.startPos == self.selection:
125+
col = curses.color_pair(6)
126+
self.bodyWin.hline(lineNum + 1, 1, ' ', self.bodyMaxX - 2, col)
127+
elif lineNum + self.startPos == self.playing:
128+
col = curses.color_pair(4)
129+
self.bodyWin.hline(lineNum + 1, 1, ' ', self.bodyMaxX - 2, col)
130+
line = "{0}. {1}".format(lineNum + self.startPos + 1, station[0])
131+
self.bodyWin.addstr(lineNum + 1, 1, line, col)
132+
138133
def run(self):
139134

140135
if not self.play is False:
@@ -155,8 +150,12 @@ def run(self):
155150

156151
def setStation(self, number):
157152
""" Select the given station number """
158-
number = max(0, number)
159-
number = min(number, len(self.stations) - 1)
153+
# If we press up at the first station, we go to the last one
154+
# and if we press down on the last one we go back to the first one.
155+
if number < 0:
156+
number = len(self.stations) - 1
157+
elif number >= len(self.stations):
158+
number = 0
160159

161160
self.selection = number
162161

0 commit comments

Comments
 (0)