Package tipy :: Module clbk
[hide private]
[frames] | no frames]

Source Code for Module tipy.clbk

 1  #!/usr/bin/env python3 
 2  # -*- coding: utf-8 -*- 
 3   
 4  """Callback class holds the buffers and allow the outer world to access them.""" 
 5   
 6   
7 -class Callback(object):
8 """Allow access to the current buffers from anywhere. 9 10 This class is used as a callback to retrieve and modify the input buffers. 11 There is two input buffers the left buffer and the right buffer: 12 - Left buffer contains every characters preceding the cursor. 13 - Right buffer contains every characters following the cursor. 14 15 It is possible to subclass this class and override its methods in order to 16 use the program in other GUIs or command line interfaces. 17 18 G{classtree Callback} 19 """ 20
21 - def __init__(self):
22 """Initialize the left and right buffer to empty string.""" 23 self.left = '' 24 self.right = ''
25
26 - def update(self, character, offset=0):
27 """Update the left and right buffers. 28 29 This method should be called after each character addition or deletion 30 or after each cursor move. 31 In the default GUI this method is called when a signal indicating that 32 the input text have changed or that the cursor position have changed. 33 34 @param character: 35 The character which has been inputted. It could be any character 36 associated to keyboard keys including backspace, arrow keys... 37 @type character: str 38 @param offset: 39 Indicate the cursor position offset against the previous cursor 40 position. 0 means no move, a positive value means move to the right 41 and a negative value means move to the left. 42 @type offset: int 43 """ 44 if character == '\b' and len(self.left) >= offset: 45 self.left = self.left[:-offset] 46 elif character == '\x1B[D' and len(self.left) >= offset: 47 self.right = self.left[offset:] + self.right 48 self.left = self.left[:offset] 49 elif character == '\x1B[C' and len(self.right) >= offset: 50 self.left += self.right[:offset] 51 self.right = self.right[offset:] 52 else: 53 self.left += character
54