about summary refs log tree commit diff stats
path: root/ranger/gui/widgets/notify.py
blob: 8b783fdb204eff42edc552bc11180a1424e82dcb (plain) (blame)
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
"""Notify is a bar which displays messages."""

from . import Widget
from time import time
from collections import deque

class Notify(Widget):
	requested_height = 1
	max_size = 5
	textcontainer = None

	def __init__(self, win):
		Widget.__init__(self, win)
		self.textcontainer = deque(maxlen=self.max_size)

	def poke(self):
		for i in reversed(range(len(self.textcontainer))):
			msg = self.textcontainer[i]
			if msg.elapse and time() > msg.elapse:
				msg.alive = False
				del self.textcontainer[i]
		self.requested_height = len(self.textcontainer) + 1

	def draw(self):
		import curses, socket, os

		i = 0
		for msg in self.textcontainer:
			if i >= self.hei - 1:
				break

			how = msg.bad and 'bad' or 'good'
			self.color_at(i, 0, self.wid,\
					'in_notify', 'background', how)
			self.color('in_notify', 'message', how)
			self.addstr(i, 0, msg.text)
			i += 1

		self.color_reset()
	
	def display(self, text, duration=4, bad=False):
		msg = Message(self.textcontainer, text, duration, bad)
		self.textcontainer.append(msg)
		return msg

class Message(object):
	elapse = None
	text = None
	bad = False
	alive = True
	container = None

	def __init__(self, container, text, duration, bad):
		self.text = text
		self.bad = bad
		self.container = container
		self.set_duration(duration)
	
	def set_duration(self, n=4):
		if n:
			self.elapse = time() + n
		else:
			self.elapse = None

	def delete(self):
		self.alive = False
		try:
			self.container.remove(self)
		except ValueError:
			pass