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

Remove dependency on cursor #58

Merged
merged 7 commits into from
Feb 25, 2019
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
12 changes: 0 additions & 12 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

35 changes: 35 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ default encoding:

* ``vistir.compat.fs_str``
* ``vistir.compat.to_native_string``
* ``vistir.compat.fs_encode``
* ``vistir.compat.fs_decode``


🐉 Context Managers
Expand Down Expand Up @@ -291,6 +293,39 @@ be reset upon exiting the context.
['/home/user/.pyenv/versions/3.7.0/bin', '/home/user/.pyenv/versions/3.7.0/lib/python37.zip', '/home/user/.pyenv/versions/3.7.0/lib/python3.7', '/home/user/.pyenv/versions/3.7.0/lib/python3.7/lib-dynload', '/home/user/.pyenv/versions/3.7.0/lib/python3.7/site-packages']


🐉 Cursor Utilities
--------------------------

The following Cursor utilities are available to manipulate the console cursor:

* ``vistir.cursor.hide_cursor``
* ``vistir.cursor.show_cursor``


.. _`hide_cursor`:

**hide_cursor**
/////////////////

Hide the console cursor in the given stream.

.. code:: python

>>> vistir.cursor.hide_cursor(stream=sys.stdout)


.. _`show_cursor`:

**show_cursor**
/////////////////

Show the console cursor in the given stream.

.. code:: python

>>> vistir.cursor.show_cursor(stream=sys.stdout)


🐉 Miscellaneous Utilities
--------------------------

Expand Down
1 change: 1 addition & 0 deletions news/57.feature.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added a custom cursor hiding implementation to avoid depending on the cursor library, which was re-released under the GPL.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
[build-system]
requires = ['setuptools>=36.2.2', 'wheel>=0.28.0']
requires = ['setuptools>=40.8.0', 'wheel>=0.33.0']

[tool.towncrier]
package = 'vistir'
package_dir = 'src'
filename = 'CHANGELOG.rst'
newsfile = 'CHANGELOG.rst'
directory = 'news/'
title_format = '{version} ({project_date})'
issue_format = '`#{issue} <https://github.com/sarugaku/vistir/issues/{issue}>`_'
Expand Down
1 change: 0 additions & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ install_requires =

[options.extras_require]
spinner =
cursor
yaspin
tests =
pytest
Expand Down
5 changes: 4 additions & 1 deletion src/vistir/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
spinner,
replaced_stream
)
from .cursor import show_cursor, hide_cursor
from .misc import (
load_path,
partialclass,
Expand Down Expand Up @@ -67,5 +68,7 @@
"StringIO",
"get_wrapped_stream",
"StreamWrapper",
"replaced_stream"
"replaced_stream",
"show_cursor",
"hide_cursor"
]
76 changes: 76 additions & 0 deletions src/vistir/cursor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
# -*- coding=utf-8 -*-
from __future__ import absolute_import, print_function

import ctypes
import os
import sys


__all__ = ["hide_cursor", "show_cursor"]


class CONSOLE_CURSOR_INFO(ctypes.Structure):
_fields_ = [('dwSize', ctypes.c_int),
('bVisible', ctypes.c_int)]


WIN_STDERR_HANDLE_ID = ctypes.c_ulong(-12)
WIN_STDOUT_HANDLE_ID = ctypes.c_ulong(-11)


def get_stream_handle(stream=sys.stdout):
"""
Get the OS appropriate handle for the corresponding output stream.

:param str stream: The the stream to get the handle for
:return: A handle to the appropriate stream, either a ctypes buffer
or **sys.stdout** or **sys.stderr**.
"""
handle = stream
if os.name == "nt":
from ctypes import windll
handle_id = WIN_STDOUT_HANDLE_ID
handle = windll.kernel32.GetStdHandle(handle_id)
return handle


def hide_cursor(stream=sys.stdout):
"""
Hide the console cursor on the given stream

:param stream: The name of the stream to get the handle for
:return: None
:rtype: None
"""

handle = get_stream_handle(stream=stream)
if os.name == "nt":
from ctypes import windll
cursor_info = CONSOLE_CURSOR_INFO()
windll.kernel32.GetConsoleCursorInfo(handle, ctypes.byref(cursor_info))
cursor_info.visible = False
windll.kernel32.SetConsoleCursorInfo(handle, ctypes.byref(cursor_info))
else:
handle.write("\033[?25l")
handle.flush()


def show_cursor(stream=sys.stdout):
"""
Show the console cursor on the given stream

:param stream: The name of the stream to get the handle for
:return: None
:rtype: None
"""

handle = get_stream_handle(stream=stream)
if os.name == "nt":
from ctypes import windll
cursor_info = CONSOLE_CURSOR_INFO()
windll.kernel32.GetConsoleCursorInfo(handle, ctypes.byref(cursor_info))
cursor_info.visible = True
windll.kernel32.SetConsoleCursorInfo(handle, ctypes.byref(cursor_info))
else:
handle.write("\033[?25h")
handle.flush()
7 changes: 3 additions & 4 deletions src/vistir/spin.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,17 @@
import six

from .compat import to_native_string
from .cursor import hide_cursor, show_cursor
from .termcolors import COLOR_MAP, COLORS, colored, DISABLE_COLORS
from .misc import decode_for_output
from io import StringIO

try:
import yaspin
import cursor
except ImportError:
yaspin = None
Spinners = None
SpinBase = None
cursor = None
else:
import yaspin.spinners
import yaspin.core
Expand Down Expand Up @@ -421,13 +420,13 @@ def _reset_signal_handlers(self):
def _hide_cursor(target=None):
if not target:
target = sys.stdout
cursor.hide(stream=target)
hide_cursor(stream=target)

@staticmethod
def _show_cursor(target=None):
if not target:
target = sys.stdout
cursor.show(stream=target)
show_cursor(stream=target)

@staticmethod
def _clear_err():
Expand Down