<commit_before>--this is a lua script for use in conky
require 'cairo'
json =  require 'json'

function conky_conkybar()
    if conky_window == nil then
        return
    end
    local cs = cairo_xlib_surface_create(conky_window.display,
                                         conky_window.drawable,
                                         conky_window.visual,
                                         conky_window.width,
                                         conky_window.height)
    cr = cairo_create(cs)
    local updates=tonumber(conky_parse('${updates}'))
    if updates>3 then
        print ('W x H',conky_window.width,conky_window.height)
        local primary_font='Source Han Sans CN'
        local primary_font_size=16
        local primary_font_slant=CAIRO_FONT_SLANT_NORMAL
        local primary_font_face=CAIRO_FONT_WEIGHT_NORMAL

        local red,green,blue,alpha=1,1,1,1
        local xpos,ypos=0,0
        local text=''



        -- workspace indicator
        xpos,ypos=3,20
        red,green,blue,alpha=1,1,1,1
        cairo_move_to(cr,xpos,ypos)
        cairo_select_font_face(cr, primary_font, primary_font_slant, primary_font_face)
        cairo_set_font_size(cr, primary_font_size)
        cairo_set_source_rgba(cr,red,green,blue,alpha)

        local workspaces = json.decode(conky_parse('${exec i3-msg -t get_workspaces}'))
        -- parse ws here
        text=''
        for i,w in ipairs(workspaces) do
          if w['visible'] == true then
            text = text .. ' (' .. w.num .. ') '
          else
            text = text .. ' ' .. w.num .. ' '
          end
        end
        cairo_show_text(cr,text)
        cairo_stroke(cr)



        -- debugging info
        xpos,ypos=400,20
        red,green,blue,alpha=1,1,0,1
        cairo_move_to(cr,xpos,ypos)
        cairo_select_font_face(cr, primary_font, primary_font_slant, primary_font_face)
        cairo_set_font_size(cr, primary_font_size)
        cairo_set_source_rgba(cr,red,green,blue,alpha)

        text='w=' .. conky_window.width .. ',h=' .. conky_window.height
        cairo_show_text(cr,text)
        cairo_stroke(cr)



        -- date time
        xpos,ypos=653,20
        red,green,blue,alpha=1,1,1,1
        cairo_move_to(cr,xpos,ypos)
        cairo_select_font_face(cr, primary_font, primary_font_slant, primary_font_face)
        cairo_set_font_size(cr, primary_font_size)
        cairo_set_source_rgba(cr,red,green,blue,alpha)

        text=conky_parse('${time %a, %d %b %Y %T %z}')
        cairo_show_text(cr,text)
        cairo_stroke(cr)



        -- clementine playing
        xpos,ypos=1300,20
        red,green,blue,alpha=1,1,1,1
        cairo_move_to(cr,xpos,ypos)
        cairo_select_font_face(cr, primary_font, primary_font_slant, primary_font_face)
        cairo_set_font_size(cr, primary_font_size)
        cairo_set_source_rgba(cr,red,green,blue,alpha)
        text=conky_parse([[${if_running clementine}
${if_empty ${exec /home/chino/bin/wclementineplaying.py -a}}
${else} Music: ${exec /home/chino/bin/wclementineplaying.py -a} - ${exec /home/chino/bin/wclementineplaying.py -t}
${endif}
${endif}]])
        cairo_show_text(cr,text)
        cairo_stroke(cr)
    end
    cairo_destroy(cr)
    cairo_surface_destroy(cs)
    cr=nil
end<commit_msg>draw SVG and raster image; better code styling<commit_after>--this is a lua script for use in conky
require 'cairo'
require 'imlib2'
require 'rsvg'
json = require 'json'

-- Draw SVG function
-- Usage:
-- draw_svg({x=0,y=0,h=20,w=20,file="/path/to/awesome.svg"})
function draw_svg(im)
    local x, y, w, h = nil, nil, nil, nil
    local file = nil
    x = (im.x or 0)
    y = (im.y or 0)
    w = (im.w or 0)
    h = (im.h or 0)
    file = tostring(im.file)
    if file ==  nil then print("need svg file") end
    -----------
    local handle = rsvg_handle_new_from_file(file)
    -- store current transformation matrix
    -- because we will use translate and scale to put svg graph
    local mat = cairo_matrix_t.create()
    cairo_get_matrix(im.cr,mat)

    local dimensions = RsvgDimensionData.create()
    rsvg_handle_get_dimensions(handle, dimensions)
    local x_factor = w / dimensions['width']
    local y_factor = h / dimensions['height']
    local scale_factor = math.min(x_factor, y_factor)

    cairo_translate(im.cr, x, y) -- translate bofore scale!
    cairo_scale(im.cr, scale_factor, scale_factor)
    rsvg_handle_render_cairo(handle, im.cr)

    -- restore transformation matrix
    cairo_set_matrix(im.cr, mat)

    rsvg_destroy_handle(handle)
    handle = nil
    dimensions = nil
    mat = nil
end -- function draw_svg

-- Image display function
-- https://github.com/brndnmtthws/conky/wiki/Using-Lua-scripts-in-conky:-Useful-functions-and-code#image-display-function
-- usage:
-- image({x=100,y=100,h=50,w=50,file="/home/username/cute_puppy.png"})
function draw_raster(im)
    local x, y, w, h = nil, nil, nil, nil
    local file = nil
    x = (im.x or 0)
    y = (im.y or 0)
    w = (im.w or 0)
    h = (im.h or 0)
    file = tostring(im.file)
    if file ==  nil then print("need svg file") end
    ---------------------------------------------
    local show = imlib_load_image(file)
    if show == nil then return end
    local width, height = 0, 0
    imlib_context_set_image(show)
    if tonumber(w) == 0 then
        width = imlib_image_get_width()
    else
        width = tonumber(w)
    end
    if tonumber(h) == 0 then
        height = imlib_image_get_height()
    else
        height = tonumber(h)
    end
    imlib_context_set_image(show)
    local scaled = imlib_create_cropped_scaled_image(
        0, 0,
        imlib_image_get_width(), imlib_image_get_height(),
        width, height)
    imlib_free_image()
    imlib_context_set_image(scaled)
    imlib_render_image_on_drawable(x, y)
    imlib_free_image()
    show = nil
end -- function draw_raster

------------------------------------------------------------------------
--    _______  _____  __   _ _     _ __   __ ______  _______  ______  --
--    |       |     | | \  | |____/    \_/   |_____] |_____| |_____/  --
--    |_____  |_____| |  \_| |    \_    |    |_____] |     | |    \_  --
------------------------------------------------------------------------
function conky_conkybar()
    if conky_window == nil then
        return
    end
    local cs = cairo_xlib_surface_create(
        conky_window.display,
        conky_window.drawable,
        conky_window.visual,
        conky_window.width,
        conky_window.height)
    local cr = cairo_create(cs)
    local updates = tonumber(conky_parse('${updates}'))
    if updates>3 then
        print ('W x H', conky_window.width, conky_window.height)
        local primary_font = 'Source Han Sans CN'
        local primary_font_size = 16
        local primary_font_slant = CAIRO_FONT_SLANT_NORMAL
        local primary_font_face = CAIRO_FONT_WEIGHT_NORMAL

        local red,green,blue,alpha = 1, 1, 1, 1
        local xpos, ypos = 0, 0
        local text = ''

        -- workspace indicator
        xpos, ypos = 3, 20
        red, green, blue, alpha = 1, 1, 1, 1
        cairo_move_to(cr, xpos, ypos)
        cairo_select_font_face(cr, primary_font, primary_font_slant, primary_font_face)
        cairo_set_font_size(cr, primary_font_size)
        cairo_set_source_rgba(cr, red, green, blue, alpha)

        local workspaces = json.decode(conky_parse('${exec i3-msg -t get_workspaces}'))
        -- parse ws here
        text = ''
        for i, w in ipairs(workspaces) do
          if w['visible'] == true then
            text = text .. ' (' .. w.num .. ') '
          else
            text = text .. ' ' .. w.num .. ' '
          end
        end
        cairo_show_text(cr, text)
        cairo_stroke(cr)

        -- debugging info
        xpos, ypos = 400, 20
        red, green, blue, alpha = 1, 1, 0, 1
        cairo_move_to(cr, xpos, ypos)
        cairo_select_font_face(cr, primary_font, primary_font_slant, primary_font_face)
        cairo_set_font_size(cr, primary_font_size)
        cairo_set_source_rgba(cr, red, green, blue, alpha)

        text='w=' .. conky_window.width .. ',h=' .. conky_window.height
        cairo_show_text(cr, text)
        cairo_stroke(cr)

        -- image test
        draw_raster({x = 100, y = 0,
            h = 27, w = 27,
            file = "/tmp/angry_sakamoto_by_leeh_chan-d3ic3rl.png"})
        draw_svg({cr = cr,
            x = 130, y = 0,
            h = 27, w = 27,
            file = "/tmp/aru-1x-1_050_1.svg"})

        -- date time
        xpos, ypos = 653, 20
        red, green, blue, alpha = 1, 1, 1, 1
        cairo_move_to(cr, xpos, ypos)
        cairo_select_font_face(cr, primary_font, primary_font_slant, primary_font_face)
        cairo_set_font_size(cr, primary_font_size)
        cairo_set_source_rgba(cr, red, green, blue, alpha)

        text = conky_parse('${time %a, %d %b %Y %T %z}')
        cairo_show_text(cr, text)
        cairo_stroke(cr)

        -- clementine playing
        xpos, ypos = 1300, 20
        red, green, blue, alpha = 1, 1, 1, 1
        cairo_move_to(cr, xpos, ypos)
        cairo_select_font_face(cr, primary_font, primary_font_slant, primary_font_face)
        cairo_set_font_size(cr, primary_font_size)
        cairo_set_source_rgba(cr, red, green, blue, alpha)
        text = conky_parse([[
              ${if_running clementine}
              ${if_empty ${exec /home/chino/bin/wclementineplaying.py -a}}
              ${else} Music: ${exec /home/chino/bin/wclementineplaying.py -a} - ${exec /home/chino/bin/wclementineplaying.py -t}
              ${endif}
              ${endif}
        ]])
        cairo_show_text(cr, text)
        cairo_stroke(cr)
    end
    cairo_destroy(cr)
    cairo_surface_destroy(cs)
    cr = nil
end
{'fraction_non_alphanumeric': 0.1288866085171883, 'fraction_numerical': 0.014879425346331451, 'mean_word_length': 2.1766623207301175, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 5, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1340823', 'n_tokens_mistral': 3511, 'n_tokens_neox': 3337, 'n_words': 680}