2012-05-04 11:40:30 +02:00
|
|
|
# coding: utf-8
|
|
|
|
#
|
2015-10-05 06:17:10 +02:00
|
|
|
# Copyright © 2012-2015 Ejwa Software. All rights reserved.
|
2012-05-04 11:40:30 +02:00
|
|
|
#
|
|
|
|
# This file is part of gitinspector.
|
|
|
|
#
|
|
|
|
# gitinspector is free software: you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# gitinspector is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with gitinspector. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2012-05-25 16:42:29 +02:00
|
|
|
from __future__ import print_function
|
2013-05-20 01:48:17 +02:00
|
|
|
import codecs
|
2012-05-04 15:15:41 +02:00
|
|
|
import os
|
2012-05-04 11:40:30 +02:00
|
|
|
import platform
|
2012-05-24 01:30:51 +02:00
|
|
|
import sys
|
2014-12-19 03:02:08 +01:00
|
|
|
import unicodedata
|
2012-05-04 11:40:30 +02:00
|
|
|
|
2012-05-04 15:15:41 +02:00
|
|
|
__bold__ = "\033[1m"
|
|
|
|
__normal__ = "\033[0;0m"
|
2012-05-04 11:40:30 +02:00
|
|
|
|
2013-07-23 01:08:53 +02:00
|
|
|
DEFAULT_TERMINAL_SIZE = (80, 25)
|
|
|
|
|
2012-05-04 11:40:30 +02:00
|
|
|
def __get_size_windows__():
|
2012-05-04 15:15:41 +02:00
|
|
|
res = None
|
2012-05-04 11:40:30 +02:00
|
|
|
try:
|
|
|
|
from ctypes import windll, create_string_buffer
|
|
|
|
|
2012-05-04 15:15:41 +02:00
|
|
|
handler = windll.kernel32.GetStdHandle(-12) # stderr
|
2012-05-04 11:40:30 +02:00
|
|
|
csbi = create_string_buffer(22)
|
2012-05-04 15:15:41 +02:00
|
|
|
res = windll.kernel32.GetConsoleScreenBufferInfo(handler, csbi)
|
2012-05-04 11:40:30 +02:00
|
|
|
except:
|
2013-07-23 01:08:53 +02:00
|
|
|
return DEFAULT_TERMINAL_SIZE
|
2012-05-04 11:40:30 +02:00
|
|
|
|
|
|
|
if res:
|
|
|
|
import struct
|
2012-05-04 15:15:41 +02:00
|
|
|
(_, _, _, _, _, left, top, right, bottom, _, _) = struct.unpack("hhhhHhhhhhh", csbi.raw)
|
2012-05-04 11:40:30 +02:00
|
|
|
sizex = right - left + 1
|
|
|
|
sizey = bottom - top + 1
|
|
|
|
return sizex, sizey
|
|
|
|
else:
|
2013-07-23 01:08:53 +02:00
|
|
|
return DEFAULT_TERMINAL_SIZE
|
2012-05-04 11:40:30 +02:00
|
|
|
|
|
|
|
def __get_size_linux__():
|
2012-05-04 15:15:41 +02:00
|
|
|
def ioctl_get_window_size(file_descriptor):
|
2012-05-04 11:40:30 +02:00
|
|
|
try:
|
2012-05-04 15:15:41 +02:00
|
|
|
import fcntl, termios, struct
|
2013-07-23 01:09:20 +02:00
|
|
|
size = struct.unpack('hh', fcntl.ioctl(file_descriptor, termios.TIOCGWINSZ, "1234"))
|
2012-05-04 11:40:30 +02:00
|
|
|
except:
|
2013-07-23 01:08:53 +02:00
|
|
|
return DEFAULT_TERMINAL_SIZE
|
2012-05-04 11:40:30 +02:00
|
|
|
|
2012-05-04 15:15:41 +02:00
|
|
|
return size
|
2012-05-04 11:40:30 +02:00
|
|
|
|
2012-05-04 15:15:41 +02:00
|
|
|
size = ioctl_get_window_size(0) or ioctl_get_window_size(1) or ioctl_get_window_size(2)
|
2012-05-04 11:40:30 +02:00
|
|
|
|
2012-05-04 15:15:41 +02:00
|
|
|
if not size:
|
2012-05-04 11:40:30 +02:00
|
|
|
try:
|
2012-05-04 15:15:41 +02:00
|
|
|
file_descriptor = os.open(os.ctermid(), os.O_RDONLY)
|
|
|
|
size = ioctl_get_window_size(file_descriptor)
|
|
|
|
os.close(file_descriptor)
|
2012-05-04 11:40:30 +02:00
|
|
|
except:
|
|
|
|
pass
|
2012-05-04 15:15:41 +02:00
|
|
|
if not size:
|
2012-05-04 11:40:30 +02:00
|
|
|
try:
|
2012-05-04 15:15:41 +02:00
|
|
|
size = (os.environ["LINES"], os.environ["COLUMNS"])
|
2012-05-04 11:40:30 +02:00
|
|
|
except:
|
2013-07-23 01:08:53 +02:00
|
|
|
return DEFAULT_TERMINAL_SIZE
|
2012-05-04 11:40:30 +02:00
|
|
|
|
2012-05-04 15:15:41 +02:00
|
|
|
return int(size[1]), int(size[0])
|
2012-05-04 11:40:30 +02:00
|
|
|
|
2012-05-18 04:33:10 +02:00
|
|
|
def clear_row():
|
2012-05-25 16:42:29 +02:00
|
|
|
print("\b" * 200, end="")
|
2012-05-18 04:33:10 +02:00
|
|
|
|
2012-05-04 11:40:30 +02:00
|
|
|
def skip_escapes(skip):
|
|
|
|
if skip:
|
2012-05-04 15:15:41 +02:00
|
|
|
global __bold__
|
|
|
|
global __normal__
|
|
|
|
__bold__ = ""
|
|
|
|
__normal__ = ""
|
2012-05-04 11:40:30 +02:00
|
|
|
|
|
|
|
def printb(string):
|
2012-05-25 16:42:29 +02:00
|
|
|
print(__bold__ + string + __normal__)
|
2012-05-04 11:40:30 +02:00
|
|
|
|
|
|
|
def get_size():
|
2013-05-06 00:47:51 +02:00
|
|
|
width = 0
|
|
|
|
height = 0
|
|
|
|
|
2012-05-24 01:30:51 +02:00
|
|
|
if sys.stdout.isatty():
|
|
|
|
current_os = platform.system()
|
|
|
|
|
2013-07-23 01:09:20 +02:00
|
|
|
if current_os == "Windows":
|
2013-05-06 00:47:51 +02:00
|
|
|
(width, height) = __get_size_windows__()
|
2013-07-23 01:09:20 +02:00
|
|
|
elif current_os == "Linux" or current_os == "Darwin" or current_os.startswith("CYGWIN"):
|
2013-05-06 00:47:51 +02:00
|
|
|
(width, height) = __get_size_linux__()
|
|
|
|
|
2013-05-06 11:17:09 +02:00
|
|
|
if width > 0:
|
2013-05-06 00:47:51 +02:00
|
|
|
return (width, height)
|
2012-05-24 01:30:51 +02:00
|
|
|
|
2013-07-23 01:08:53 +02:00
|
|
|
return DEFAULT_TERMINAL_SIZE
|
2013-05-20 01:48:17 +02:00
|
|
|
|
|
|
|
def set_stdout_encoding():
|
|
|
|
if not sys.stdout.isatty() and sys.version_info < (3,):
|
2013-07-23 01:09:20 +02:00
|
|
|
sys.stdout = codecs.getwriter("utf-8")(sys.stdout)
|
2013-07-14 03:10:00 +02:00
|
|
|
|
2014-01-08 06:05:10 +01:00
|
|
|
def set_stdin_encoding():
|
|
|
|
if not sys.stdin.isatty() and sys.version_info < (3,):
|
|
|
|
sys.stdin = codecs.getreader("utf-8")(sys.stdin)
|
|
|
|
|
2013-07-14 03:10:00 +02:00
|
|
|
def convert_command_line_to_utf8():
|
|
|
|
try:
|
|
|
|
argv = []
|
|
|
|
|
|
|
|
for arg in sys.argv:
|
|
|
|
argv.append(arg.decode(sys.stdin.encoding, "replace"))
|
|
|
|
|
2013-07-14 04:47:34 +02:00
|
|
|
return argv
|
2013-07-14 03:10:00 +02:00
|
|
|
except AttributeError:
|
2013-07-14 04:47:34 +02:00
|
|
|
return sys.argv
|
2014-01-08 06:05:10 +01:00
|
|
|
|
|
|
|
def check_terminal_encoding():
|
|
|
|
if sys.stdout.isatty() and (sys.stdout.encoding == None or sys.stdin.encoding == None):
|
|
|
|
print(_("WARNING: The terminal encoding is not correctly configured. gitinspector might malfunction. "
|
|
|
|
"The encoding can be configured with the environment variable 'PYTHONIOENCODING'."), file=sys.stderr)
|
2014-12-19 03:02:08 +01:00
|
|
|
|
|
|
|
def get_excess_column_count(string):
|
|
|
|
width_mapping = {'F': 2, 'H': 1, 'W': 2, 'Na': 1, 'N': 1, 'A': 1}
|
|
|
|
result = 0
|
|
|
|
|
2015-10-05 06:17:10 +02:00
|
|
|
for i in string:
|
|
|
|
width = unicodedata.east_asian_width(i)
|
|
|
|
result += width_mapping[width]
|
2014-12-19 03:02:08 +01:00
|
|
|
|
|
|
|
return result - len(string)
|
|
|
|
|
|
|
|
def ljust(string, pad):
|
|
|
|
return string.ljust(pad - get_excess_column_count(string))
|
|
|
|
|
|
|
|
def rjust(string, pad):
|
|
|
|
return string.rjust(pad - get_excess_column_count(string))
|