Skip to content

Commit 57910c3

Browse files
Merge 5eb87f4 into e928f02
2 parents e928f02 + 5eb87f4 commit 57910c3

1 file changed

Lines changed: 31 additions & 22 deletions

File tree

source/appModules/foobar2000.py

Lines changed: 31 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,31 +14,38 @@
1414
# A named tuple for holding the elapsed and total playing times from Foobar2000's status bar
1515
statusBarTimes = collections.namedtuple('StatusBarTimes', ['elapsed', 'total'])
1616

17-
def getParsingFormat(interval):
18-
"""Attempts to find a suitable parsing format string for a HH:MM:SS, MM:SS or SS -style time interval."""
19-
timeParts = len(interval.split(":"))
17+
18+
def parseIntervalToTimestamp(interval):
19+
"""Parses a D HH:MM:SS, HH:MM:SS, MM:SS or SS -style interval to a timestamp."""
20+
timeParts = len(interval.strip().replace(" ", ":").split(":"))
21+
# day is automatically set to 1
22+
# if no day is provided, remove 86400 seconds from parsed time
2023
if timeParts == 1:
21-
return "%S"
24+
return calendar.timegm(time.strptime(interval.strip(), "%S")) - 86400
2225
elif timeParts == 2:
23-
return "%M:%S"
26+
return calendar.timegm(time.strptime(interval.strip(), "%M:%S")) - 86400
2427
elif timeParts == 3:
25-
return "%H:%M:%S"
28+
return calendar.timegm(time.strptime(interval.strip(), "%H:%M:%S")) - 86400
29+
elif timeParts == 4:
30+
return calendar.timegm(time.strptime(interval.strip(), "%dd %H:%M:%S"))
2631
else:
2732
return None
2833

29-
def getOutputFormat(seconds):
30-
"""Returns a format string for the given number of seconds with the least leading zeros."""
31-
if seconds < 60:
32-
return "%S"
33-
elif seconds < 3600:
34-
return "%M:%S"
34+
35+
def getRemainingTime(parsedElapsedTime, parsedTotalTime):
36+
"""Returns the difference between two times."""
37+
remainingTime = parsedTotalTime - parsedElapsedTime
38+
if remainingTime < 60:
39+
return time.strftime("%S", time.gmtime(remainingTime))
40+
elif remainingTime < 3600:
41+
return time.strftime("%M:%S", time.gmtime(remainingTime))
42+
elif remainingTime < 86400:
43+
return time.strftime("%H:%M:%S", time.gmtime(remainingTime))
44+
elif remainingTime < 172800:
45+
return time.strftime("%#d day %H:%M:%S", time.gmtime(remainingTime - 86400))
3546
else:
36-
return "%H:%M:%S"
47+
return time.strftime("%#d days %H:%M:%S", time.gmtime(remainingTime - 86400))
3748

38-
def parseIntervalToTimestamp(interval):
39-
"""Parses a HH:MM:SS, MM:SS or SS -style interval to a timestamp."""
40-
format = getParsingFormat(interval)
41-
return calendar.timegm(time.strptime(interval.strip(), format))
4249

4350
class AppModule(appModuleHandler.AppModule):
4451
_statusBar = None
@@ -77,25 +84,27 @@ def script_reportRemainingTime(self,gesture):
7784
# Translators: Reported if the remaining time can not be calculated in Foobar2000
7885
msg = _("Unable to determine remaining time")
7986
else:
80-
parsedElapsedTime = parseIntervalToTimestamp(elapsedTime)
8187
parsedTotalTime = parseIntervalToTimestamp(totalTime)
82-
remainingTime = parsedTotalTime - parsedElapsedTime
83-
msg = time.strftime(getOutputFormat(remainingTime), time.gmtime(remainingTime))
88+
parsedElapsedTime = parseIntervalToTimestamp(elapsedTime)
89+
msg = getRemainingTime(parsedElapsedTime, parsedTotalTime) + " remaining"
8490
ui.message(msg)
8591
# Translators: The description of an NVDA command for reading the remaining time of the currently playing track in Foobar 2000.
8692
script_reportRemainingTime.__doc__ = _("Reports the remaining time of the currently playing track, if any")
8793

8894
def script_reportElapsedTime(self,gesture):
8995
elapsedTime = self.getElapsedAndTotalIfPlaying()[0]
9096
if elapsedTime is not None:
91-
ui.message(elapsedTime)
97+
ui.message(elapsedTime + " elapsed")
98+
else:
99+
# Translators: Reported if the elapsed time is not available in Foobar2000
100+
ui.message(_("Elapsed time not available"))
92101
# Translators: The description of an NVDA command for reading the elapsed time of the currently playing track in Foobar 2000.
93102
script_reportElapsedTime.__doc__ = _("Reports the elapsed time of the currently playing track, if any")
94103

95104
def script_reportTotalTime(self,gesture):
96105
totalTime = self.getElapsedAndTotalIfPlaying()[1]
97106
if totalTime is not None:
98-
ui.message(totalTime)
107+
ui.message(totalTime + " total")
99108
else:
100109
# Translators: Reported if the total time is not available in Foobar2000
101110
ui.message(_("Total time not available"))

0 commit comments

Comments
 (0)