Highlighting and Selecting text with Python curses

后端 未结 2 2178
自闭症患者
自闭症患者 2021-02-09 00:53

This is my first post to stack overflow. I\'ve been lurking this site for information for years, and it\'s always helpful, so I thought that I would post my first question.

相关标签:
2条回答
  • 2021-02-09 01:17

    I don't know anything about ncurses, but using Tkinter or one of the other GUI toolkits, it could be done with a canvas. You would first set the scroll region, then bind the "h" key so it sets a highlight variable to True, bind a mouse click to a function that retrieves the location to start, bind the left and right arrows to move left or right one letter, and move the cursor and reverse the colors of the next letter as well as keep track of how many letters you are from the starting point, which also tells you the end location, and bind "y" to copy the text.

    It is not as complicated as it might seem as each part is a fairly simple function. All of this would of course depend on the font size, and I would suggest displaying the text with a fixed font so each letter occupies the same width. Once again, I do not know ncurses, but it would be similiar with any toolkit. If you can work it around so that the user can select one entire line with a mouse click, it makes the coding much easier. Post back with some code if you do indeed decide to go with Tkinter or wxPython and want some additional help.

    0 讨论(0)
  • 2021-02-09 01:19

    I would like to update this question in case anybody else is searching the web for this and stumbles upon this question.

    Okay, so the answer was actually quite simple and required reading all the functions listed on the python curses documentation.

    What I did was make a 3 state machine: State 1: Normal mode (displays text only), State 2: highlight mode, allow the cursor to move around the window, and State 3: highlighter mode, which gives only limited movement from left to right over texts and highlights the text as it's moving.

    To accomplish this task, it just takes some basic curses function calls.

    I made separate windows, but I'll just assume a single window for explaination.

    Do display text in the window, stick with the:

    window.addstr()
    window.refresh()
    

    For moving the cursor around:

    #get current cursor position
    curr_y, curr_x = window.getyx()
    
    # depending on direction, update the cursor with
    next_y, next_x = get_next_direction()
    
    # place cursor in new position
    window.move(next_y, next_x)
    
    window.refresh()
    

    Once the cursor is over the starting point for highlighting, press 'v' enter the highlighter state, and limit the movement of the cursor, change the attribute of the selected text:

    # get current cursor position
    curr_y, curr_x = window.getyx()
    
    # save position for later use
    start_y = curr_y; start_x = curr_x
    
    # loop until 'v' is pressed again
    while highlight_state:
        # change the attribute of the current character, for 1 character only, to reverse
        window.chgat(curr_y,curr_x, 1, curses.A_REVERSE)
        curr_y, curr_x = get_next_direction()
    
    # save end state, this is buggy obviously, but you get the point
    end_y = curr_y; end_x = curr_X
    

    Now extract that information from start to end

    # get integer representation of char at positiong
    
    outstr = ''
    #from start to end
    char_as_int = windows.inch(y,x)
    char = char_as_int & 0000FF
    attr = char_as_int & FFFF00 #not useful here, but maybe later
    
    outstr += char
    

    That's it! I also tried another way to save the highlighted material which was basically transformat the x,y coordinates to the index of the string that was being display, but that let to issue in string representation (newlines, tabs, and so on), plus was just harder to do.

    If anyone has comments of a more efficient/ cleaner method, please respond!

    0 讨论(0)
提交回复
热议问题