forked from titoBouzout/WordCount
-
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathWordingStatus.py
414 lines (280 loc) · 14.2 KB
/
WordingStatus.py
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
import sublime
import sublime_plugin
import re
import time
import threading
from math import ceil as ceil
from os.path import basename
VIEW_SIZE_LIMIT = 4194304
Preferences = {}
g_sleepEvent = threading.Event()
g_is_already_running = False
def plugin_unloaded():
global Preferences
global g_is_already_running
g_is_already_running = False
sublime_settings.clear_on_change( 'WordingStatus' )
for window in sublime.windows():
for view in window.views():
view.erase_status(Preferences.status_name);
def plugin_loaded():
global Preferences
global sublime_settings
sublime_settings = sublime.load_settings( 'WordingStatus.sublime-settings' )
Preferences.load();
sublime_settings.clear_on_change( 'WordingStatus' )
sublime_settings.add_on_change( 'WordingStatus', lambda: Preferences.load() )
# Initialize the WordingStatuses's countView attribute
WordingStatuses.setUpView( get_active_view() )
if not g_is_already_running:
g_sleepEvent.set()
# Wait the Preferences class to be loaded
sublime.set_timeout_async( configure_word_count, 5000 )
def configure_word_count():
"""
break/interrupt a time.sleep() in python
https://stackoverflow.com/questions/5114292/break-interrupt-a-time-sleep-in-python
"""
global g_is_already_running
g_is_already_running = True
# Reset the internal flag to false. Subsequently, threads calling wait() will block until set()
# is called to set the internal flag to true again.
g_sleepEvent.clear()
thread = threading.Thread( target=word_count_loop )
thread.start()
def word_count_loop():
mininum_time = 0.01
default_time = 3.0
while True:
# Stops the thread when the plugin is reloaded or unloaded
if not g_is_already_running:
break
# sleep time is adaptive, if takes more than `mininum_time` to calculate the word count,
# sleep_time becomes `elapsed_time*3`
if not Preferences.is_already_running:
if g_sleepEvent.is_set():
# set g_sleepEvent._flag, a.k.a., g_sleepEvent.is_set() to False
g_sleepEvent.clear()
WordingStatuses.setUpView( WordingStatuses.activeView )
WordingStatuses.doCounting()
# print( "word_count_loop, elapsed_time: %f microseconds" % ( Preferences.elapsed_time * 1000 ) )
g_sleepEvent.wait( Preferences.elapsed_time*100 if Preferences.elapsed_time > mininum_time else default_time )
class Preferences():
@staticmethod
def load():
Preferences.elapsed_time = 1.4
Preferences.is_already_running = False
Preferences.wordRegex = re.compile( sublime_settings.get('word_regexp', "^[^\w]?`*\w+[^\w]*$"), re.U )
Preferences.wordRegex = Preferences.wordRegex.match
Preferences.splitRegex = sublime_settings.get('word_split', None)
if Preferences.splitRegex:
Preferences.splitRegex = re.compile(Preferences.splitRegex, re.U)
Preferences.splitRegex = Preferences.splitRegex.findall
Preferences.status_name = sublime_settings.get('status_order_prefix', '') + 'WordCountStatus'
Preferences.enable_readtime = sublime_settings.get('enable_readtime', False)
Preferences.enable_count_lines = sublime_settings.get('enable_count_lines', False)
Preferences.enable_count_chars = sublime_settings.get('enable_count_chars', False)
Preferences.enable_count_pages = sublime_settings.get('enable_count_pages', False)
Preferences.enable_count_words = sublime_settings.get('enable_count_words', True)
Preferences.file_size_limit = sublime_settings.get('file_size_limit', VIEW_SIZE_LIMIT)
Preferences.enable_line_word_count = sublime_settings.get('enable_line_word_count', False)
Preferences.enable_line_char_count = sublime_settings.get('enable_line_char_count', False)
Preferences.readtime_wpm = sublime_settings.get('readtime_wpm', 200)
Preferences.words_per_page = sublime_settings.get('words_per_page', 300)
Preferences.char_ignore_whitespace = sublime_settings.get('char_ignore_whitespace', True)
Preferences.whitelist_syntaxes = sublime_settings.get('whitelist_syntaxes', [])
Preferences.blacklist_syntaxes = sublime_settings.get('blacklist_syntaxes', [])
Preferences.strip = sublime_settings.get('strip', [])
Preferences.thousands_separator = sublime_settings.get('thousands_separator' , "." )
Preferences.label_line = sublime_settings.get('label_line' , " Lines" )
Preferences.label_word = sublime_settings.get('label_word' , " Words" )
Preferences.label_char = sublime_settings.get('label_char' , " Chars" )
Preferences.label_word_in_line = sublime_settings.get('label_word_in_line', " Words in lines" )
Preferences.label_char_in_line = sublime_settings.get('label_char_in_line', " Chars in lines" )
Preferences.label_time = sublime_settings.get('label_time' , " reading time" )
Preferences.label_page = sublime_settings.get('label_page' , "Page " )
Preferences.page_count_mode_count_words = sublime_settings.get('page_count_mode_count_words', True)
class WordingStatuses(sublime_plugin.EventListener):
countView = None
activeView = None
wordCountViews = {}
def on_close(self, view):
view_id = view.id()
if view_id in WordingStatuses.wordCountViews:
del WordingStatuses.wordCountViews[view_id]
def on_selection_modified_async(self, view):
if Preferences.enable_count_words:
selections = view.sel()
for selection in selections:
if len( selection ):
WordingStatuses.countView.is_text_selected = True
return
WordingStatuses.countView.is_text_selected = False
def on_activated_async(self, view):
# print( "on_activated_async, view_id: %d" % view.id() )
WordingStatuses.activeView = view
g_sleepEvent.set()
@classmethod
def doCounting(cls):
countView = cls.countView
if countView.view.change_count() != countView.change_count \
or countView.is_text_selected:
countView.startCounting()
@classmethod
def setUpView(cls, view):
view_settings = view.settings()
wordCountViews = cls.wordCountViews
if view_settings.get('is_widget'):
_view = get_active_view()
if _view:
view = _view
view_settings = view.settings()
syntax, is_enabled = cls.should_run_with_syntax( view_settings )
view_id = view.id()
# print( "setUpView, view_id: %d" % view_id )
if view_id in wordCountViews:
wordCountView = wordCountViews[view_id]
wordCountView.syntax = syntax
wordCountView.syntax = is_enabled
else:
wordCountView = WordingStatusesView( view, syntax, is_enabled )
wordCountViews[view_id] = wordCountView
cls.countView = wordCountView
@staticmethod
def should_run_with_syntax(view_settings):
syntax = view_settings.get('syntax')
syntax = basename( syntax ).split( '.' )[0].lower() if syntax != None else "plain text"
if len( Preferences.blacklist_syntaxes ) > 0:
for white in Preferences.blacklist_syntaxes:
if white == syntax:
return syntax, False
if len(Preferences.whitelist_syntaxes) > 0:
for white in Preferences.whitelist_syntaxes:
if white == syntax:
return syntax, True
return syntax, False
return syntax, True
class WordingStatusesView():
def __init__(self, view, syntax, is_enabled):
self.syntax = syntax
self.is_enabled = is_enabled
self.is_text_selected = False
# We need to set it to -1, because by default it starts on 0. Then we for an update when a
# view is first activated by `WordingStatuses::on_activated_async()`
self.change_count = -1
self.lines_contents = []
self.view = view
self.contents = []
self.char_count = 0
self.word_count = 0
self.line_count = 0
self.word_count_line = 0
self.char_count_line = 0
def updateViewContents(self):
view = self.view
del self.contents[:]
selections = view.sel()
view_size = view.size()
if Preferences.enable_line_char_count or Preferences.enable_line_word_count:
del self.lines_contents[:]
for selection in selections:
self.lines_contents.append( view.substr( view.line( selection.end() ) ) )
file_size_limit = Preferences.file_size_limit
is_limited = view_size > file_size_limit
if is_limited:
self.is_text_selected = False
if self.is_text_selected:
for selection in selections:
self.contents.append( view.substr( selection ) )
else:
self.contents.append( view.substr( sublime.Region( 0, file_size_limit if is_limited else view_size ) ) )
def startCounting(self):
if not self.is_enabled:
return
Preferences.start_time = time.perf_counter()
Preferences.is_already_running = True
view = self.view
self.updateViewContents()
if self.syntax and self.syntax in Preferences.strip:
for regular_expression in Preferences.strip[self.syntax]:
lines_count = len( self.contents )
lines_contents_count = len( self.lines_contents )
for selection_index in range( lines_count ):
self.contents[selection_index] = re.sub( regular_expression, '', self.contents[selection_index] )
for selection_index in range( lines_contents_count ):
self.lines_contents[selection_index] = re.sub( regular_expression, '', self.lines_contents[selection_index] )
if Preferences.enable_count_lines:
self.line_count = view.rowcol( view.size() )[0] + 1
if Preferences.enable_count_words:
self.word_count = count_words( self.contents )
if Preferences.enable_count_chars:
self.char_count = count_chars( self.contents )
if Preferences.enable_line_char_count:
self.char_count_line = count_chars( self.lines_contents )
if Preferences.enable_line_word_count:
self.word_count_line = count_words( self.lines_contents )
self.displayCountResults()
def displayCountResults(self):
display( self.view, self.word_count, self.char_count, self.line_count, self.word_count_line, self.char_count_line )
Preferences.elapsed_time = time.perf_counter() - Preferences.start_time
Preferences.is_already_running = False
def display(view, word_count, char_count, line_count, word_count_line, char_count_line):
status = []
seconds = int( word_count % Preferences.readtime_wpm / ( Preferences.readtime_wpm / 60 ) )
k_sep = Preferences.thousands_separator
if line_count > 0:
status.append( "{:,}{}".format( line_count , Preferences.label_line ).replace(',',k_sep) )
if word_count > 0:
status.append( "{:,}{}".format( word_count , Preferences.label_word ).replace(',',k_sep) )
if char_count > 0:
status.append( "{:,}{}".format( char_count , Preferences.label_char ).replace(',',k_sep) )
if word_count_line > 0:
status.append( "{:,}{}".format( word_count_line, Preferences.label_word_in_line ).replace(',',k_sep) )
if char_count_line > 0:
status.append( "{:,}{}".format( char_count_line, Preferences.label_char_in_line ).replace(',',k_sep) )
if Preferences.enable_count_pages and word_count > 0:
if not Preferences.page_count_mode_count_words or Preferences.words_per_page < 1:
visible = view.visible_region()
rows_per_page = (view.rowcol(visible.end())[0]) - (view.rowcol(visible.begin())[0])
pages = ceil((view.rowcol(view.size()-1)[0] + 1 ) / rows_per_page)
current_line = view.rowcol(view.sel()[0].begin())[0]+1
current_page = ceil(current_line / rows_per_page)
else:
pages = ceil(word_count / Preferences.words_per_page)
rows = view.rowcol(view.size()-1)[0] + 1
current_line = view.rowcol(view.sel()[0].begin())[0]+1
current_page = ceil((current_line / Preferences.words_per_page) / (rows / Preferences.words_per_page))
if pages > 1:
status.append( "{}{:,}/{:,}".format( Preferences.label_page, current_page, pages ).replace( ',', k_sep ) )
if Preferences.enable_readtime and seconds >= 1:
minutes = int( word_count / Preferences.readtime_wpm )
status.append( "~{:,}m {:,}s{}".format( minutes, seconds, Preferences.label_time ).replace( ',', k_sep ) )
status_text = ', '.join( status )
view.set_status( Preferences.status_name, status_text )
# print( "view: %d, Setting status to: " % view.id() + status_text )
def count_words(text_list):
words_count = 0
wordRegex = Preferences.wordRegex
splitRegex = Preferences.splitRegex
if splitRegex:
for text in text_list:
words = splitRegex( text )
for word in words:
if wordRegex( word ):
words_count += 1
else:
for text in text_list:
words_count += len( text.split() )
return words_count
def count_chars(text_list):
char_count = 0
if Preferences.char_ignore_whitespace:
char_count = sum( sum( len( word ) for word in words.split() ) for words in text_list )
else:
char_count = sum( len( words ) for words in text_list )
return char_count
def get_active_view():
window = sublime.active_window()
if window:
return window.active_view()
return None