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

Shows/Hides the terminal cursor on start/exit. #61

Merged
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: 15 additions & 1 deletion termsaver.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,12 @@
# Python built-in modules
#
import sys
from signal import SIGINT, signal

from termsaverlib import common, constants, exception
from termsaverlib.helper.smartformatter import SmartFormatter
from termsaverlib.helper.utilities import (hide_stdout_cursor,
show_stdout_cursor)
from termsaverlib.i18n import _
#
# Internal modules
Expand Down Expand Up @@ -104,6 +107,11 @@ def usage():
def skip(arg = None):
pass

def handler(signal_received, frame):
# Handle any cleanup here
print('SIGINT or CTRL-C detected. Exiting gracefully')
show_stdout_cursor()
sys.exit(0)

def entryPoint():
tscreen = getScreen()
Expand Down Expand Up @@ -141,6 +149,7 @@ def getScreen():
verbose = True if args.verbose else False
if args.screen == None or (args.screen == None and args.h0elp == True):
usage()
show_stdout_cursor()
sys.exit(0)

# Find the screen we're using and create it's parser as well as check validity with args.
Expand All @@ -156,6 +165,7 @@ def getScreen():
if screen == None:
print(_("Invalid Screen."))
usage()
show_stdout_cursor()
sys.exit(0)

# Pass the parser to the selected screen.
Expand All @@ -177,6 +187,7 @@ def getScreen():
common.prettify_exception(e)

# Just finish gracefully
show_stdout_cursor()
sys.exit(0)

except exception.TermSaverException as e:
Expand Down Expand Up @@ -266,6 +277,7 @@ def getScreen():
if e.help_msg not in (None, ''):
print(e.help_msg)

show_stdout_cursor()
sys.exit(error_number)

except Exception as e:
Expand Down Expand Up @@ -297,11 +309,13 @@ def getScreen():
the bug report, that will help track faster the problem.
Thanks!
"""))

show_stdout_cursor()
sys.exit(errno.EPERM)
if __name__ == '__main__':
#
# The entry point of this application, as this should not be accessible as
# a python module to be imported by another application.
#
signal(SIGINT, handler)
hide_stdout_cursor()
entryPoint()
46 changes: 46 additions & 0 deletions termsaverlib/helper/utilities.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
###############################################################################
#
# file: utilities.py
#
# Purpose: refer to module documentation for details
#
# Note: This file is part of Termsaver application, and should not be used
# or executed separately.
#
###############################################################################
#
# Copyright 2012 Termsaver
#
# Licensed under the Apache License, Version 2.0 (the "License"); you may
# not use this file except in compliance with the License. You may obtain
# a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
# License for the specific language governing permissions and limitations
# under the License.
#
###############################################################################
import sys


def hide_stdout_cursor():
sys.stdout.write("\033[?25l")

def show_stdout_cursor():
sys.stdout.write('\033[?25h')

def hide_cursor():
"""
Hides the cursor.
"""
print("\033[?25l", end="")

def show_cursor():
"""
Shows the cursor.
"""
print('\033[?25h', end="")
3 changes: 3 additions & 0 deletions termsaverlib/screen/base/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,8 @@
import subprocess
import sys

from termsaverlib.helper.utilities import show_stdout_cursor

pynput_installed = None
reqs = subprocess.check_output([sys.executable, '-m', 'pip', 'freeze'])
installed_packages = [r.decode().split('==')[0] for r in reqs.split()]
Expand Down Expand Up @@ -245,6 +247,7 @@ def autorun(self, loop=True):
# Clear screen if appropriate
if self.cleanup_per_cycle:
self.clear_screen()
show_stdout_cursor()

def _run_cycle(self):
"""
Expand Down