Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Colourising Method representation for easier glancing #40913

Closed
tecosaur opened this issue May 22, 2021 · 7 comments · Fixed by #45069
Closed

Colourising Method representation for easier glancing #40913

tecosaur opened this issue May 22, 2021 · 7 comments · Fixed by #45069
Labels
display and printing Aesthetics and correctness of printed representations of objects.

Comments

@tecosaur
Copy link
Contributor

tecosaur commented May 22, 2021

Hello!

I love being able to see all the various implement methods, or methods using a certain type in Julia.
However, I didn't find it easy to find what I was looking for at a glance. Particularly for functions with many (tens-hundreds) of implementations. So, I've tried adding a bit of colour. I've found it really helpful, and think it would be good if something like it were the default.

Happy to make a PR, or just see my work used.

Example

Current output for methods(sum)

image

My output for methods(sum)

image

Code

function Base.show(io::IO, m::Method)
    tv, decls, file, line = Base.arg_decl_parts(m)
    sig = Base.unwrap_unionall(m.sig)
    if sig === Tuple
        # Builtin
        print(io, m.name, "(...) in ", m.module)
        return
    end
    printstyled(io, decls[1][2]; color=:blue)
    print(io, "(")
    for (d, i) in zip(decls[2:end], 2:length(decls))
        if isempty(d[2])
            printstyled(io, d[1]; color=:magenta)
        else
            printstyled(io, d[1]; color=:magenta)
            typespec = split(d[2], "{"; limit=2)
            if length(typespec) == 1
                printstyled(io, "::", d[2]; color=:yellow)
            else
                printstyled(io, "::", typespec[1]; color=:yellow)
                printstyled(io, "{", typespec[2]; color=247)
            end
        end
        if i < length(decls)
            printstyled(io, ", ")
        end
    end
    kwargs = Base.kwarg_decl(m)
    if !isempty(kwargs)
        printstyled(io, "; "; bold=true)
        for (k, i) in zip(kwargs, 1:length(kwargs))
            printstyled(io, Base.sym_to_string(k); color=:magenta)
            if i < length(kwargs)
                printstyled(io, ", ")
            end
        end
    end
    print(io, ")")
    Base.show_method_params(io, tv)
    printstyled(io, " in "; color=:light_black)
    printstyled(io, m.module; color=:light_red)
    if line > 0
        file, line = Base.updated_methodloc(m)
        printstyled(io, " at "; color=:light_black)
        printstyled(io, file; color=:green)
        printstyled(io, ":", line)
    end
end

function Base.show_method_list_header(io::IO, ms::Base.MethodList, namefmt::Function)
    mt = ms.mt
    name = mt.name
    hasname = isdefined(mt.module, name) &&
              typeof(getfield(mt.module, name)) <: Function
    n = length(ms)
    if mt.module === Core && n == 0 && mt.defs === nothing && mt.cache !== nothing
        # try to detect Builtin
        print(io, "# built-in function; no methods")
    else
        printstyled(io, "# "; color=:light_black)
        printstyled(io, n; color=:cyan, bold=true)
        print(io, " ", n==1 ? "method" : "methods")
        sname = string(name)
        if hasname
            what = startswith(sname, '@') ? "macro" : "generic function"
            print(io, " for ", what, " ")
            printstyled(io, sname; color=:blue)
        elseif '#' in sname
            print(io, " for anonymous function ", namefmt(sname))
        elseif mt === Base._TYPE_NAME.mt
            print(io, " for type constructor")
        end
        print(io, ":")
    end
end
@tecosaur tecosaur changed the title Colourising Methods representation for easier glancing Colourising Method representation for easier glancing May 22, 2021
@oscardssmith
Copy link
Member

This would be great to get in a PR. Prepare for lots of bike-sheading about what colors to use for what though. With a little bit of luck, this might be able to make it into 1.7 (feature freeze June 1st)

@tecosaur
Copy link
Contributor Author

Cool! I'll whip one up then.

Oh, we can also bikeshed about supporting terminal file hyperlinks (like ls --hyperlink) 😛

@thofma
Copy link
Contributor

thofma commented May 22, 2021

#40251

@tecosaur
Copy link
Contributor Author

Funny that someone else made a PR for the exact same thing a bit over a month ago 😆. Missing the "show" keyword in the thread caused me to miss it when searching, thanks for linking it @thofma.

Should I close my PR or leave it open to discuss what I have there?

@oscardssmith
Copy link
Member

oscardssmith commented May 22, 2021

Honestly, I think this color scheme is probably better than the one in 40251. I'd probably move discussion to the PR, and we can try to get it merged soon.

@cormullion
Copy link
Contributor

think it would be good if something like it were the default

I think this should be optional and configurable.

@tecosaur
Copy link
Contributor Author

I think this should be optional and configurable.

Probably best for discussion to be in the PR, now that it exists, but if there's going to be configuration for Julia REPL colourisation — either a global Bool or more granular structure would be the way to go, wouldn't it? If so, that seems like it would be a discussion beyond the scope of this change (even if prompted by this), considering the number of ways Julia currently outputs coloured text.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
display and printing Aesthetics and correctness of printed representations of objects.
Projects
None yet
5 participants