FTerm.nvim icon indicating copy to clipboard operation
FTerm.nvim copied to clipboard

How to run statically-typed language by scratch terminal (Such as c/cpp/java)

Open justrico opened this issue 2 years ago • 1 comments

Hi, thx for this great project! I don't know how to set compile and execute commands in scratch terminal cmd config. Firstly, I've tried this by follow readme :

local runners = { c = 'gcc', cpp = 'g++' }

vim.keymap.set('n', '<leader><Enter>', function()
    local buf = vim.api.nvim_buf_get_name(0)
    local ftype = vim.filetype.match({ filename = buf })
    local fname = string.gsub(buf, ftype, '')
    local exec = runners[ftype]
    if exec ~= nil then
    	if exec == 'c' then
    		local cbin_fname = fname..'out'
        	require('FTerm').scratch({
        		cmd = { exec, buf, '-o', '&&', './'..cbin_fname }
        	})
        end
    end
end)

But this didn't work what I expect. The result is same as cmd = { exec, buf }, just compile to a default a.out file, and didn't executed.

image

Then I tried this to only execute a.out :

local runners = { c = 'gcc', cpp = 'g++' }

vim.keymap.set('n', '<leader><Enter>', function()
    local buf = vim.api.nvim_buf_get_name(0)
    local ftype = vim.filetype.match({ filename = buf })
    local exec = runners[ftype]
    if exec ~= nil then
    	if exec == 'c' then
        	require('FTerm').scratch({
        		cmd = { exec, buf, '&&', './a.out' }
        	})
        end
    end
end)

This had same result as I firstly tried. So I wonder to know how to set commands to auto compile and run statically-typed language.

justrico avatar Feb 02 '23 07:02 justrico

Hi, have you tried

require('FTerm').scratch {
    cmd = 'gcc -o tmp ' .. buf .. ' && ./tmp && rm ./tmp'
}

I do this for all language. And its work as expected.

muhhae avatar Mar 01 '24 12:03 muhhae