Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1"""LICENSE 

2Copyright 2018 Hermann Krumrey <hermann@krumreyh.com> 

3 

4This file is part of bokkichat. 

5 

6bokkichat is free software: you can redistribute it and/or modify 

7it under the terms of the GNU General Public License as published by 

8the Free Software Foundation, either version 3 of the License, or 

9(at your option) any later version. 

10 

11bokkichat is distributed in the hope that it will be useful, 

12but WITHOUT ANY WARRANTY; without even the implied warranty of 

13MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 

14GNU General Public License for more details. 

15 

16You should have received a copy of the GNU General Public License 

17along with bokkichat. If not, see <http://www.gnu.org/licenses/>. 

18LICENSE""" 

19 

20from typing import Optional, List 

21from bokkichat.entities.message.Message import Message 

22from bokkichat.entities.Address import Address 

23 

24 

25class TextMessage(Message): 

26 """ 

27 Class that defines an interface for text messages. 

28 Each text message has a title and a body. 

29 Some chat services don't allow titles for messages, in those cases, 

30 the title will be blank. 

31 """ 

32 

33 def __init__( 

34 self, 

35 sender: Address, 

36 receiver: Address, 

37 body: str, 

38 title: Optional[str] = "" 

39 ): 

40 """ 

41 Initializes the TextMessage object 

42 :param sender: The sender of the message 

43 :param receiver: The receiver of the message 

44 :param body: The message body 

45 :param title: The title of the message. Defaults to an empty string 

46 """ 

47 super().__init__(sender, receiver) 

48 self.body = body 

49 self.title = title 

50 

51 def __str__(self) -> str: 

52 """ 

53 :return: A string representation of the TextMessage object 

54 """ 

55 return "{}: {}".format(self.title, self.body) 

56 

57 def make_reply( 

58 self, 

59 body: Optional[str] = None, 

60 title: Optional[str] = None 

61 ) -> Message: 

62 """ 

63 Swaps the sender and receiver of the message 

64 :return: The generated reply 

65 """ 

66 if body is None: 

67 body = self.body 

68 if title is None: 

69 title = self.title 

70 return TextMessage(self.receiver, self.sender, body, title) 

71 

72 @staticmethod 

73 def is_text() -> bool: 

74 """ 

75 :return: Whether or not the message is a text message 

76 """ 

77 return True 

78 

79 def split(self, max_chars: int) -> List[str]: 

80 """ 

81 Splits the body text into multiple chunks below a certain size. 

82 Will try to not break up any lines 

83 :param max_chars: The chunk size 

84 :return: The parts of the message 

85 """ 

86 parts = [""] 

87 for line in self.body.split("\n"): 

88 if len(line) > max_chars: 

89 line = line[0:max_chars - 4] + "..." 

90 current_chunk = parts[-1] 

91 if len(current_chunk) + len(line) > max_chars: 

92 parts.append(line) 

93 else: 

94 parts[-1] = current_chunk + "\n" + line 

95 

96 return parts