Skip to content

Commit

Permalink
Object 118
Browse files Browse the repository at this point in the history
  • Loading branch information
luozhiya committed Jan 7, 2025
1 parent aa81ab4 commit f8ec401
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 24 deletions.
17 changes: 15 additions & 2 deletions lua/fittencode/cc/projects/hash/init.lua
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
package.cpath = package.cpath .. ';' .. require('cpath')

local _, CC = pcall(require, 'hash')
local _, So = pcall(require, 'hash')
if not _ then
return
end

return CC
local M = {}

function M.is_supported(method)
return So.is_supported(method)
end

function M.hash(method, plaintext)
local _, ciphertext = pcall(So.hash, method, plaintext)
if _ then
return ciphertext
end
end

return M
16 changes: 9 additions & 7 deletions lua/fittencode/hash/hash.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,22 @@ local Fn = require('fittencode.fn')
local M = {}

function M.is_supported(method)
return CC[string.lower(method)]
return CC.is_supported(method)
end

function M.hash(method, plaintext, on_success, on_error)
if not M.is_supported(method) then
Fn.schedule_call(on_error)
return
end
local _, ciphertext = pcall(CC[string.lower(method)], plaintext)
if _ then
Fn.schedule_call(on_success, ciphertext)
else
Fn.schedule_call(on_error)
end
vim.schedule_wrap(function()
local ciphertext = CC.hash(method, plaintext)
if ciphertext then
Fn.schedule_call(on_success, ciphertext)
else
Fn.schedule_call(on_error)
end
end)
end

return M
11 changes: 4 additions & 7 deletions lua/fittencode/hash/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,13 @@ local M = {}
---@param plaintext string
---@return string?
function M.hash(method, plaintext, on_success, on_error)
method = string.lower(method)
if method == 'md5' then
if method == 'MD5' then
if Config.hash.md5.backend == 'hash' then
local CC = require('fittencode.hash.hash')
CC.hash(method, plaintext, on_success, on_error)
require('fittencode.hash.hash').hash(method, plaintext, on_success, on_error)
elseif Config.hash.md5.backend == 'md5sum' then
local MD5Sum = require('fittencode.hash.md5sum')
MD5Sum.hash(method, plaintext, on_success, on_error)
require('fittencode.hash.md5sum').hash(method, plaintext, on_success, on_error)
else
Log.error('Unsupported md5 backend: ' .. Config.hash.md5.backend)
Log.error('Unsupported MD5 backend: ' .. Config.hash.md5.backend)
Fn.schedule_call(on_error)
return
end
Expand Down
5 changes: 3 additions & 2 deletions lua/fittencode/hash/md5sum.lua
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ local Process = require('fittencode.process')

local M = {}

local md5sum = {
local md5sum_meta = {
cmd = 'md5sum',
args = {
'-', -- With no FILE, or when FILE is -, read standard input.
Expand All @@ -12,14 +12,15 @@ local md5sum = {
}

function M.is_supported(method)
return string.lower(method) == 'md5'
return method == 'MD5'
end

function M.hash(method, plaintext, on_success, on_error)
if not M.is_supported(method) then
Fn.schedule_call(on_error)
return
end
local md5sum = vim.deepcopy(md5sum_meta)
Process.spawn(md5sum, {
on_input = vim.schedule_wrap(function()
return plaintext
Expand Down
8 changes: 2 additions & 6 deletions lua/fittencode/inline/prompt.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,9 @@ local last_filename = ''
local last_text = ''
local last_ciphertext = ''

local function hash(t)
return Hash.hash('md5', t)
end

local function aVe(filename, text, on_success, on_error)
Promise:new(function(resolve, reject)
Hash.hash('md5', text, function(ciphertext)
Hash.hash('MD5', text, function(ciphertext)
resolve(ciphertext)
end, function()
reject()
Expand All @@ -68,7 +64,7 @@ local function aVe(filename, text, on_success, on_error)
filename = filename
})
else
local indices = vim.diff(last_text, text, {result_type = 'indices'})
local indices = vim.diff(last_text, text, { result_type = 'indices' })

local n = 0
while n < #text and n < #last_text and text:sub(n + 1, n + 1) == last_text:sub(n + 1, n + 1) do
Expand Down

0 comments on commit f8ec401

Please sign in to comment.