Skip to content

Commit

Permalink
feat: handle options
Browse files Browse the repository at this point in the history
  • Loading branch information
rawnly committed Mar 15, 2023
1 parent 8ca0c37 commit 01d5964
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
7 changes: 7 additions & 0 deletions lua/gist/core/utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,13 @@ function M.parseArgs(args)

for _, arg in ipairs(vim.split(args, " ", {})) do
local key, value = unpack(vim.split(arg, "=", { plain = true }))

if value == "true" then
value = true
elseif value == "false" then
value = false
end

parsed[key] = value
end

Expand Down
29 changes: 22 additions & 7 deletions lua/gist/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,19 @@ local core = require("gist.core.gh")

local M = {}

local function get_details(default_description)
local function get_details(ctx)
local config = core.read_config()

local filename = vim.fn.expand("%:t")
local description = default_description or vim.fn.input("Gist description: ")
local is_private = config.is_private or vim.fn.input("Create a private Gist? (y/n): ") == "y"
local description = ctx.description or vim.fn.input("Gist description: ")

local is_private

if ctx.public ~= nil then
is_private = not ctx.public
else
is_private = config.public or vim.fn.input("Create a private Gist? (y/n): ") == "y"
end

return {
filename = filename,
Expand All @@ -17,9 +24,9 @@ local function get_details(default_description)
}
end

local function create(content, desc)
local function create(content, ctx)
local config = core.read_config()
local details = get_details(desc)
local details = get_details(ctx)

local url, err = core.create_gist(details.filename, content, details.description, details.is_private)

Expand All @@ -34,6 +41,7 @@ end
--- Creates a Gist from the current selection
function M.create(opts)
local content = nil
local args = utils.parseArgs(opts.args)

local start_line = opts.line1
local end_line = opts.line2
Expand All @@ -43,14 +51,21 @@ function M.create(opts)
content = utils.get_current_selection(start_line, end_line)
end

return create(content, description)
return create(content, {
description = description,
public = args.public,
})
end

--- Creates a Gist from the current file.
function M.create_from_file(opts)
local args = utils.parseArgs(opts.args)
local description = opts.fargs[1]

create(nil, description)
create(nil, {
description = description,
public = args.public,
})
end

return M
1 change: 0 additions & 1 deletion plugin/gist.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
local gist = require("gist")
local utils = require("gist.core.utils")

vim.api.nvim_create_user_command("CreateGistFromFile", gist.create_from_file, {
nargs = "?",
Expand Down

0 comments on commit 01d5964

Please sign in to comment.