Skip to content

Commit

Permalink
chore: format
Browse files Browse the repository at this point in the history
  • Loading branch information
zztrieuzz committed Apr 17, 2024
1 parent 8b42f6f commit 9653847
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 11 deletions.
8 changes: 4 additions & 4 deletions lua/spectre/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,12 @@ local config = {
['tab'] = {
map = '<Tab>',
cmd = "<cmd>lua require('spectre').tab()<cr>",
desc = 'next query'
desc = 'next query',
},
['shift-tab'] = {
map = '<S-Tab>',
cmd = "<cmd>lua require('spectre').tab_shift()<cr>",
desc = 'previous query'
desc = 'previous query',
},
['toggle_line'] = {
map = 'dd',
Expand Down Expand Up @@ -197,8 +197,8 @@ local config = {
},
},
['sd'] = {
cmd = "sd",
options = { },
cmd = 'sd',
options = {},
},
},
default = {
Expand Down
15 changes: 8 additions & 7 deletions lua/spectre/replace/sd.lua
Original file line number Diff line number Diff line change
Expand Up @@ -15,34 +15,35 @@ sd.replace = function(self, value)

-- Read the original file
local lines = {}
local file = io.open(value.filename, "r")
local file = io.open(value.filename, 'r')
for line in file:lines() do
table.insert(lines, line)
end
file:close()

if value.lnum <= #lines then
-- Use `io.popen` to get the transformed line using `sd`
local command = string.format("echo '%s' | sd '%s' '%s'", lines[value.lnum], value.search_text, value.replace_text)
local command =
string.format("echo '%s' | sd '%s' '%s'", lines[value.lnum], value.search_text, value.replace_text)
local handle = io.popen(command, 'r')
if handle then
local transformedLine = handle:read("*a")
local transformedLine = handle:read('*a')
handle:close()
-- Replace the line in memory
lines[value.lnum] = transformedLine:gsub("\n$", "") -- Remove trailing newline added by `echo`
lines[value.lnum] = transformedLine:gsub('\n$', '') -- Remove trailing newline added by `echo`
else
self:on_error(false, value)
return
end
else
log.debug("Line number out of bounds.")
log.debug('Line number out of bounds.')
return
end

-- Write the modified lines back to the file
file = io.open(value.filename, "w")
file = io.open(value.filename, 'w')
for _, line in ipairs(lines) do
file:write(line, "\n")
file:write(line, '\n')
end
file:close()

Expand Down

0 comments on commit 9653847

Please sign in to comment.