comparison frontends/sortilege/chat.py @ 0:c4bc297b82f0

sat: - first public release, initial commit
author goffi@necton2
date Sat, 29 Aug 2009 13:34:59 +0200
parents
children a5b5fb5fc9fd
comparison
equal deleted inserted replaced
-1:000000000000 0:c4bc297b82f0
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3
4 """
5 sortilege: a SAT frontend
6 Copyright (C) 2009 Jérôme Poisson (goffi@goffi.org)
7
8 This program is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
12
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with this program. If not, see <http://www.gnu.org/licenses/>.
20 """
21
22
23
24 import os.path
25 import pdb
26 from logging import debug, info, error
27 from window import Window
28 import os
29 from time import time, localtime, strftime
30 import curses
31 import curses.ascii as ascii
32 from tools.jid import JID
33 from quick_frontend.quick_chat import QuickChat
34
35
36 def C(k):
37 """return the value of Ctrl+key"""
38 return ord(ascii.ctrl(k))
39
40 class Chat(Window, QuickChat):
41
42 def __init__(self, to_id, host):
43 QuickChat.__init__(self, to_id, host)
44 self.__parent=host.stdscr
45 self.to_id=JID(to_id)
46 self.content=[]
47 self.__scollIdx=0
48 Window.__init__(self, self.__parent, self.__parent.getmaxyx()[0]-2, self.__parent.getmaxyx()[1]-30, 0,30, code=host.code)
49
50 #history
51 self.historyPrint(50, True)
52
53 self.hide()
54
55 def resize(self, height, width, y, x):
56 Window.resize(self, height, width, y, x)
57 self.update()
58
59 def resizeAdapt(self):
60 """Adapt window size to self.__parent size.
61 Must be called when self.__parent is resized."""
62 self.resize(self.__parent.getmaxyx()[0]-2, self.__parent.getmaxyx()[1]-30, 0,30)
63 self.update()
64
65 def __getHeader(self, line):
66 """Return the header of a line (eg: "[12:34] <toto> ")."""
67 header=''
68 if self.host.chatParams["timestamp"]:
69 header = header + '[%s] ' % strftime("%H:%M", localtime(float(line[0])))
70 if self.host.chatParams['short_nick']:
71 header = header + ('> ' if line[1]==self.host.whoami.short else '** ')
72 else:
73 header = header + '<%s> ' % line[1]
74 return header
75
76 def update(self):
77 if self.isHidden():
78 echo ("fenetre cachee")
79 return
80 Window.update(self)
81 content=[] #what is really printed
82 irange=range(len(self.content))
83 irange.reverse() #we print the text upward
84 for idx in irange:
85 header=self.__getHeader(self.content[idx])
86 msg=self.content[idx][2]
87 part=0 #part of the text
88 if JID(self.content[idx][1]).short==self.host.whoami.short:
89 att_header=curses.color_pair(1)
90 else:
91 att_header=curses.color_pair(2)
92
93 while (msg):
94 if part==0:
95 hd=header
96 att=att_header
97 max=self.rWidth-len(header)
98 else:
99 hd=""
100 att=0
101 max=self.rWidth
102
103 LF = msg.find('\n') #we look for Line Feed
104 if LF != -1 and LF < max:
105 max = LF
106 next = max + 1 #we skip the LF
107 else:
108 next = max
109
110 content.insert(part,[att,hd, msg[:max]])
111 msg=msg[next:] #we erase treated part
112 part=part+1
113
114 if len(content)>=self.rHeight+self.__scollIdx:
115 #all the screen is filled, we can continue
116 break
117
118 if self.__scollIdx>0 and len(content)<self.rHeight+self.__scollIdx:
119 self.__scollIdx=abs(len(content)-self.rHeight) #all the log fit on the screen, we must stop here
120
121 idx=0
122 for line in content[-self.rHeight-self.__scollIdx : -self.__scollIdx or None]:
123 self.addYXStr(idx, 0, line[1], line[0])
124 self.addYXStr(idx, len(line[1]), line[2])
125 idx=idx+1
126
127 self.noutrefresh()
128
129 def scrollIdxUp(self):
130 """increment scroll index"""
131 self.__scollIdx = self.__scollIdx + 1
132 self.update()
133
134 def scrollIdxDown(self):
135 """decrement scroll index"""
136 if self.__scollIdx > 0:
137 self.__scollIdx = self.__scollIdx - 1
138 self.update()
139
140 def printMessage(self, jid, msg, timestamp=""):
141 if timestamp=="":
142 current_time=time()
143 timestamp=str(current_time)
144 if self.last_history and current_time - float(self.last_history) < 5: #FIXME: Q&D fix to avoid double print on new chat window
145 return
146 self.content.append([timestamp,jid.short,msg])
147 self.update()
148
149 def handleKey(self, k):
150 if k == C('p'):
151 self.scrollIdxUp()
152 elif k == C('n'):
153 self.scrollIdxDown()
154