Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
93 changes: 93 additions & 0 deletions examples/add_image.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
-- !exe [luafile %]

local h = require('hologram')
local state = require('hologram.state')
local cairo = require('hologram.cairo.cairo')

-- Draw image using cairo

local width = state.dimensions.cell_pixels.width * 5
local height = state.dimensions.cell_pixels.height * 3
local surface = cairo.image_surface('argb32', width, height)
local cr = surface:context()
cr:line_width(2) cr:rgba(1.0, 0.0, 0.0, 1.0)
cr:rectangle(0, 0, width, height)
cr:stroke()
cr:save()
cr:translate(width / 2.0, height / 2.0)
cr:scale(5, 5)
cr:arc(0.0, 0.0, 1.0, 0.0, 2 * math.pi)
cr:stroke_preserve()
cr:restore()
surface:flush()

h.add_image(surface, { buffer = 0, row = 0, col = 0 })






-- h.add_image('/home/romgrk/img/lena.png', { buf = 0, row = 32, col = 0 })


-- Send a custom created bitmap

-- local red_100 = {255, 0, 0, 255}
-- local red_50 = {255, 0, 0, 100}
--
-- local function fill(length, value)
-- local array = {}
-- for i = 1, length do
-- table.insert(array, value)
-- end
-- return array
-- end
--
-- local function concat(tables)
-- local result = {}
-- for _, t in ipairs(tables) do
-- for _, value in ipairs(t) do
-- table.insert(result, value)
-- end
-- end
-- return result
-- end
--
-- local size = 50
-- local width = 1
-- local data = concat({
-- { fill(size, red_100) },
-- fill(size, concat({ { red_100 }, fill(size - width * 2, red_50), { red_100 } })),
-- { fill(size, red_100) },
-- })
--
-- h.add_image(data, { buf = 0, col = 17, 0 })




























90 changes: 90 additions & 0 deletions lua/hologram/api.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
local Image = require('hologram.image')
local vim = _G.vim

local hologram = {}

-- Get all extmarks in viewport (and within winwidth/2 of viewport bounds)
function hologram.get_ext_loclist(buf)
if buf == 0 then buf = vim.api.nvim_get_current_buf() end
local top = vim.fn.line('w0')
local bot = vim.fn.line('w$')

local view_top = math.floor(math.max(0, top-(bot-top)/2))
local view_bot = math.floor(bot+(bot-top)/2)

return vim.api.nvim_buf_get_extmarks(buf,
vim.g.hologram_extmark_ns,
{view_top, 0},
{view_bot, -1},
{})
end

function hologram.update_images(buf)
if buf == 0 then buf = vim.api.nvim_get_current_buf() end

for _, ext_loc in ipairs(hologram.get_ext_loclist(0)) do
local ext, _, _ = unpack(ext_loc)

local image = hologram.get_image(buf, ext)
image:adjust()
end
end

function hologram.clear_images(buf)
if buf == 0 then
buf = vim.api.nvim_get_current_buf()
end

for _, image in ipairs(Image.instances) do
if buf == nil or image.buffer == buf then
image:delete({ free = true })
end
end
end

-- @param data
-- @param opts
function hologram.add_image(data, opts)
local img = Image.new(data, opts)
img:transmit()
return img
end

-- Return image in 'buf' linked to 'ext'
function hologram.get_image(buf, ext)
if buf == 0 then buf = vim.api.nvim_get_current_buf() end

local img = nil
for _, i in ipairs(Image.instances) do
if i.buffer == buf and i.extmark == ext then
img = i
end
end
return img
end

function hologram.gen_images(buf, ft)
if buf == 0 then buf = vim.api.nvim_get_current_buf() end
ft = ft or vim.bo.filetype

local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)

if ft == 'markdown' then
for row, line in ipairs(lines) do
local image_link = line:match('!%[.-%]%(.-%)')
if image_link then
local source = image_link:match('%((.+)%)')
hologram.add_image(buf, source, row, 0)
end
end
end
end

function hologram.create_autocmds()
vim.cmd("augroup Hologram")
vim.cmd("autocmd!")
vim.cmd("silent autocmd WinScrolled * :lua require('hologram').update_images(0)")
vim.cmd("augroup END")
end

return hologram
176 changes: 176 additions & 0 deletions lua/hologram/base64.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
-- base64 -- v1.5.3 public domain Lua base64 encoder/decoder
-- no warranty implied; use at your own risk
--
-- Author: Ilya Kolbin ([email protected])
-- URL: https://github.com/iskolbin/lbase64/blob/master/base64.lua
--
-- LICENSE
-- See end of file for license information.

local base64 = {}

local shl, shr, band = _G.bit.lshift, _G.bit.rshift, _G.bit.band
local extract = function(v, from, width)
return band(shr(v, from), shl(1, width) - 1)
end


function base64.makeencoder(s62, s63, spad)
local encoder = {}
for b64code, char in pairs{[0]='A','B','C','D','E','F','G','H','I','J',
'K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y',
'Z','a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z','0','1','2',
'3','4','5','6','7','8','9',s62 or '+',s63 or'/',spad or'='} do
encoder[b64code] = char:byte()
end
return encoder
end

function base64.makedecoder(s62, s63, spad)
local decoder = {}
for b64code, charcode in pairs(base64.makeencoder(s62, s63, spad)) do
decoder[charcode] = b64code
end
return decoder
end

local DEFAULT_ENCODER = base64.makeencoder()
local DEFAULT_DECODER = base64.makedecoder()

local char, concat = string.char, table.concat

function base64.encode(input)
local encoder = DEFAULT_ENCODER

local t = {}
local k = 1
local n = #input
local lastn = n % 3

for i = 1, n-lastn, 3 do
local a, b, c = input:byte(i, i + 2)
local v = a * 0x10000 + b * 0x100 + c
local s = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[extract(v,0,6)])
t[k] = s
k = k + 1
end

if lastn == 2 then
local a, b = input:byte(n-1, n)
local v = a*0x10000 + b*0x100
t[k] = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[64])
elseif lastn == 1 then
local v = input:byte(n) * 0x10000
t[k] = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[64], encoder[64])
end

return concat(t)
end

function base64.encode_bytes(bytes, length)
local encoder = DEFAULT_ENCODER

local t = {}
local k = 1
local n = length or #bytes
local lastn = n % 3

for i = 1, n-lastn, 3 do
local a, b, c = bytes[i], bytes[i + 1], bytes[i + 2]
local v = a * 0x10000 + b * 0x100 + c
local s = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[extract(v,0,6)])
t[k] = s
k = k + 1
end

if lastn == 2 then
local a, b = bytes[n - 1], bytes[n]
local v = a*0x10000 + b*0x100
t[k] = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[extract(v,6,6)], encoder[64])
elseif lastn == 1 then
local v = bytes[n] * 0x10000
t[k] = char(encoder[extract(v,18,6)], encoder[extract(v,12,6)], encoder[64], encoder[64])
end

return concat(t)
end

function base64.decode(b64, decoder)
decoder = decoder or DEFAULT_DECODER
local pattern = '[^%w%+%/%=]'
if decoder then
local s62, s63
for charcode, b64code in pairs(decoder) do
if b64code == 62 then s62 = charcode
elseif b64code == 63 then s63 = charcode
end
end
pattern = ('[^%%w%%%s%%%s%%=]'):format(char(s62), char(s63))
end
b64 = b64:gsub(pattern, '')
local t, k = {}, 1
local n = #b64
local padding = b64:sub(-2) == '==' and 2 or b64:sub(-1) == '=' and 1 or 0
for i = 1, padding > 0 and n-4 or n, 4 do
local a, b, c, d = b64:byte(i, i+3)
local v = decoder[a]*0x40000 + decoder[b]*0x1000 + decoder[c]*0x40 + decoder[d]
local s = char(extract(v,16,8), extract(v,8,8), extract(v,0,8))
t[k] = s
k = k + 1
end
if padding == 1 then
local a, b, c = b64:byte(n-3, n-1)
local v = decoder[a]*0x40000 + decoder[b]*0x1000 + decoder[c]*0x40
t[k] = char(extract(v,16,8), extract(v,8,8))
elseif padding == 2 then
local a, b = b64:byte(n-3, n-2)
local v = decoder[a]*0x40000 + decoder[b]*0x1000
t[k] = char(extract(v,16,8))
end
return concat(t)
end

return base64

--[[
------------------------------------------------------------------------------
This software is available under 2 licenses -- choose whichever you prefer.
------------------------------------------------------------------------------
ALTERNATIVE A - MIT License
Copyright (c) 2018 Ilya Kolbin
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
------------------------------------------------------------------------------
ALTERNATIVE B - Public Domain (www.unlicense.org)
This is free and unencumbered software released into the public domain.
Anyone is free to copy, modify, publish, use, compile, sell, or distribute this
software, either in source code form or as a compiled binary, for any purpose,
commercial or non-commercial, and by any means.
In jurisdictions that recognize copyright laws, the author or authors of this
software dedicate any and all copyright interest in the software to the public
domain. We make this dedication for the benefit of the public at large and to
the detriment of our heirs and successors. We intend this dedication to be an
overt act of relinquishment in perpetuity of all present and future rights to
this software under copyright law.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
------------------------------------------------------------------------------
--]]
Binary file added lua/hologram/cairo/bin/linux64/libcairo.a
Binary file not shown.
Binary file added lua/hologram/cairo/bin/linux64/libcairo.so
Binary file not shown.
Binary file added lua/hologram/cairo/bin/mingw64/cairo.a
Binary file not shown.
Binary file added lua/hologram/cairo/bin/mingw64/cairo.dll
Binary file not shown.
Binary file added lua/hologram/cairo/bin/osx64/libcairo.a
Binary file not shown.
Binary file added lua/hologram/cairo/bin/osx64/libcairo.dylib
Binary file not shown.
Loading