Skip to content

Commit

Permalink
Merge pull request #56 from EddieDover/any-key-quits_v2
Browse files Browse the repository at this point in the history
Implemented 'quit on any input' option.
  • Loading branch information
EddieDover committed Jan 23, 2023
2 parents 0543207 + af617c2 commit 67e0bb8
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 13 deletions.
16 changes: 10 additions & 6 deletions termsaver.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,22 @@
* `parse_args` function
"""

import argparse
import errno
#
# Python built-in modules
#
import sys
import errno

from termsaverlib import common, constants, exception
from termsaverlib.helper.smartformatter import SmartFormatter
from termsaverlib.i18n import _
#
# Internal modules
#
from termsaverlib.screen import build_screen_usage_list, get_available_screens
from termsaverlib.screen.base import ScreenBase
from termsaverlib.screen.helper import ScreenHelperBase
from termsaverlib import common, exception, constants
from termsaverlib.i18n import _

import argparse
from termsaverlib.helper.smartformatter import SmartFormatter

verbose = False
"""
Expand Down Expand Up @@ -87,6 +86,11 @@ def usage():
-h, --help Displays this help message
-v, --verbose Displays python exception errors (for debugging)
Enhanced Features:
* Install the following modules to enable enhanced features:
* pynput - Enables the 'Press any key to exit' feature.
* pygments - Colorizes the output of the Programmer screen.
Refer also to each screen's help by typing: %(app_name)s [screen] -h
""") % {
'app_name': constants.App.NAME,
Expand Down
44 changes: 37 additions & 7 deletions termsaverlib/screen/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -82,18 +82,22 @@
#
# Python built-in modules
#
import os
import getopt
import subprocess
import sys

pynput_installed = None
reqs = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze'])
installed_packages = [r.decode().split('==')[0] for r in reqs.split()]
if 'pynput' in installed_packages:
pynput_installed = True
from pynput import keyboard

#
# Internal modules
#
from termsaverlib import common, constants, exception
from termsaverlib.screen.helper import ScreenHelperBase
from termsaverlib import constants
from termsaverlib.i18n import _
from termsaverlib.helper.smartformatter import SmartFormatter
import argparse
from termsaverlib.screen.helper import ScreenHelperBase


class ScreenBase(ScreenHelperBase):
Expand Down Expand Up @@ -177,6 +181,26 @@ def __init__(self, name, description, parser=None):

self.name = name
self.description = description

if pynput_installed is not None:
self.listener = keyboard.Listener(
on_press=self.on_press,
on_release=self.on_release
)

def on_press(self, key):
"""
This method is called when a key is pressed.
"""
if pynput_installed is not None:
self.listener.stop()

def on_release(self, key):
"""
This method is called when a key is released.
Unused for now, but leaving it in so we have options in the future.
"""
pass

def autorun(self, loop=True):
"""
Expand All @@ -202,7 +226,13 @@ def autorun(self, loop=True):
# execute the cycle
self.clear_screen()

while(loop):
if pynput_installed is not None:
self.listener.start()
while(
(loop and pynput_installed is None)
or
(loop and pynput_installed is not None and self.listener.is_alive())
):
try:
self._run_cycle()
except KeyboardInterrupt as e:
Expand Down

0 comments on commit 67e0bb8

Please sign in to comment.