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

feat: go install option and extracted prev into agnostic function #139

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
53 changes: 44 additions & 9 deletions lua/glow.lua
Original file line number Diff line number Diff line change
Expand Up @@ -181,15 +181,15 @@ local function open_window(cmd_args)
end
end

---@return string
---@return string, string
local function release_file_url()
local os, arch
local version = "1.5.1"

-- check pre-existence of required programs
if vim.fn.executable("curl") == 0 or vim.fn.executable("tar") == 0 then
err("curl and/or tar are required")
return ""
return "", ""
end

-- local raw_os = jit.os
Expand All @@ -215,12 +215,12 @@ local function release_file_url()

if os == nil or arch == nil then
err("os not supported or could not be parsed")
return ""
return "", ""
end

-- create the url, filename based on os and arch
local filename = "glow_" .. os .. "_" .. arch .. (os == "Windows" and ".zip" or ".tar.gz")
return "https://github.com/charmbracelet/glow/releases/download/v" .. version .. "/" .. filename
return "https://github.com/charmbracelet/glow/releases/download/v" .. version .. "/" .. filename, os
end

---@return boolean
Expand Down Expand Up @@ -296,16 +296,44 @@ local function run(opts)
open_window(cmd_args)
end

local function install_glow(opts)
local release_url = release_file_url()
---Installation process using `go install` command
---@param opts table
local function go_install_glow(opts)
if vim.fn.exepath("go") == "" then
return
end

local download_command = { "go", "install", "github.com/charmbracelet/glow@latest" }

local callbacks = {
on_sterr = vim.schedule_wrap(function(_, data, _)
local out = table.concat(data, "\n")
err(out)
end),
on_exit = vim.schedule_wrap(function()
vim.notify("go install github.com/charmbracelet/glow@latest completed", vim.log.levels.INFO, { title = "Glow" })
glow.config.glow_path = vim.fn.exepath("glow")
run(opts)
end),
}
vim.fn.jobstart(download_command, callbacks)
end

---Installation process independent from `go` command and knowledgeable of the OS
---@param opts table
local function agnostic_install_glow(opts)
local release_url, os = release_file_url()
if release_url == "" then
return
end

local install_path = glow.config.install_path
local download_command = { "curl", "-sL", "-o", "glow.tar.gz", release_url }
local extract_command = { "tar", "-zxf", "glow.tar.gz", "-C", install_path }
local output_filename = "glow.tar.gz"
local output_filename = (os == "Windows" and "glow.zip" or "glow.tar.gz")
local download_command = { "curl", "-sL", "-o", output_filename, release_url }
local extract_command = (
os == "Windows" and { "Expand-Archive", output_filename, install_path }
or { "tar", "-zxf", output_filename, "-C", install_path }
)
---@diagnostic disable-next-line: missing-parameter
local binary_path = vim.fn.expand(table.concat({ install_path, "glow" }, "/"))

Expand Down Expand Up @@ -346,6 +374,13 @@ local function install_glow(opts)
vim.fn.jobstart(download_command, callbacks)
end

local function install_glow(opts)
if vim.fn.exepath("go") ~= "" then
go_install_glow(opts)
end
agnostic_install_glow(opts)
end

---@return string
local function get_executable()
if glow.config.glow_path ~= "" then
Expand Down