soxofaan / duviz

Command-line disk space usage visualization utility

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Add support for html colors

Gunni opened this issue · comments

Depends on #9 ofcourse

Basically i'd like to pipe the output of this script to something called xymon.

It could be neat!

You mean rendering to HTML markup and colors? or just HTML colors (not sure what that would mean though)?

Here is an example from a script I made:

Screenshot from 2019-11-01 16-56-10

Basically the script outputs plain text but with the span tags around stuff that needs colors.

The script logic is literally if shell then shell sequences, else if html then html span and for both it has a close tag thing/shell reset sequence.

Example code:

class ColorFormatter(string.Formatter):
	colors = None

	def __init__(self, enabled) -> None:
		self.enabled = enabled != None
		self.type = enabled

		if self.enabled and self.type == 'shell':
			self.tags = {
				'red': '\033[31m',
				'yellow': '\033[33m',
				'grey': '\033[2m',
				'done': '\033[m',
			}
		elif self.type == 'html':
			self.tags = {
				'red': '<span style="color: red">',
				'yellow': '<span style="color: yellow">',
				'grey': '<span style="color: grey">',
				'done': '</span>',
			}

	def get_value(self, key, args, kwds) -> str:
		# Return the unmodified key still in braces if not a string key
		if not isinstance(key, str):
			return '{{{}}}'.format(key)

		# If we are not enabled always return no color for specified key
		if not self.enabled:
			return ''

		try:
			return self.tags[key]
		except KeyError as e:
			if key[0] == '/':
				return self.tags['done']

			raise e

def Color(s) -> str:
	colorKind = None

	if sys.stdout.isatty():
		colorKind = 'shell'

	if args.html or args.xymon:
		colorKind = 'html'

	return ColorFormatter(colorKind).format(s)

print(Color('{grey}example{/grey}'))

This may be accomplised by piping the output into aha from https://github.com/theZiz/aha (also available in several linux distributions).