numToStr / FTerm.nvim

:fire: No-nonsense floating terminal plugin for neovim :fire:
MIT License
721 stars 24 forks source link

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

Open justrico opened 1 year ago

justrico commented 1 year ago

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.

muhhae commented 4 months ago

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.