Code Monkey home page Code Monkey logo

tklinenums's Introduction

A Little Background On Me

  • I code in a lot of languages, mainly Python and Mojo
  • I like making code editors for fun
  • Most of my code is private, readme stats are skewed
  • Contact me at [email protected] (I'll check it when I find time) or just open an issue on my account repo.

Moosems GitHub Stats

tklinenums's People

Contributors

lazeit avatar littlewhitecloud avatar moosems avatar rdbende avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

tklinenums's Issues

Colors from text widget

[22:12]	Akuli	idea: maybe there would be an option to
		take the colors from the text widget rather
		than the ttk theme?
[22:13]	Moosems	?
[22:13]	Moosems	Issue?
[22:13]	Akuli	e.g. in porcupine the line numbers are
		always same color as the text widget
		background, and that has nothing to do
		with a ttk theme
[22:13]	Moosems	:)
[22:13]	Akuli	so you could have e.g. a gray UI with
		even darker text widget (my porcupine)

Mouse Scroll `sel`

Add sel tag if the mouse is down in case it's not moving while scrolling

Combining with tkscrollbar doesn't work

Hi,

If you want to use it alongside tk.Scrollbar, I can't seem to find a way to tie it together, both scrolls work but not simultaneously.
Any ideas?

Thanks

How to use TkLineNums together with a scroll bar?

editor["yscrollcommand"] = self.redraw

The standard way to make a scroll bar is:

text["yscrollcommand"] = scrollbar.set
scrollbar["command"] = text.yview

which is incompatible with how TkLineNums sets the yscrollcommand.

To work around this, Porcupine has a function add_scroll_command() that lets you set multiple commands:

def add_scroll_command(
    widget: tkinter.Text,
    option: Literal["xscrollcommand", "yscrollcommand"],
    callback: Callable[[], None],
) -> None:
    """Schedule ``callback`` to run with no arguments when ``widget`` is scrolled.

    The option should be ``'xscrollcommand'`` for horizontal scrolling or
    ``'yscrollcommand'`` for vertical scrolling.

    Unlike when setting the option directly, this function can be called
    multiple times with the same widget and the same option to set multiple
    callbacks.
    """
    if not widget[option]:
        widget[option] = lambda *args: None
    tcl_code = widget[option]
    assert isinstance(tcl_code, str)
    assert tcl_code

    # from options(3tk): "... the widget will generate a Tcl command by
    # concatenating the scroll command and two numbers."
    #
    # So if tcl_code is like this:  bla bla bla
    #
    # it would be called like this:  bla bla bla 0.123 0.456
    #
    # and by putting something in front on separate line we can make it get called like this
    #
    #   something
    #   bla bla bla 0.123 0.456
    widget[option] = widget.register(callback) + "\n" + tcl_code

And for line numbers, Porcupine does:

        utils.add_scroll_command(textwidget_of_tab, "yscrollcommand", self.do_update)

Adding tilde characters

Hello, @Moosems !
First of all, I would like to thank you and all contributors for this amazing repository that's making my python text editor, Pytext, become true.
I'm opening this issue to discuss about a contribution to TkLineNums, to add tilde chars.

If you don't know what a tilde char is, I will show you an example with Vim:
intro-vim-unix-text-editor-every-hacker-should-be-familiar-with w1456
The "~" chars are tilde chars. They represent non-lines in the line counter. In the current TkLineNums, tilde chars are not customizable and, if a line index is not existent, it is just ignored.

I changed your code so user can add a custom tilde char to its TkLineNums instance.

Example in my current code:
self.line_counter = TkLineNumbers(master, self, justify="right", colors=("#e3ba68", "#1D1E1E"),tilde="~", bd=0)
image

If user doesn't provide a tilde parameter, It will be None. In this case, TkLineNums will work normally, so projects that already use TkLineNums won't break.

    def __init__(
        self: TkLineNumbers,
        master: Misc,
        textwidget: Text,
        justify: str = "left",
        # None means take colors from text widget (default).
        # Otherwise it is a function that takes no arguments and returns (fg, bg) tuple.
        colors: Callable[[], tuple[str, str]] | tuple[str, str] | None = None,
        tilde: str | None = None,
        *args,
        **kwargs,
    ) -> None:

I hope you like my idea and my contribution is already done, It just needs to pull-request. Please, tell my what do you think about this new TkLineNums functionality.
Best regards!

error in redraw function

There is an error in the redraw function when there are more that 1000 lines:

def resize(self) -> None:
        """Resizes the widget to fit the text widget"""

        # Get amount of lines in the text widget
        end = self.textwidget.index("end").split(".")[0]

        # Set the width of the widget to the required width to display the biggest line number
        self.config(width=Font(font=(temp_font := self.textwidget.cget("font"))).measure(" 1234 ")) if int(
            end
        ) <= 1000 else self.config(width=temp_font.measure(f" {end} "))

Namely, temp_font will be undefined if int(end) > 1000. The fix looks pretty simple in that := can be replaced with a normal assignment operator of temp_font prior to calling self.config.

I would be happy to make a PR.

Elided text causes line numbers to overlap

from platform import system
from tkinter import Text, Tk
from tkinter.ttk import Style

from tklinenums import TkLineNumbers

# This is to make the example work on both Windows and Mac
if system() == "Darwin":
    contmand: str = "Command"
else:
    contmand: str = "Control"

# Create the root window
root = Tk()

# Set the ttk style (tkinter's way of styling) for the line numbers
style = Style()
style.configure("TLineNumbers", background="#ffffff", foreground="#2197db")

# Create the Text widget and pack it to the right
text = Text(root)
text.pack(side="right")

# Insert 50 lines of text into the Text widget
for i in range(100):
    text.insert("end", f"Line {i+1}\n")

text.tag_config("foo", elide=True)
text.tag_add("foo", "20.0", "80.0")

# Create the TkLineNumbers widget and pack it to the left
linenums = TkLineNumbers(root, text)
linenums.pack(fill="y", side="left")

# Create binds to redraw the line numbers for most common events that change the text in the Text widget
text.bind("<Key>", lambda event: root.after_idle(linenums.redraw), add=True)
text.bind("<BackSpace>", lambda event: root.after_idle(linenums.redraw), add=True)
text.bind(f"<{contmand}-v>", lambda event: root.after_idle(linenums.redraw), add=True)

# Start the mainloop for the root window
root.mainloop()

s

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.