(Pythonscript) Status bar to display word count & understanding SCINTILLANOTIFICATION
-
So using the following two posts I managed to make some progress on a simple script that displays the word count in my status bar.
https://community.notepad-plus-plus.org/topic/14144/is-there-a-way-to-customize-the-status-bar-to-display-more-helpful-information
https://community.notepad-plus-plus.org/topic/22101/status-bar-to-display-selected-word-countHowever I would like to modify it so that the script is more performant and runs only when a word / words are entered or removed. This would be cases like:
- A word is completed (Space or Return key is pressed)
- Many words are added or removed (Undo, redo, cut, paste, delete, backspace)
I’ve spent a ton of time trying to understand the
SCINTILLANOTIFICATION.KEY
andMODIFICATIONFLAGS
and what'modificationType': 16400
mean but I’m getting stuck.Seems no matter what I do my update wordcount callback runs 2-3 times every time I press a single key.
Here is what I have so far, which updates on every click, selection and key input:
from re import findall def callback_sci_UPDATEUI(args): if editor.getTextLength() < 100000 and args['updated']: # don't search "big" files text = editor.getText() word_count = len(findall(r'\w+', text)) sbar_value = ' ' if word_count == 0 else 'Words: {}'.format(word_count) notepad.setStatusBar(STATUSBARSECTION.DOCTYPE, sbar_value) editor.callback(callback_sci_UPDATEUI, [SCINTILLANOTIFICATION.UPDATEUI])
-
Here is what I have so far, which updates on every click, selection and key input…SCINTILLANOTIFICATION.UPDATEUI
Well “update ui” IS going to fire everytime the UI needs updating, hence its name. :-)
Perhaps
SCINTILLANOTIFICATION.MODIFIED
is better?
That one fires when text content of the editor buffer is changed.
It still may occur more times than you’d expect, but I would say have a look into it. -
Yeah I tried
SCINTILLANOTIFICATION.MODIFIED
but it actually fired a lot more thanSCINTILLANOTIFICATION.UPDATEUI
.For example, just just one keystroke would call the function three times. Same with
SCINTILLANOTIFICATION.KEY
which would call it 2 times IIRC - though maybe its firing on key up and key down? I couldn’t really find good documentation or examples relating to SCINTILLANOTIFICATION.For example,
args
with SCINTILLANOTIFICATION.UPDATEUI contains'updated': 2
which seems to correspond to the type of action'updated': 1
and'updated': 3
being keystroke or backspace and'updated': 2
being click. How does one figure out what number corresponds to what action? -
@Dimitri-Sudomoin said in (Pythonscript) Status bar to display word count & understanding SCINTILLANOTIFICATION:
I couldn’t really find good documentation
I suppose this is the bible of documentation for it: https://www.scintilla.org/ScintillaDoc.html#Notifications
I do realize that some of the notifications fire “a lot”. But in practice, I typically just ignore that and let my callbacks do their duty, no matter how redundant. Is this causing some sort of problem for you, or does it merely bother you?
Perhaps you can cut down on some of the “noise” by using: https://www.scintilla.org/ScintillaDoc.html#SCI_SETMODEVENTMASK
-
@Alan-Kilborn said in (Pythonscript) Status bar to display word count & understanding SCINTILLANOTIFICATION:
Is this causing some sort of problem for you, or does it merely bother you?
Mostly the latter, haha.
In any case, I don’t really notice any impact on performance so I’m going to let it go. Here is my final script that I ended up writing for those that come along and want to see it.
from re import findall def callback_sci_UPDATEUI(args): SPEECH_SEC_PER_WORD = 0.66 # 0.55 = fast, 0.66 = normal, 0.75 = paused if editor.getTextLength() < 100000 and args['updated']: # don't search "big" files if editor.getSelections() == 1 and not editor.getSelectionEmpty(): # show word count for selection text = editor.getSelText() sel_info = ' (For Selected)' else: # show word count for entire document text = editor.getText() sel_info = '' word_count = len(findall(r'\w+', text)) speech_minutes = (word_count * SPEECH_SEC_PER_WORD / 60) % 60 speech_seconds = (word_count * SPEECH_SEC_PER_WORD) % 60 speaking_time_str = "%02dm%02ds" % (speech_minutes, speech_seconds) sbar_value = 'Words: {} Speaking Time: {}{}'.format(word_count, speaking_time_str, sel_info) notepad.setStatusBar(STATUSBARSECTION.DOCTYPE, sbar_value) editor.callback(callback_sci_UPDATEUI, [SCINTILLANOTIFICATION.UPDATEUI])