summaryrefslogtreecommitdiffstats
path: root/libtoprammer/user_interface.py
blob: ae7385ed9772ac80c1103182f4f8546d7784e182 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"""
#    TOP2049 Open Source programming suite
#
#    Generic user interface support
#
#    Copyright (c) 2009-2010 Michael Buesch <m@bues.ch>
#
#    This program 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 2 of the License, or
#    (at your option) any later version.
#
#    This program 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 this program; if not, write to the Free Software Foundation, Inc.,
#    51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
"""

import sys


class AbstractUserInterface:
	"Abstract user interface class. Subclass it."

	# Progress meter IDs
	PROGRESSMETER_CHIPACCESS	= 0 # Programming/reading progress meter
	PROGRESSMETER_USER		= 100 # IDs equal or bigger than this are available to the app

	def progressMeterInit(self, meterId, message, nrSteps):
		pass # Do nothing by default

	def progressMeterFinish(self, meterId):
		pass # Do nothing by default

	def progressMeter(self, meterId, step):
		pass # Do nothing by default

	def warningMessage(self, message):
		pass # Do nothing by default

	def infoMessage(self, message):
		pass # Do nothing by default

	def debugMessage(self, message):
		pass # Do nothing by default

class ConsoleUserInterface(AbstractUserInterface):
	"Console based user interface."

	def progressMeterInit(self, meterId, message, nrSteps):
		if meterId != self.PROGRESSMETER_CHIPACCESS:
			return
		self.progressNrSteps = nrSteps
		self.progress = range(0, 100)
		self.__genericConsoleMessage(message + " [0%", newline=False)

	def progressMeterFinish(self, meterId):
		if meterId != self.PROGRESSMETER_CHIPACCESS:
			return
		if not self.progressNrSteps:
			self.progressMeter(meterId, 0)
		self.__genericConsoleMessage("100%]")

	def progressMeter(self, meterId, step):
		if meterId != self.PROGRESSMETER_CHIPACCESS:
			return
		if self.progressNrSteps:
			percent = (step * 100 // self.progressNrSteps)
		else:
			percent = 100
		for progress in list(self.progress):
			if progress > percent:
				break
			if progress == 25:
				self.__genericConsoleMessage("25%", newline=False)
			elif progress == 50:
				self.__genericConsoleMessage("50%", newline=False)
			elif progress == 75:
				self.__genericConsoleMessage("75%", newline=False)
			elif progress % 2 == 0:
				self.__genericConsoleMessage(".", newline=False)
			self.progress.remove(progress)

	def __genericConsoleMessage(self, message, newline=True):
		if newline:
			print message
		else:
			sys.stdout.write(message)
			sys.stdout.flush()

	def warningMessage(self, message):
		self.__genericConsoleMessage(message)

	def infoMessage(self, message):
		self.__genericConsoleMessage(message)

	def debugMessage(self, message):
		self.__genericConsoleMessage(message)
bues.ch cgit interface