glances_colorconsole.py 4.4 KB
Newer Older
1 2
# -*- coding: utf-8 -*-
#
3
# This file is part of Glances.
4
#
5
# Copyright (C) 2015 Nicolargo <nicolas@nicolargo.com>
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
#
# Glances is free software; you can redistribute it and/or modify
# it under the terms of the GNU Lesser General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Glances 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 Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import sys
A
Alessio Sergi 已提交
21
import threading
A
Alessio Sergi 已提交
22
import time
A
Alessio Sergi 已提交
23

A
Alessio Sergi 已提交
24
import msvcrt
A
Alessio Sergi 已提交
25

26 27
from glances.compat import queue
from glances.logger import logger
A
Alessio Sergi 已提交
28

29 30 31 32
try:
    import colorconsole
    import colorconsole.terminal
except ImportError:
A
Alessio Sergi 已提交
33
    logger.critical("Colorconsole module not found. Glances cannot start in standalone mode.")
34 35 36 37 38 39
    sys.exit(1)


class ListenGetch(threading.Thread):

    def __init__(self, nom=''):
A
Alessio Sergi 已提交
40
        super(ListenGetch, self).__init__()
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
        self.Terminated = False
        self.q = queue.Queue()

    def run(self):
        while not self.Terminated:
            char = msvcrt.getch()
            self.q.put(char)

    def stop(self):
        self.Terminated = True
        while not self.q.empty():
            self.q.get()

    def get(self, default=None):
        try:
            return ord(self.q.get_nowait())
        except Exception:
            return default


A
Alessio Sergi 已提交
61
class Screen(object):
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78

    COLOR_DEFAULT_WIN = '0F'  # 07'#'0F'
    COLOR_BK_DEFAULT = colorconsole.terminal.colors["BLACK"]
    COLOR_FG_DEFAULT = colorconsole.terminal.colors["WHITE"]

    def __init__(self, nc):
        self.nc = nc
        self.term = colorconsole.terminal.get_terminal()
        # os.system('color %s' % self.COLOR_DEFAULT_WIN)
        self.listen = ListenGetch()
        self.listen.start()

        self.term.clear()

    def subwin(self, x, y):
        return self

79
    def keypad(self, screen_id):
80 81
        return None

82
    def nodelay(self, screen_id):
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
        return None

    def getch(self):
        return self.listen.get(27)

    def erase(self):
        self.reset()
        return None

    def addnstr(self, y, x, msg, ln, typo=0):
        try:
            fgs, bks = self.nc.colors[typo]
        except Exception:
            fgs, bks = self.COLOR_FG_DEFAULT, self.COLOR_BK_DEFAULT
        self.term.set_color(fg=fgs, bk=bks)
        self.term.print_at(x, y, msg.ljust(ln))
        self.term.set_color(fg=self.COLOR_FG_DEFAULT, bk=self.COLOR_BK_DEFAULT)

    def getmaxyx(self):
        x = (self.term._Terminal__get_console_info().srWindow.Right -
             self.term._Terminal__get_console_info().srWindow.Left + 1)
        y = (self.term._Terminal__get_console_info().srWindow.Bottom -
             self.term._Terminal__get_console_info().srWindow.Top + 1)
        return [y, x]

    def reset(self):
        self.term.clear()
        self.term.reset()
        return None

    def restore_buffered_mode(self):
        self.term.restore_buffered_mode()
        return None


A
Alessio Sergi 已提交
118
class WCurseLight(object):
119 120 121 122 123 124 125 126 127

    COLOR_WHITE = colorconsole.terminal.colors["WHITE"]
    COLOR_RED = colorconsole.terminal.colors["RED"]
    COLOR_GREEN = colorconsole.terminal.colors["GREEN"]
    COLOR_BLUE = colorconsole.terminal.colors["LBLUE"]
    COLOR_MAGENTA = colorconsole.terminal.colors["LPURPLE"]
    COLOR_BLACK = colorconsole.terminal.colors["BLACK"]
    A_UNDERLINE = 0
    A_BOLD = 0
128
    A_PROTECT = 0
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
    COLOR_PAIRS = 9
    colors = {}

    def __init__(self):
        self.term = Screen(self)

    def initscr(self):
        return self.term

    def start_color(self):
        return None

    def use_default_colors(self):
        return None

    def noecho(self):
        return None

    def cbreak(self):
        return None

    def curs_set(self, y):
        return None

    def has_colors(self):
        return True

    def echo(self):
        return None

    def nocbreak(self):
        return None

    def endwin(self):
        self.term.reset()
        self.term.restore_buffered_mode()
        self.term.listen.stop()

    def napms(self, t):
        time.sleep(t / 1000 if t > 1000 else 1)

170 171
    def init_pair(self, color_id, fg, bk):
        self.colors[color_id] = [max(fg, 0), max(bk, 0)]
172

173 174
    def color_pair(self, color_id):
        return color_id