From patchwork Tue Jan 26 13:47:05 2016 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Paolo Bonzini X-Patchwork-Id: 573298 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Received: from lists.gnu.org (lists.gnu.org [IPv6:2001:4830:134:3::11]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 03C731402EC for ; Wed, 27 Jan 2016 01:09:56 +1100 (AEDT) Received: from localhost ([::1]:44141 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aO4Je-00027M-0p for incoming@patchwork.ozlabs.org; Tue, 26 Jan 2016 09:09:54 -0500 Received: from eggs.gnu.org ([2001:4830:134:3::10]:35374) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aO3yo-0000oW-1f for qemu-devel@nongnu.org; Tue, 26 Jan 2016 08:48:23 -0500 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1aO3yi-0004Ax-VY for qemu-devel@nongnu.org; Tue, 26 Jan 2016 08:48:21 -0500 Received: from mx1.redhat.com ([209.132.183.28]:36166) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1aO3yi-0004Af-Og for qemu-devel@nongnu.org; Tue, 26 Jan 2016 08:48:16 -0500 Received: from int-mx10.intmail.prod.int.phx2.redhat.com (int-mx10.intmail.prod.int.phx2.redhat.com [10.5.11.23]) by mx1.redhat.com (Postfix) with ESMTPS id 5E8D76E76E; Tue, 26 Jan 2016 13:48:16 +0000 (UTC) Received: from 640k.localdomain.com (ovpn-112-67.ams2.redhat.com [10.36.112.67]) by int-mx10.intmail.prod.int.phx2.redhat.com (8.14.4/8.14.4) with ESMTP id u0QDlNqi028272; Tue, 26 Jan 2016 08:48:15 -0500 From: Paolo Bonzini To: qemu-devel@nongnu.org Date: Tue, 26 Jan 2016 14:47:05 +0100 Message-Id: <1453816041-36362-34-git-send-email-pbonzini@redhat.com> In-Reply-To: <1453816041-36362-1-git-send-email-pbonzini@redhat.com> References: <1453816041-36362-1-git-send-email-pbonzini@redhat.com> X-Scanned-By: MIMEDefang 2.68 on 10.5.11.23 X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 209.132.183.28 Cc: Janosch Frank Subject: [Qemu-devel] [PULL 33/49] scripts/kvm/kvm_stat: Make tui function a class X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org Sender: qemu-devel-bounces+incoming=patchwork.ozlabs.org@nongnu.org From: Janosch Frank The tui function itself had a few sub-functions and therefore basically already was class-like. Making it an actual one with proper methods improved readability. The curses wrapper was dropped in favour of __entry/exit__ methods that implement the same behaviour. Also renamed single character variable name, so the name reflects the content. Signed-off-by: Janosch Frank Message-Id: <1452525484-32309-28-git-send-email-frankja@linux.vnet.ibm.com> Signed-off-by: Paolo Bonzini --- scripts/kvm/kvm_stat | 125 ++++++++++++++++++++++++++++++++------------------- 1 file changed, 80 insertions(+), 45 deletions(-) diff --git a/scripts/kvm/kvm_stat b/scripts/kvm/kvm_stat index 8efe3b8..63a657b 100755 --- a/scripts/kvm/kvm_stat +++ b/scripts/kvm/kvm_stat @@ -539,63 +539,97 @@ class Stats(object): LABEL_WIDTH = 40 NUMBER_WIDTH = 10 -def tui(screen, stats): - curses.use_default_colors() - curses.noecho() - drilldown = False - fields_filter = stats.fields_filter - def update_drilldown(): - if not fields_filter: - if drilldown: - stats.fields_filter = None +class Tui(object): + def __init__(self, stats): + self.stats = stats + self.screen = None + self.drilldown = False + self.fields_filter = self.stats.fields_filter + self.update_drilldown() + + def __enter__(self): + """Initialises curses for later use. Based on curses.wrapper + implementation from the Python standard library.""" + self.screen = curses.initscr() + curses.noecho() + curses.cbreak() + + # The try/catch works around a minor bit of + # over-conscientiousness in the curses module, the error + # return from C start_color() is ignorable. + try: + curses.start_color() + except: + pass + + curses.use_default_colors() + return self + + def __exit__(self, *exception): + """Resets the terminal to its normal state. Based on curses.wrappre + implementation from the Python standard library.""" + if self.screen: + self.screen.keypad(0) + curses.echo() + curses.nocbreak() + curses.endwin() + + def update_drilldown(self): + if not self.fields_filter: + if self.drilldown: + self.stats.fields_filter = None else: - stats.fields_filter = r'^[^\(]*$' - update_drilldown() - def refresh(sleeptime): - screen.erase() - screen.addstr(0, 0, 'kvm statistics') - screen.addstr(2, 1, 'Event') - screen.addstr(2, 1 + LABEL_WIDTH + NUMBER_WIDTH - len('Total'), 'Total') - screen.addstr(2, 1 + LABEL_WIDTH + NUMBER_WIDTH + 8 - len('Current'), 'Current') + self.stats.fields_filter = r'^[^\(]*$' + + def refresh(self, sleeptime): + self.screen.erase() + self.screen.addstr(0, 0, 'kvm statistics - summary', curses.A_BOLD) + self.screen.addstr(2, 1, 'Event') + self.screen.addstr(2, 1 + LABEL_WIDTH + NUMBER_WIDTH - + len('Total'), 'Total') + self.screen.addstr(2, 1 + LABEL_WIDTH + NUMBER_WIDTH + 8 - + len('Current'), 'Current') row = 3 - s = stats.get() + stats = self.stats.get() def sortkey(x): - if s[x][1]: - return (-s[x][1], -s[x][0]) + if stats[x][1]: + return (-stats[x][1], -stats[x][0]) else: - return (0, -s[x][0]) - for key in sorted(s.keys(), key=sortkey): - if row >= screen.getmaxyx()[0]: + return (0, -stats[x][0]) + for key in sorted(stats.keys(), key=sortkey): + + if row >= self.screen.getmaxyx()[0]: break - values = s[key] + values = stats[key] if not values[0] and not values[1]: break col = 1 - screen.addstr(row, col, key) + self.screen.addstr(row, col, key) col += LABEL_WIDTH - screen.addstr(row, col, '%10d' % (values[0],)) + self.screen.addstr(row, col, '%10d' % (values[0],)) col += NUMBER_WIDTH if values[1] is not None: - screen.addstr(row, col, '%8d' % (values[1] / sleeptime,)) + self.screen.addstr(row, col, '%8d' % (values[1] / sleeptime,)) row += 1 - screen.refresh() - - sleeptime = 0.25 - while True: - refresh(sleeptime) - curses.halfdelay(int(sleeptime * 10)) - sleeptime = 3 - try: - c = screen.getkey() - if c == 'x': - drilldown = not drilldown - update_drilldown() - if c == 'q': + self.screen.refresh() + + def show_stats(self): + sleeptime = 0.25 + while True: + self.refresh(sleeptime) + curses.halfdelay(int(sleeptime * 10)) + sleeptime = 3 + try: + char = self.screen.getkey() + if char == 'x': + self.drilldown = not self.drilldown + self.update_drilldown() + if char == 'q': + break + except KeyboardInterrupt: break - except KeyboardInterrupt: - break - except curses.error: - continue + except curses.error: + continue def batch(stats): s = stats.get() @@ -698,7 +732,8 @@ def main(): if options.log: log(stats) elif not options.once: - curses.wrapper(tui, stats) + with Tui(stats) as tui: + tui.show_stats() else: batch(stats)