Skip to content
This repository was archived by the owner on Aug 12, 2025. It is now read-only.
Open
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
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .editorconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
root = true

[*]
indent_style = space
indent_size = 2
charset = utf-8
end_of_file = "lf"
trim_trailing_whitespace = true
insert_final_newline = false

[Makefile]
indent_style = tab
indent_size = 4

[*.yml]
indent_style = space
indent_size = 2

# This makes it a lot less painful to write YAMLs in docs
[*.md]
indent_style = space
indent_size = 2
42 changes: 42 additions & 0 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
name: lint

on:
push:
branches:
- main
paths:
- "lua/**/*.lua"
pull_request:
types: [opened, synchronize]
branches:
- main
paths:
- "lua/**/*.lua"

jobs:
lint:
name: Selene check
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4

- name: Run selene
uses: NTBBloodbath/[email protected]
with:
token: ${{ secrets.GITHUB_TOKEN }}
args: --display-style quiet lua/freeze

style-lint:
name: Stylua check
runs-on: ubuntu-latest
steps:
- name: Checkout sources
uses: actions/checkout@v4

- name: Lint with stylua
uses: JohnnyMorganz/stylua-action@v4
with:
token: ${{ secrets.GITHUB_TOKEN }}
version: latest
args: --color always --check .
15 changes: 15 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
.PHONY: lint style-lint lint-all

lint:
@printf "\nRunning selene\n"
@selene --display-style quiet lua/freeze

style-lint:
@printf "\nRunning stylua\n"
@stylua --color always --check .

lint-all: lint style-lint

format:
@printf "\nFormatting with stylua\n"
@stylua --color always .
230 changes: 115 additions & 115 deletions lua/freeze/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -2,61 +2,61 @@ local loop = vim.loop
local default_output = "freeze.png"

local freeze = {
opts = {
dir = ".",
output = default_output,
theme = "default",
config = "base",
open = false,
},
output = nil,
opts = {
dir = ".",
output = default_output,
theme = "default",
config = "base",
open = false,
},
output = nil,
}
local stdio = { stdout = "", stderr = "" }

---The callback for reading stdout.
---@param err any the possible err we received
---@param data any the possible data we received in stdout
local function onReadStdOut(err, data)
if err then
vim.notify(err, vim.log.levels.ERROR, { title = "Freeze" })
end
if data then
stdio.stdout = stdio.stdout .. data
end
if freeze.opts.open and freeze.output ~= nil then
freeze.open(freeze.output)
freeze.output = nil
end
if err then
vim.notify(err, vim.log.levels.ERROR, { title = "Freeze" })
end
if data then
stdio.stdout = stdio.stdout .. data
end
if freeze.opts.open and freeze.output ~= nil then
freeze.open(freeze.output)
freeze.output = nil
end
end

---The callback for reading stderr.
---@param err any the possible err we received
---@param data any the possible data we received in stderr
local function onReadStdErr(err, data)
if err then
vim.notify(err, vim.log.levels.ERROR, { title = "Freeze" })
end
if data then
stdio.stderr = stdio.stderr .. data
end
if err then
vim.notify(err, vim.log.levels.ERROR, { title = "Freeze" })
end
if data then
stdio.stderr = stdio.stderr .. data
end
end

---The function called on exit of from the event loop
---@param stdout any the stdout pipe used by vim.loop
---@param stderr any the stderr pipe used by vim.loop
---@return function cb the wrapped schedule function callback
local function onExit(stdout, stderr)
return vim.schedule_wrap(function(code, _)
if code == 0 then
vim.notify("Successfully frozen 🍦", vim.log.levels.INFO, { title = "Freeze" })
else
vim.notify(stdio.stdout, vim.log.levels.ERROR, { title = "Freeze" })
end
stdout:read_stop()
stderr:read_stop()
stdout:close()
stderr:close()
end)
return vim.schedule_wrap(function(code, _)
if code == 0 then
vim.notify("Successfully frozen 🍦", vim.log.levels.INFO, { title = "Freeze" })
else
vim.notify(stdio.stdout, vim.log.levels.ERROR, { title = "Freeze" })
end
stdout:read_stop()
stderr:read_stop()
stdout:close()
stderr:close()
end)
end

--- The main function used for passing the main config to lua
Expand All @@ -66,103 +66,103 @@ end
--- @param start_line number the starting line to pass to freeze
--- @param end_line number the ending line to pass to freeze
function freeze.freeze(start_line, end_line)
if vim.fn.executable("freeze") ~= 1 then
vim.notify("`freeze` not found!", vim.log.levels.WARN, { title = "Freeze" })
return
end
if vim.fn.executable("freeze") ~= 1 then
vim.notify("`freeze` not found!", vim.log.levels.WARN, { title = "Freeze" })
return
end

local language = vim.api.nvim_buf_get_option(0, "filetype")
local file = vim.api.nvim_buf_get_name(0)
local config = freeze.opts.config
local dir = freeze.opts.dir
local stdout = loop.new_pipe(false)
local stderr = loop.new_pipe(false)
local output = freeze.opts.output
local language = vim.api.nvim_buf_get_option(0, "filetype")
local file = vim.api.nvim_buf_get_name(0)
local config = freeze.opts.config
local dir = freeze.opts.dir
local stdout = loop.new_pipe(false)
local stderr = loop.new_pipe(false)
local output = freeze.opts.output

if freeze.opts.output ~= default_output then
local timestamp = os.date("%Y%m%d%H%M%S")
local filename = file:match("^.+/(.+)$") or file
if freeze.opts.output ~= default_output then
local timestamp = os.date("%Y%m%d%H%M%S")
local filename = file:match("^.+/(.+)$") or file

output = output:gsub("{timestamp}", timestamp)
output = output:gsub("{filename}", filename)
output = output:gsub("{start_line}", start_line)
output = output:gsub("{end_line}", end_line)
end
output = output:gsub("{timestamp}", timestamp)
output = output:gsub("{filename}", filename)
output = output:gsub("{start_line}", start_line)
output = output:gsub("{end_line}", end_line)
end

freeze.output = dir .. "/" .. output
freeze.output = dir .. "/" .. output

local handle = loop.spawn("freeze", {
args = {
"--output",
freeze.output,
"--language",
language,
"--lines",
start_line .. "," .. end_line,
"--config",
config,
"--theme",
freeze.opts.theme,
file,
},
stdio = { nil, stdout, stderr },
}, onExit(stdout, stderr))
if not handle then
vim.notify("Failed to spawn freeze", vim.log.levels.ERROR, { title = "Freeze" })
end
if stdout ~= nil then
loop.read_start(stdout, onReadStdOut)
end
if stderr ~= nil then
loop.read_start(stderr, onReadStdErr)
end
local handle = loop.spawn("freeze", {
args = {
"--output",
freeze.output,
"--language",
language,
"--lines",
start_line .. "," .. end_line,
"--config",
config,
"--theme",
freeze.opts.theme,
file,
},
stdio = { nil, stdout, stderr },
}, onExit(stdout, stderr))
if not handle then
vim.notify("Failed to spawn freeze", vim.log.levels.ERROR, { title = "Freeze" })
end
if stdout ~= nil then
loop.read_start(stdout, onReadStdOut)
end
if stderr ~= nil then
loop.read_start(stderr, onReadStdErr)
end
end

--- Opens the last created image in macOS using `open`.
--- @param filename string the filename to open
function freeze.open(filename)
if vim.fn.executable("open") ~= 1 then
vim.notify("`open` not found!", vim.log.levels.WARN, { title = "Freeze" })
return
end
if vim.fn.executable("open") ~= 1 then
vim.notify("`open` not found!", vim.log.levels.WARN, { title = "Freeze" })
return
end

local stdout = loop.new_pipe(false)
local stderr = loop.new_pipe(false)
local handle = loop.spawn("open", {
args = {
filename,
},
stdio = { nil, stdout, stderr },
}, onExit(stdout, stderr))
if not handle then
vim.notify("Failed to spawn freeze", vim.log.levels.ERROR, { title = "Freeze" })
end
if stdout ~= nil then
loop.read_start(stdout, onReadStdOut)
end
if stderr ~= nil then
loop.read_start(stderr, onReadStdErr)
end
local stdout = loop.new_pipe(false)
local stderr = loop.new_pipe(false)
local handle = loop.spawn("open", {
args = {
filename,
},
stdio = { nil, stdout, stderr },
}, onExit(stdout, stderr))
if not handle then
vim.notify("Failed to spawn freeze", vim.log.levels.ERROR, { title = "Freeze" })
end
if stdout ~= nil then
loop.read_start(stdout, onReadStdOut)
end
if stderr ~= nil then
loop.read_start(stderr, onReadStdErr)
end
end

--- Setup function for enabling both user commands.
--- Sets up :Freeze for freezing a selection and :FreezeLine
--- to freeze a single line.
function freeze.setup(plugin_opts)
for k, v in pairs(plugin_opts) do
freeze.opts[k] = v
end
vim.api.nvim_create_user_command("Freeze", function(opts)
if opts.count > 0 then
freeze.freeze(opts.line1, opts.line2)
else
freeze.freeze(1, vim.api.nvim_buf_line_count(0))
end
end, { range = true })
vim.api.nvim_create_user_command("FreezeLine", function(_)
local line = vim.api.nvim_win_get_cursor(0)[1]
freeze.freeze(line, line)
end, {})
for k, v in pairs(plugin_opts) do
freeze.opts[k] = v
end
vim.api.nvim_create_user_command("Freeze", function(opts)
if opts.count > 0 then
freeze.freeze(opts.line1, opts.line2)
else
freeze.freeze(1, vim.api.nvim_buf_line_count(0))
end
end, { range = true })
vim.api.nvim_create_user_command("FreezeLine", function(_)
local line = vim.api.nvim_win_get_cursor(0)[1]
freeze.freeze(line, line)
end, {})
end

return freeze
27 changes: 27 additions & 0 deletions neovim.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
---
base: lua51

globals:
jit:
any: true
vim:
any: true
assert:
args:
- type: bool
- type: string
required: false
after_each:
args:
- type: function
before_each:
args:
- type: function
describe:
args:
- type: string
- type: function
it:
args:
- type: string
- type: function
Loading