• Login
Community
  • Login

(Pythonscript) Status bar to display word count & understanding SCINTILLANOTIFICATION

Scheduled Pinned Locked Moved Help wanted · · · – – – · · ·
5 Posts 2 Posters 515 Views
Loading More Posts
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D
    Dimitri Sudomoin
    last edited by Nov 10, 2022, 3:36 PM

    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-count

    However 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 theSCINTILLANOTIFICATION.KEY and MODIFICATIONFLAGS 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])
    

    @PeterJones, @Alan-Kilborn

    A 1 Reply Last reply Nov 10, 2022, 3:44 PM Reply Quote 4
    • A
      Alan Kilborn @Dimitri Sudomoin
      last edited by Alan Kilborn Nov 10, 2022, 3:46 PM Nov 10, 2022, 3:44 PM

      @Dimitri-Sudomoin

      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.

      D 1 Reply Last reply Nov 10, 2022, 4:10 PM Reply Quote 2
      • D
        Dimitri Sudomoin @Alan Kilborn
        last edited by Dimitri Sudomoin Nov 10, 2022, 4:14 PM Nov 10, 2022, 4:10 PM

        @Alan-Kilborn

        Yeah I tried SCINTILLANOTIFICATION.MODIFIED but it actually fired a lot more than SCINTILLANOTIFICATION.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?

        A 1 Reply Last reply Nov 10, 2022, 4:15 PM Reply Quote 0
        • A
          Alan Kilborn @Dimitri Sudomoin
          last edited by Alan Kilborn Nov 10, 2022, 4:31 PM Nov 10, 2022, 4:15 PM

          @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

          D 1 Reply Last reply Nov 10, 2022, 5:11 PM Reply Quote 0
          • D
            Dimitri Sudomoin @Alan Kilborn
            last edited by Nov 10, 2022, 5:11 PM

            @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])
            
            1 Reply Last reply Reply Quote 1
            4 out of 5
            • First post
              4/5
              Last post
            The Community of users of the Notepad++ text editor.
            Powered by NodeBB | Contributors