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

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

# -*- 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 re 

from collections import deque 

from unicodedata import normalize as ud_normalize 

from unicodedata import east_asian_width 

 

import deluge.common 

 

 

def format_speed(speed): 

    if (speed > 0): 

        return deluge.common.fspeed(speed) 

    else: 

        return "-" 

 

 

def format_time(time): 

    if (time > 0): 

        return deluge.common.ftime(time) 

    else: 

        return "-" 

 

 

def format_date(time): 

    if (time > 0): 

        return deluge.common.fdate(time) 

    else: 

        return "" 

 

 

def format_date_never(time): 

    if (time > 0): 

        return deluge.common.fdate(time) 

    else: 

        return "Never" 

 

 

def format_float(x): 

    if x < 0: 

        return "-" 

    else: 

        return "%.3f" % x 

 

 

def format_seeds_peers(num, total): 

    return "%d (%d)" % (num, total) 

 

 

def format_progress(perc): 

    if perc < 100: 

        return "%.2f%%" % perc 

    else: 

        return "100%" 

 

 

def format_pieces(num, size): 

    return "%d (%s)" % (num, deluge.common.fsize(size)) 

 

 

def format_priority(prio): 

    if prio == - 2: 

        return "[Mixed]" 

    if prio < 0: 

        return "-" 

    pstring = deluge.common.FILE_PRIORITY[prio] 

    if prio > 0: 

        return pstring[:pstring.index("Priority") - 1] 

    else: 

        return pstring 

 

 

def trim_string(string, w, have_dbls): 

    if w <= 0: 

        return "" 

    elif w == 1: 

        return u" " 

    elif have_dbls: 

        # have to do this the slow way 

        chrs = [] 

        width = 4 

        idx = 0 

        while width < w: 

            chrs.append(string[idx]) 

            if east_asian_width(string[idx]) in ["W", "F"]: 

                width += 2 

            else: 

                width += 1 

            idx += 1 

        if width != w: 

            chrs.pop() 

            chrs.append(".") 

        return u"%s " % ("".join(chrs)) 

    else: 

        return u"%s " % (string[0:w - 1]) 

 

 

def format_column(col, lim): 

    dbls = 0 

    # Chosen over isinstance(col, unicode) and col.__class__ == unicode 

    # for speed - it's ~3 times faster for non-unicode strings and ~1.5 

    # for unicode strings. 

    if col.__class__ is unicode: 

        # might have some double width chars 

        col = ud_normalize("NFC", col) 

        dbls = sum(east_asian_width(c) in "WF" for c in col) 

    size = len(col) + dbls 

    if (size >= lim - 1): 

        return trim_string(col, lim, dbls > 0) 

    else: 

        return "%s%s" % (col, " " * (lim - size)) 

 

 

def format_row(row, column_widths): 

    return "".join([format_column(row[i], column_widths[i]) for i in range(0, len(row))]) 

 

_strip_re = re.compile("\{!.*?!\}") 

 

 

def remove_formatting(string): 

    return re.sub(_strip_re, "", string) 

 

 

def wrap_string(string, width, min_lines=0, strip_colors=True): 

    """ 

    Wrap a string to fit in a particular width.  Returns a list of output lines. 

 

    :param string: str, the string to wrap 

    :param width: int, the maximum width of a line of text 

    :param min_lines: int, extra lines will be added so the output tuple contains at least min_lines lines 

    :param strip_colors: boolean, if True, text in {!!} blocks will not be considered as adding to the 

                              width of the line.  They will still be present in the output. 

    """ 

    ret = [] 

    s1 = string.split("\n") 

 

    def insert_clr(s, offset, mtchs, clrs): 

        end_pos = offset + len(s) 

        while mtchs and (mtchs[0] <= end_pos) and (mtchs[0] >= offset): 

            mtc = mtchs.popleft() - offset 

            clr = clrs.popleft() 

            end_pos += len(clr) 

            s = "%s%s%s" % (s[:mtc], clr, s[mtc:]) 

        return s 

 

    for s in s1: 

        offset = 0 

        if strip_colors: 

            mtchs = deque() 

            clrs = deque() 

            for m in _strip_re.finditer(s): 

                mtchs.append(m.start()) 

                clrs.append(m.group()) 

            cstr = _strip_re.sub("", s) 

        else: 

            cstr = s 

        while len(cstr) > width: 

            sidx = cstr.rfind(" ", 0, width - 1) 

            sidx += 1 

            if sidx > 0: 

                if strip_colors: 

                    to_app = cstr[0:sidx] 

                    to_app = insert_clr(to_app, offset, mtchs, clrs) 

                    ret.append(to_app) 

                    offset += len(to_app) 

                else: 

                    ret.append(cstr[0:sidx]) 

                cstr = cstr[sidx:] 

                if not cstr: 

                    cstr = None 

                    break 

            else: 

                # can't find a reasonable split, just split at width 

                if strip_colors: 

                    to_app = cstr[0:width] 

                    to_app = insert_clr(to_app, offset, mtchs, clrs) 

                    ret.append(to_app) 

                    offset += len(to_app) 

                else: 

                    ret.append(cstr[0:width]) 

                cstr = cstr[width:] 

                if not cstr: 

                    cstr = None 

                    break 

        if cstr is not None: 

            if strip_colors: 

                ret.append(insert_clr(cstr, offset, mtchs, clrs)) 

            else: 

                ret.append(cstr) 

 

    if min_lines > 0: 

        for i in range(len(ret), min_lines): 

            ret.append(" ") 

 

    # Carry colors over to the next line 

    last_color_string = "" 

    for i, line in enumerate(ret): 

        if i != 0: 

            ret[i] = "%s%s" % (last_color_string, ret[i]) 

 

        colors = re.findall("\\{![^!]+!\\}", line) 

        if colors: 

            last_color_string = colors[-1] 

 

    return ret 

 

 

def strwidth(string): 

    """ 

    Measure width of a string considering asian double width characters 

    """ 

    if not isinstance(string, unicode): 

        string = unicode(string, "utf-8") 

    return sum([1 + (east_asian_width(char) in ["W", "F"]) for char in string]) 

 

 

def pad_string(string, length, character=" ", side="right"): 

    """ 

    Pad string with specified character to desired length, considering double width characters. 

    """ 

    w = strwidth(string) 

    diff = length - w 

    if side == "left": 

        return "%s%s" % (character * diff, string) 

    elif side == "right": 

        return "%s%s" % (string, character * diff)