diff --git a/files/.i3/py3status/microphone.py b/files/.i3/py3status/microphone.py index f5bd184..a00391e 100644 --- a/files/.i3/py3status/microphone.py +++ b/files/.i3/py3status/microphone.py @@ -19,45 +19,65 @@ Colors: import subprocess +class NoMicrophone(Exception): + pass + + +class Error(Exception): + pass + + class Py3status: format_muted = '\uf131' # FontAwesome format_unmuted = '\uf130' # FontAwesome - class Error(Exception): - pass - def microphone(self): - isMute = self.__getMuteStatus__() - color = self.py3.COLOR_MUTE if isMute else self.py3.COLOR_UNMUTE - text = self.format_muted if isMute else self.format_unmuted - return { - 'full_text': text, - 'color': color, - } + try: + isMute = self.__getMuteStatus__() + color = self.py3.COLOR_MUTE if isMute else self.py3.COLOR_UNMUTE + text = self.format_muted if isMute else self.format_unmuted + return { + 'full_text': text, + 'color': color, + } + except NoMicrophone: + return { + 'full_text': '', + } + except Error: + return { + 'full_text': '...', + } def __getPaList__(self): try: paOutput = subprocess.check_output(["pacmd", "list-sources"]) - except CalledProcessError: + except subprocess.CalledProcessError: raise Error() return paOutput.decode() def __getMuteStatus__(self): - paList = self.__getPaList__() - fromCurrent = paList[paList.find('* index:'):] - MUTED_STR = 'muted: ' - fromMutestat = fromCurrent[fromCurrent.find(MUTED_STR) - + len(MUTED_STR):] - return fromMutestat[:3] == 'yes' + try: + paList = self.__getPaList__() + fromCurrent = paList[paList.index('* index:'):] + MUTED_STR = 'muted: ' + fromMutestat = fromCurrent[fromCurrent.index(MUTED_STR) + + len(MUTED_STR):] + return fromMutestat[:3] == 'yes' + except ValueError: + raise NoMicrophone() def __toggleMuteStatus__(self): - isMuted = self.__getMuteStatus__() - paList = self.__getPaList__() - fromCurrent = paList[paList.find('* index: ')+len('* index: '):] - sourceId = int(fromCurrent[:fromCurrent.find('\n')]) - subprocess.call(['pacmd', 'set-source-mute', str(sourceId), - str(int(not isMuted))]) + try: + isMuted = self.__getMuteStatus__() + paList = self.__getPaList__() + fromCurrent = paList[paList.index('* index: ')+len('* index: '):] + sourceId = int(fromCurrent[:fromCurrent.index('\n')]) + subprocess.call(['pacmd', 'set-source-mute', str(sourceId), + str(int(not isMuted))]) + except ValueError: + raise NoMicrophone() def on_click(self, event): self.__toggleMuteStatus__()