From 7b27ad11166e4758b6cf9b01f66f56d4aa7c1ef7 Mon Sep 17 00:00:00 2001 From: Alex Yatskov Date: Sat, 31 Dec 2022 19:51:09 -0800 Subject: [PATCH] Generate guid --- lua/guid.lua | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/lua/guid.lua b/lua/guid.lua index 0dd9257..1c2e842 100644 --- a/lua/guid.lua +++ b/lua/guid.lua @@ -1,5 +1,33 @@ -local function guid_insert() +local function get_cursor_pos() + local _, row, col, _ = unpack(vim.fn.getpos('.')) + return {row = row, col = col} +end +local function guid_generate() + local bytes = {} + for i = 1, 16 do + bytes[i] = math.random(0, 255) + end + return bytes +end + +local function guid_format(guid) + return string.format('%.2x%.2x%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x-%.2x%.2x%.2x%.2x%.2x%.2x', unpack(guid)) +end + +local function insert_text_at_pos(text, pos) + local line = vim.fn.getline(pos.row) + ---@diagnostic disable-next-line: param-type-mismatch + local prefix = string.sub(line, 0, pos.col - 1) + ---@diagnostic disable-next-line: param-type-mismatch + local suffix = string.sub(line, pos.col) + vim.fn.setline(pos.row, prefix .. text .. suffix) +end + +local function guid_insert() + local pos = get_cursor_pos() + local guid = guid_generate() + insert_text_at_pos(guid_format(guid), pos) end return {