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

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

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

# -*- coding: utf-8 -*- 

# 

# Copyright (C) 2011 Nick Lanham <nick@afternight.org> 

# 

# This file is part of Deluge and is licensed under GNU General Public License 3.0, or later, with 

# the additional special exception to link portions of this program with the OpenSSL library. 

# See LICENSE for more details. 

# 

 

import logging 

 

import deluge.component as component 

from deluge.ui.client import client 

from deluge.ui.console.modes import format_utils 

from deluge.ui.console.modes.basemode import BaseMode 

 

try: 

    import curses 

except ImportError: 

    pass 

 

log = logging.getLogger(__name__) 

 

 

class EventView(BaseMode): 

    def __init__(self, parent_mode, stdscr, encoding=None): 

        self.parent_mode = parent_mode 

        self.offset = 0 

        BaseMode.__init__(self, stdscr, encoding) 

 

    def refresh(self): 

        "This method just shows each line of the event log" 

        events = component.get("ConsoleUI").events 

 

        self.stdscr.erase() 

 

        self.add_string(0, self.statusbars.topbar) 

        hstr = "%sPress [h] for help" % (" " * (self.cols - len(self.statusbars.bottombar) - 10)) 

        # This will quite likely fail when switching modes 

        try: 

            rf = format_utils.remove_formatting 

            string = self.statusbars.bottombar 

            hstr = "Press {!magenta,blue,bold!}[h]{!status!} for help" 

 

            string += " " * (self.cols - len(rf(string)) - len(rf(hstr))) + hstr 

 

            self.add_string(self.rows - 1, string) 

        except: 

            pass 

 

        if events: 

            for i, event in enumerate(events): 

                if i - self.offset >= self.rows - 2: 

                    more = len(events) - self.offset - self.rows + 2 

                    if more > 0: 

                        self.add_string(i - self.offset, "  (And %i more)" % more) 

                    break 

 

                elif i - self.offset < 0: 

                    continue 

                try: 

                    self.add_string(i + 1 - self.offset, event) 

                except curses.error: 

                    pass  # This'll just cut the line. Note: This seriously should be fixed in a better way 

        else: 

            self.add_string(1, "{!white,black,bold!}No events to show yet") 

 

        if component.get("ConsoleUI").screen != self: 

            return 

 

        self.stdscr.noutrefresh() 

        curses.doupdate() 

 

    def on_resize(self, *args): 

        BaseMode.on_resize_norefresh(self, *args) 

 

        # Always refresh Legacy(it will also refresh AllTorrents), otherwise it will bug deluge out 

        legacy = component.get("LegacyUI") 

        legacy.on_resize(*args) 

        self.stdscr.erase() 

        self.refresh() 

 

    def back_to_overview(self): 

        self.stdscr.erase() 

        component.get("ConsoleUI").set_mode(self.parent_mode) 

        self.parent_mode.resume() 

 

    def read_input(self): 

        c = self.stdscr.getch() 

 

        if c > 31 and c < 256: 

            if chr(c) == "Q": 

                from twisted.internet import reactor 

                if client.connected(): 

                    def on_disconnect(result): 

                        reactor.stop() 

                    client.disconnect().addCallback(on_disconnect) 

                else: 

                    reactor.stop() 

                return 

            elif chr(c) == "q": 

                self.back_to_overview() 

                return 

 

        if c == 27: 

            self.back_to_overview() 

            return 

 

        # TODO: Scroll event list 

        jumplen = self.rows - 3 

        num_events = len(component.get("ConsoleUI").events) 

 

        if c == curses.KEY_UP: 

            self.offset -= 1 

        elif c == curses.KEY_PPAGE: 

            self.offset -= jumplen 

        elif c == curses.KEY_HOME: 

            self.offset = 0 

        elif c == curses.KEY_DOWN: 

            self.offset += 1 

        elif c == curses.KEY_NPAGE: 

            self.offset += jumplen 

        elif c == curses.KEY_END: 

            self.offset += num_events 

        elif c == ord("j"): 

            self.offset -= 1 

        elif c == ord("k"): 

            self.offset += 1 

 

        if self.offset <= 0: 

            self.offset = 0 

        elif num_events > self.rows - 3: 

            if self.offset > num_events - self.rows + 3: 

                self.offset = num_events - self.rows + 3 

        else: 

            self.offset = 0 

 

        self.refresh()