Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented 'quit on any input' option. #56

Merged
merged 3 commits into from
Jan 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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