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

Feature request: add renderable for code #165

Closed
adrhill opened this issue Nov 24, 2022 · 2 comments
Closed

Feature request: add renderable for code #165

adrhill opened this issue Nov 24, 2022 · 2 comments

Comments

@adrhill
Copy link

adrhill commented Nov 24, 2022

It would be great to have something akin to RenderableText that prints highlighted Julia code.

Similar functionality seems to already exist in Term's Markdown support.

@FedeClaudi
Copy link
Owner

Hey,

What do you mean highlighted "Julia code"? How do you pass the code to the print function?
If you have your code as a string you can already do this:

using Term
import Term: highlight_syntax


mycode  = 
"""
# function to calculate the volume of a sphere
function sphere_vol(r)
    # julia allows Unicode names (in UTF-8 encoding)
    # so either "pi" or the symbol π can be used
    return 4/3*pi*r^3
end

# functions can also be defined more succinctly
quadratic(a, sqr_term, b) = (-b + sqr_term) / 2a

# calculates x for 0 = a*x^2+b*x+c, arguments types can be defined in function definitions
function quadratic2(a::Float64, b::Float64, c::Float64)
    # unlike other languages 2a is equivalent to 2*a
    # a^2 is used instead of a**2 or pow(a,2)
    sqr_term = sqrt(b^2-4a*c)
    r1 = quadratic(a, sqr_term, b)
    r2 = quadratic(a, -sqr_term, b)
    # multiple values can be returned from a function using tuples
    # if the return keyword is omitted, the last term is returned
    r1, r2
end

"""


highlight_syntax(mycode) |> print

image

@adrhill
Copy link
Author

adrhill commented Nov 24, 2022

Sorry for the noise, I must have missed this in the docs!

@adrhill adrhill closed this as completed Nov 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants