Skip to content

Commit

Permalink
refactor: migrate to vim.notify from prints
Browse files Browse the repository at this point in the history
  • Loading branch information
olexsmir committed Jun 24, 2022
1 parent b4fd34e commit 0b415c3
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 22 deletions.
8 changes: 8 additions & 0 deletions lua/gopher/_utils/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -36,4 +36,12 @@ return {

return false
end,

---@param msg string
---@param lvl string
notify = function(msg, lvl)
vim.defer_fn(function()
vim.notify(msg, lvl)
end, 0)
end,
}
7 changes: 4 additions & 3 deletions lua/gopher/gogenerate.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Job = require "plenary.job"
local u = require "gopher._utils"

---run "go generate"
return function(...)
Expand All @@ -15,11 +16,11 @@ return function(...)
args = cmd_args,
on_exit = function(_, retval)
if retval ~= 0 then
print("command exited with code " .. retval)
u.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error")
return
else
print "command runs successfully"
end

u.notify("go generate was success runned", "info")
end,
})
:start()
Expand Down
7 changes: 4 additions & 3 deletions lua/gopher/goget.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Job = require "plenary.job"
local u = require "gopher._utils"

---run "go get"
return function(...)
Expand All @@ -17,11 +18,11 @@ return function(...)
args = cmd_args,
on_exit = function(_, retval)
if retval ~= 0 then
print("command exited with code " .. retval)
u.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error")
return
else
print "command runs successfully"
end

u.notify("go get was success runned", "info")
end,
})
:start()
Expand Down
7 changes: 4 additions & 3 deletions lua/gopher/gomod.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Job = require "plenary.job"
local u = require "gopher._utils"

---run "go mod"
return function(...)
Expand All @@ -11,11 +12,11 @@ return function(...)
args = cmd_args,
on_exit = function(_, retval)
if retval ~= 0 then
print("command exited with code " .. retval)
u.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error")
return
else
print "command runs successfully"
end

u.notify("go mod was success runned", "info")
end,
})
:start()
Expand Down
5 changes: 3 additions & 2 deletions lua/gopher/gotests.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
local Job = require "plenary.job"
local ts_utils = require "gopher._utils.ts"
local u = require "gopher._utils"
local M = {}

---@param cmd_args table
Expand All @@ -10,11 +11,11 @@ local function run(cmd_args)
args = cmd_args,
on_exit = function(_, retval)
if retval ~= 0 then
print("command exited with code " .. retval)
u.notify("command 'go " .. unpack(cmd_args) .. "' exited with code " .. retval, "error")
return
end

print "unit test(s) generated"
u.notify("unit test(s) generated", "info")
end,
})
:start()
Expand Down
8 changes: 5 additions & 3 deletions lua/gopher/impl.lua
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
local Job = require "plenary.job"
local ts_utils = require "gopher._utils.ts"
local u = require "gopher._utils"

---@return string
local function get_struct()
local ns = ts_utils.get_struct_node_at_pos(unpack(vim.api.nvim_win_get_cursor(0)))
if ns == nil then
print "put cursor on struct or specify a receiver"
u.notify("put cursor on a struct or specify a receiver", "info")
return ""
end

Expand All @@ -26,7 +27,8 @@ return function(...)
iface = vim.fn.input "impl: generating method stubs for interface: "
vim.cmd "redeaw!"
if iface == "" then
print "usage: GoImpl f *File io.Reader"
u.notify("usage: GoImpl f *File io.Reader", "info")
return
end
elseif #args == 1 then -- :GoImpl io.Reader
recv = string.lower(recv) .. " *" .. recv
Expand Down Expand Up @@ -57,7 +59,7 @@ return function(...)
args = cmd_args,
on_exit = function(data, retval)
if retval ~= 0 then
print("command exited with code " .. retval)
u.notify("command 'impl " .. unpack(cmd_args) .. "' exited with code " .. retval, "error")
return
end

Expand Down
9 changes: 5 additions & 4 deletions lua/gopher/installer.lua
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
local Job = require "plenary.job"
local u = require "gopher._utils"
local urls = {
gomodifytags = "github.com/fatih/gomodifytags",
impl = "github.com/josharian/impl",
Expand All @@ -13,13 +14,13 @@ local function install(pkg)
:new({
command = "go",
args = { "install", url },
on_exit = function(_, ret_val)
if ret_val ~= 0 then
print("command exited with code " .. ret_val)
on_exit = function(_, retval)
if retval ~= 0 then
u.notify("command 'go install " .. url .. "' exited with code " .. retval, "error")
return
end

print("install " .. url .. " finished")
u.notify("install " .. url .. " finished", "info ")
end,
})
:start()
Expand Down
11 changes: 7 additions & 4 deletions lua/gopher/struct_tags.lua
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
local Job = require "plenary.job"
local ts_utils = require "gopher._utils.ts"
local utils = require "gopher._utils"
local u = require "gopher._utils"
local M = {}

local function modify(...)
Expand Down Expand Up @@ -46,7 +46,10 @@ local function modify(...)
args = cmd_args,
on_exit = function(data, retval)
if retval ~= 0 then
print("command exited with code " .. retval)
u.notify(
"command 'gomodifytags " .. unpack(cmd_args) .. "' exited with code " .. retval,
"error"
)
return
end

Expand All @@ -63,11 +66,11 @@ local function modify(...)
or tagged["start"] == nil
or tagged["start"] == 0
then
print("failed to set tags " .. vim.inspect(tagged))
u.notify("failed to set tags " .. vim.inspect(tagged), "error")
end

for i, v in ipairs(tagged.lines) do
tagged.lines[i] = utils.rtrim(v)
tagged.lines[i] = u.rtrim(v)
end

-- write goted tags
Expand Down

0 comments on commit 0b415c3

Please sign in to comment.