From a4d5a653b16604695a1ee67521ed84f6766b3e21 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 31 Dec 2022 21:09:47 -0800 Subject: [PATCH] Format stub --- lua/guid.lua | 22 +++++++++++++++------- plugin/guid.lua | 8 +++++++- 2 files changed, 22 insertions(+), 8 deletions(-) diff --git a/lua/guid.lua b/lua/guid.lua index 6955017..31ae37f 100644 --- a/lua/guid.lua +++ b/lua/guid.lua @@ -20,8 +20,10 @@ local function guid_generate() return bytes end -local function guid_format(guid, style) - style = style or 'd' +local function guid_print(guid, style) + if style == '' then + style = 'd' + end -- Format specifier definition: -- https://learn.microsoft.com/en-us/dotnet/api/system.guid.tostring?view=net-7.0 @@ -45,20 +47,26 @@ local function guid_format(guid, style) format = '{0x%.2x%.2x%.2x%.2x,0x%.2x%.2x,0x%.2x%.2x,{0x%.2x,0x%.2x,0x%.2x,0x%.2x,0x%.2x,0x%.2x,0x%.2x,0x%.2x}}' end - local formatted = string.format(format, unpack(guid)) + local guid_printed = string.format(format, unpack(guid)) if style:upper() == style then - formatted = formatted:upper():gsub('X', 'x') + guid_printed = guid_printed:upper():gsub('X', 'x') end - return formatted + return guid_printed end local function guid_insert(style) local pos = get_cursor_pos() local guid = guid_generate() - insert_text_at_pos(guid_format(guid, style), pos) + local guid_printed = guid_print(guid, style) + insert_text_at_pos(guid_printed, pos) +end + +local function guid_format() + end return { - guid_insert = guid_insert + guid_format = guid_format, + guid_insert = guid_insert, } diff --git a/plugin/guid.lua b/plugin/guid.lua index aaa9be3..f9852ff 100644 --- a/plugin/guid.lua +++ b/plugin/guid.lua @@ -10,8 +10,14 @@ local function guid_insert(ctx) guid.guid_insert(ctx.args) end +local function guid_format(ctx) + guid_reload() + guid.guid_format(ctx.args) +end + if not vim.g.guid then - vim.api.nvim_create_user_command('GuidReload', guid_reload, {}) + vim.api.nvim_create_user_command('GuidFormat', guid_format, {nargs = '?'}) vim.api.nvim_create_user_command('GuidInsert', guid_insert, {nargs = '?'}) + vim.api.nvim_create_user_command('GuidReload', guid_reload, {}) vim.g.guid = true end