charmbracelet / freeze

Generate images of code and terminal output 📸
MIT License
2.98k stars 50 forks source link

Support Multi-Language highlighting #70

Open b-per opened 3 months ago

b-per commented 3 months ago

Is your feature request related to a problem? Please describe. In my day to day, I write a lot of dbt code which leverages a lot of jinja-sql (e.g. Jinja syntax in conjunction with SQL).

freeze can highlight those either as SQL or Jinja, but not as both (the Jinja code as Jinja and the SQL code as SQL, at the same time)

Describe the solution you'd like It looks like chroma supports this type of multi-language highlighting already, when we define both a language and a root.

freeze could be updated to add an argument for a second language, or like the Jinja VSCode extension does, provide new languages, like jinja-sql

Describe alternatives you've considered Not getting both languages highlighting and doing screenshots in VSCode

Additional context N/A

bashbunni commented 3 months ago

That would be an awesome improvement to the tool. Thanks for the suggestion! @Delta456 expressed interest in working on this, so will let him take a stab at solving this :cook:

Delta456 commented 3 months ago

As @bashbunni said, I will be working on implementing this.

Delta456 commented 3 months ago

Hey @b-per! Can you share code examples used with freeze? It would help to understand the issue better.

b-per commented 3 months ago

Here is an example of code that combines SQL and Jinja. In the "dbt" world, it would be saved as .sql file.

{{
    config(
        materialized='incremental'
    )
}}

select
    *
    cola,
    colb,
    colc,
    my_slow_function(my_column)

from {{ ref('app_data_events') }}

{% if is_incremental() %}

  where event_time >= (select max(event_time) from {{ this }})

{% endif %}

In VSCode, I can get it formatted as:

Ideally, freeze would allow Jinja-SQL as well

Delta456 commented 3 months ago

My current implementation highlights the code like this:

Running: ./freeze --language jinja,sql file.sql

freeze

I believe this is the ideal behaviour.