Commits

Anonymous committed 9921677

Support colorful output and fix option handling.

Comments (0)

Files changed (1)

smart-live-rebuild.py

 rebuilds = {}
 
 class out:
+	red = '\033[1;31m'
+	green = '\033[32m'
+	lime = '\033[1;32m'
+	yellow = '\033[1;33m'
+	cyan = '\033[36m'
+	turq = '\033[1;36m'
+	white = '\033[1;37m'
+	reset = '\033[0m'
+
+	s1reset = lime
+	s2reset = green
+	s3reset = cyan
+	errreset = yellow
+
+	@classmethod
+	def monochromize(self):
+		for k in dir(self):
+			if not k.startswith('_'):
+				v = getattr(self, k)
+				if isinstance(v, str) and v.startswith('\033'):
+					setattr(self, k, '')
+
 	@classmethod
 	def s1(self, msg):
-		self.out('*** %s\n' % msg)
+		self.out('%s*** %s%s\n' % (self.s1reset, msg, self.reset))
 	@classmethod
 	def s2(self, msg):
-		self.out('->  %s\n' % msg)
+		self.out('%s->%s  %s\n' % (self.s2reset, self.reset, msg))
 	@classmethod
 	def s3(self, msg):
-		self.out('--> %s\n' % msg)
+		self.out('%s-->%s %s\n' % (self.s3reset, self.reset, msg))
 
 	@classmethod
 	def err(self, msg):
-		self.out('!!! %s\n' % msg)
+		self.out('%s!!!%s %s%s%s\n' % (self.red, self.reset, self.errreset, msg, self.reset))
 
 	@staticmethod
 	def out(msg):
 			newrev = self.getrev()
 
 			if oldrev == newrev:
-				out.s3('at rev %s (not updated)' % oldrev)
+				out.s3('at rev %s%s%s (no changes)' % (out.green, oldrev, out.reset))
 				return False
 			else:
-				out.s3('update from %s to %s' % (oldrev, newrev))
+				out.s3('update from %s%s%s to %s%s%s' % (out.green, oldrev, out.reset, out.lime, newrev, out.reset))
 				return True
 
 	def __unicode__(self):
 			version='%%prog %s' % PV,
 			description='Enumerate all live packages in system, check their repositories for updates and remerge the updated ones.'
 	)
+	opt.add_option('-C', '--no-color', action='store_false', dest='color', default=True,
+		help='Disable colorful output')
 	opt.add_option('-p', '--pretend', action='store_true', dest='pretend', default=False,
 		help='Only print a list of the packages which were updated; do not call emerge to rebuild them.')
 	opt.add_option('-R', '--record', action='store_true', dest='record', default=False,
 		help='Omit passing --oneshot option to portage, and thus add updated packages to the @world set.')
 	(opts, args) = opt.parse_args(argv[1:])
 
+	if not opts.color:
+		out.monochromize()
+
 	out.s1('Enumerating packages ...')
 
 	for cpv in db.cpv_all():
 
 	if len(packages) < 1:
 		out.s1('No updates found')
-	elif opts['pretend']:
+	elif opts.pretend:
 		out.s1('Printing list of updated packages ...')
 		for p in packages:
 			print p
 	else:
-		out.s1('Calling emerge to rebuild %d packages ...' % len(packages))
+		out.s1('Calling emerge to rebuild %s%d%s packages ...' % (out.white, len(packages), out.s1reset))
 		cmd = ['emerge']
-		if not opts['record']:
+		if not opts.record:
 			cmd.append('--oneshot')
 		cmd.extend(args)
 		cmd.extend(['=%s' % x for x in packages])