From: unknown <dalal.chinmay.0101@gmail.com>
---
lua/cphelper/definitions.lua | 6 ++--
lua/cphelper/helpers.lua | 59 +++++++++++++++++++++---------------
plugin/cphelper.vim | 8 ++---
3 files changed, 41 insertions(+), 32 deletions(-)
diff --git a/lua/cphelper/definitions.lua b/lua/cphelper/definitions.lua
index d53bd4a..439b8cc 100644
--- a/lua/cphelper/definitions.lua
+++ b/lua/cphelper/definitions.lua
@@ -5,11 +5,11 @@ return {
rust = "rustc solution.rs -o rust.out",
},
run_cmd = {
- c = "./c.out",
- cpp = "./cpp.out",
+ c = vim.fn.has('win32') == 1 and ".\\c.out" or "./c.out",
+ cpp = vim.fn.has("win32") == 1 and ".\\cpp.out" or "./cpp.out",
lua = "lua solution.lua",
python = "python solution.py",
- rust = "./rust.out",
+ rust = vim.fn.has('win32') == 1 and ".\\rust.out" or "./rust.out",
},
extensions = {
c = "c",
diff --git a/lua/cphelper/helpers.lua b/lua/cphelper/helpers.lua
index 794c2dc..b7b5060 100644
--- a/lua/cphelper/helpers.lua
+++ b/lua/cphelper/helpers.lua
@@ -6,11 +6,11 @@ local M = {}
function M.sanitize(s)
local unwanted = { "-", " ", "#", "%.", ":", "'", "+", "%%" }
for _, char in pairs(unwanted) do
- local pos = string.find(s, char)
- while pos do
- s = string.sub(s, 1, pos - 1) .. string.sub(s, pos + 1)
- pos = string.find(s, char)
- end
+ local pos = string.find(s, char)
+ while pos do
+ s = string.sub(s, 1, pos - 1) .. string.sub(s, pos + 1)
+ pos = string.find(s, char)
+ end
end
return s
end
@@ -21,12 +21,21 @@ end
--- @return boolean #True if both the tables are equal
function M.comparetables(t1, t2)
if #t1 ~= #t2 then
- return false
+ return false
+ end
+ if type(t1[1]) == 'string' then
+ if vim.fn.has('win32') == 1 then
+ for k, _ in pairs(t1) do
+ t1[k] = string.gsub(t1[k], "\r", "")
+ t2[k] = string.gsub(t2[k], "\r", "")
+ end
+ end
end
for k, v in pairs(t1) do
- if t2[k] ~= v then
- return false
- end
+ if t2[k] ~= t1[k] then
+ vim.api.nvim_err_writeln(t1[k] .. "!=".. t2[k])
+ return false
+ end
end
return true
end
@@ -35,24 +44,24 @@ end
-- Credits: Christian Clason and Hirokazu Hata
local function pad(contents, opts)
vim.validate({
- contents = { contents, "t" },
- opts = { opts, "t", true },
+ contents = { contents, "t" },
+ opts = { opts, "t", true },
})
opts = opts or {}
local left_padding = (" "):rep(opts.pad_left or 1)
local right_padding = (" "):rep(opts.pad_right or 1)
for i, line in ipairs(contents) do
- contents[i] = string.format("%s%s%s", left_padding, line:gsub("\r", ""), right_padding)
+ contents[i] = string.format("%s%s%s", left_padding, line:gsub("\r", ""), right_padding)
end
if opts.pad_top then
- for _ = 1, opts.pad_top do
- table.insert(contents, 1, "")
- end
+ for _ = 1, opts.pad_top do
+ table.insert(contents, 1, "")
+ end
end
if opts.pad_bottom then
- for _ = 1, opts.pad_bottom do
- table.insert(contents, "")
- end
+ for _ = 1, opts.pad_bottom do
+ table.insert(contents, "")
+ end
end
return contents
end
@@ -65,13 +74,13 @@ function M.display_right(contents)
local width = math.floor(vim.o.columns * 0.5)
local height = math.floor(vim.o.lines * 0.9)
vim.api.nvim_open_win(bufnr, true, {
- border = vim.g.cphborder or "rounded",
- style = "minimal",
- relative = "editor",
- row = math.floor(((vim.o.lines - height) / 2) - 1),
- col = math.floor(vim.o.columns - width - 1),
- width = width,
- height = height,
+ border = vim.g.cphborder or "rounded",
+ style = "minimal",
+ relative = "editor",
+ row = math.floor(((vim.o.lines - height) / 2) - 1),
+ col = math.floor(vim.o.columns - width - 1),
+ width = width,
+ height = height,
})
contents = pad(contents, { pad_top = 1 })
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, contents)
diff --git a/plugin/cphelper.vim b/plugin/cphelper.vim
index 0ff2865..977d108 100644
--- a/plugin/cphelper.vim
+++ b/plugin/cphelper.vim
@@ -1,7 +1,7 @@
command CphReceive lua require 'cphelper.receive'.receive()
command CphStop lua require 'cphelper.receive'.stop()
-command -nargs=* CphTest lua require 'cphelper.process_tests'.process(<f-args>)
-command -nargs=* CphRetest lua require 'cphelper.process_tests'.process_retests(<f-args>)
-command -nargs=+ CphDelete lua require 'cphelper.modify_tc'.deletetc(<f-args>)
-command -nargs=1 CphEdit lua require 'cphelper.modify_tc'.edittc(<f-args>)
+command -nargs=* CphTest silent lcd %:p:h | lua require 'cphelper.process_tests'.process(<f-args>)
+command -nargs=* CphRetest silent lcd %:p:h | lua require 'cphelper.process_tests'.process_retests(<f-args>)
+command -nargs=+ CphDelete silent lcd %:p:h | lua require 'cphelper.modify_tc'.deletetc(<f-args>)
+command -nargs=1 CphEdit silent lcd %:p:h | lua require 'cphelper.modify_tc'.edittc(<f-args>)
highlight Underline gui=underline cterm=underline
--
2.33.0.windows.2
From 3518e400f780117488711cf269de26347b6e2da4 Mon Sep 17 00:00:00 2001
From: unknown <dalal.chinmay.0101@gmail.com>
Date: Sat, 18 Sep 2021 19:54:41 +0530
Subject: [PATCH cphelper.nvim 2/5] Stylua
---
lua/cphelper/definitions.lua | 4 +--
lua/cphelper/helpers.lua | 65 ++++++++++++++++++------------------
2 files changed, 34 insertions(+), 35 deletions(-)
diff --git a/lua/cphelper/definitions.lua b/lua/cphelper/definitions.lua
index 439b8cc..3391b80 100644
--- a/lua/cphelper/definitions.lua
+++ b/lua/cphelper/definitions.lua
@@ -5,11 +5,11 @@ return {
rust = "rustc solution.rs -o rust.out",
},
run_cmd = {
- c = vim.fn.has('win32') == 1 and ".\\c.out" or "./c.out",
+ c = vim.fn.has("win32") == 1 and ".\\c.out" or "./c.out",
cpp = vim.fn.has("win32") == 1 and ".\\cpp.out" or "./cpp.out",
lua = "lua solution.lua",
python = "python solution.py",
- rust = vim.fn.has('win32') == 1 and ".\\rust.out" or "./rust.out",
+ rust = vim.fn.has("win32") == 1 and ".\\rust.out" or "./rust.out",
},
extensions = {
c = "c",
diff --git a/lua/cphelper/helpers.lua b/lua/cphelper/helpers.lua
index b7b5060..4f96a95 100644
--- a/lua/cphelper/helpers.lua
+++ b/lua/cphelper/helpers.lua
@@ -6,11 +6,11 @@ local M = {}
function M.sanitize(s)
local unwanted = { "-", " ", "#", "%.", ":", "'", "+", "%%" }
for _, char in pairs(unwanted) do
- local pos = string.find(s, char)
- while pos do
- s = string.sub(s, 1, pos - 1) .. string.sub(s, pos + 1)
- pos = string.find(s, char)
- end
+ local pos = string.find(s, char)
+ while pos do
+ s = string.sub(s, 1, pos - 1) .. string.sub(s, pos + 1)
+ pos = string.find(s, char)
+ end
end
return s
end
@@ -21,21 +21,20 @@ end
--- @return boolean #True if both the tables are equal
function M.comparetables(t1, t2)
if #t1 ~= #t2 then
- return false
+ return false
end
- if type(t1[1]) == 'string' then
- if vim.fn.has('win32') == 1 then
- for k, _ in pairs(t1) do
- t1[k] = string.gsub(t1[k], "\r", "")
- t2[k] = string.gsub(t2[k], "\r", "")
- end
- end
+ if type(t1[1]) == "string" then
+ if vim.fn.has("win32") == 1 then
+ for k, _ in pairs(t1) do
+ t1[k] = string.gsub(t1[k], "\r", "")
+ t2[k] = string.gsub(t2[k], "\r", "")
+ end
+ end
end
for k, v in pairs(t1) do
- if t2[k] ~= t1[k] then
- vim.api.nvim_err_writeln(t1[k] .. "!=".. t2[k])
- return false
- end
+ if t2[k] ~= t1[k] then
+ return false
+ end
end
return true
end
@@ -44,24 +43,24 @@ end
-- Credits: Christian Clason and Hirokazu Hata
local function pad(contents, opts)
vim.validate({
- contents = { contents, "t" },
- opts = { opts, "t", true },
+ contents = { contents, "t" },
+ opts = { opts, "t", true },
})
opts = opts or {}
local left_padding = (" "):rep(opts.pad_left or 1)
local right_padding = (" "):rep(opts.pad_right or 1)
for i, line in ipairs(contents) do
- contents[i] = string.format("%s%s%s", left_padding, line:gsub("\r", ""), right_padding)
+ contents[i] = string.format("%s%s%s", left_padding, line:gsub("\r", ""), right_padding)
end
if opts.pad_top then
- for _ = 1, opts.pad_top do
- table.insert(contents, 1, "")
- end
+ for _ = 1, opts.pad_top do
+ table.insert(contents, 1, "")
+ end
end
if opts.pad_bottom then
- for _ = 1, opts.pad_bottom do
- table.insert(contents, "")
- end
+ for _ = 1, opts.pad_bottom do
+ table.insert(contents, "")
+ end
end
return contents
end
@@ -74,13 +73,13 @@ function M.display_right(contents)
local width = math.floor(vim.o.columns * 0.5)
local height = math.floor(vim.o.lines * 0.9)
vim.api.nvim_open_win(bufnr, true, {
- border = vim.g.cphborder or "rounded",
- style = "minimal",
- relative = "editor",
- row = math.floor(((vim.o.lines - height) / 2) - 1),
- col = math.floor(vim.o.columns - width - 1),
- width = width,
- height = height,
+ border = vim.g.cphborder or "rounded",
+ style = "minimal",
+ relative = "editor",
+ row = math.floor(((vim.o.lines - height) / 2) - 1),
+ col = math.floor(vim.o.columns - width - 1),
+ width = width,
+ height = height,
})
contents = pad(contents, { pad_top = 1 })
vim.api.nvim_buf_set_lines(bufnr, 0, -1, true, contents)
--
2.33.0.windows.2
From 7c3342903b4d27cb51fc664b0404db90d56a2adb Mon Sep 17 00:00:00 2001
From: unknown <dalal.chinmay.0101@gmail.com>
Date: Sun, 19 Sep 2021 13:45:11 +0530
Subject: [PATCH cphelper.nvim 3/5] add comments to `run_tests.lua`
---
lua/cphelper/run_test.lua | 7 +++++++
1 file changed, 7 insertions(+)
diff --git a/lua/cphelper/run_test.lua b/lua/cphelper/run_test.lua
index ba50ca1..a645583 100644
--- a/lua/cphelper/run_test.lua
+++ b/lua/cphelper/run_test.lua
@@ -18,6 +18,8 @@ function M.run_test(case, cmd)
vim.list_extend(result, exp_out_arr)
local output_arr = {}
local err_arr = {}
+
+ -- Run executable
local job_id = vim.fn.jobstart(cmd, {
on_stdout = function(_, data, _)
vim.list_extend(output_arr, data)
@@ -49,12 +51,17 @@ function M.run_test(case, cmd)
end
end,
})
+
+ -- Send input
vim.fn.chansend(job_id, vim.list_extend(vim.fn.readfile(case), { "" }))
+
+ -- Wait till `timeout`
local len = vim.fn.jobwait({ job_id }, timeout)
if len[1] == -1 then
vim.list_extend(result, { string.format("Status: Timed out after %d ms", timeout) })
vim.fn.jobstop(job_id)
end
+
return result, status
end
return M
--
2.33.0.windows.2
From 75b6a1ead62dda9ade3815698852f2588ad3b78c Mon Sep 17 00:00:00 2001
From: unknown <dalal.chinmay.0101@gmail.com>
Date: Sun, 19 Sep 2021 13:58:48 +0530
Subject: [PATCH cphelper.nvim 4/5] strip CR in run_tests.lua instead of
helpers.comparetables()
---
lua/cphelper/helpers.lua | 8 --------
lua/cphelper/run_test.lua | 7 +++++++
2 files changed, 7 insertions(+), 8 deletions(-)
diff --git a/lua/cphelper/helpers.lua b/lua/cphelper/helpers.lua
index 4f96a95..b031ca6 100644
--- a/lua/cphelper/helpers.lua
+++ b/lua/cphelper/helpers.lua
@@ -23,14 +23,6 @@ function M.comparetables(t1, t2)
if #t1 ~= #t2 then
return false
end
- if type(t1[1]) == "string" then
- if vim.fn.has("win32") == 1 then
- for k, _ in pairs(t1) do
- t1[k] = string.gsub(t1[k], "\r", "")
- t2[k] = string.gsub(t2[k], "\r", "")
- end
- end
- end
for k, v in pairs(t1) do
if t2[k] ~= t1[k] then
return false
diff --git a/lua/cphelper/run_test.lua b/lua/cphelper/run_test.lua
index a645583..5cb1967 100644
--- a/lua/cphelper/run_test.lua
+++ b/lua/cphelper/run_test.lua
@@ -29,6 +29,13 @@ function M.run_test(case, cmd)
vim.list_extend(err_arr, data)
end,
on_exit = function(_, exit_code, _)
+ -- Strip CR on Windows
+ if vim.fn.has("win32") then
+ for k, v in pairs(output_arr) do
+ output_arr[k] = string.gsub(v, "\r", "")
+ end
+ end
+
if #output_arr ~= 0 then
vim.list_extend(result, { "Received output:" })
vim.list_extend(result, output_arr)
--
2.33.0.windows.2
From 5a51e773afe80bd8ac5000a482872f2bdc7fa3bb Mon Sep 17 00:00:00 2001
From: unknown <dalal.chinmay.0101@gmail.com>
Date: Mon, 20 Sep 2021 08:47:53 +0530
Subject: [PATCH cphelper.nvim 5/5] Space out receive()
---
lua/cphelper/receive.lua | 3 +++
1 file changed, 3 insertions(+)
diff --git a/lua/cphelper/receive.lua b/lua/cphelper/receive.lua
index a0b6b9d..bbac930 100644
--- a/lua/cphelper/receive.lua
+++ b/lua/cphelper/receive.lua
@@ -26,14 +26,17 @@ function M.receive()
else
client:shutdown()
client:close()
+
local lines = {}
for line in string.gmatch(buffer, "[^\r\n]+") do
table.insert(lines, line)
end
buffer = lines[#lines]
+
vim.schedule(function()
process(buffer)
end)
+
M.server:shutdown()
end
end)
--
2.33.0.windows.2