numToStr / FTerm.nvim

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

Dynamic `cmd` #50

Closed fedoranvar closed 2 years ago

fedoranvar commented 2 years ago

Good day to you!

For opening csv files in certain program i have this code:

local fterm = require("FTerm")

function _G.fterm.visidata()
    local visidata = fterm:new({
        ft = "fterm_visidata", -- You can also override the default filetype, if you want
        cmd = "visidata " .. vim.fn.expand("%"),
        dimensions = {
            x = 1,
            y = 0,
            height = 0.9,
            width = 0.9,
        },
    })
    -- if vim.bo.filetype == "csv" then
    visidata:toggle()
    -- end
end

right now it creates new fterm-instance in function.

Question:

is it possible to define template without cmd attribute, but set it in dynamic function, so cmd could be dynamic because of expansion of %?

something like this (i've tried this, but get only shell float window):

local fterm = require("FTerm")
local visidata = fterm:new({
    ft = "fterm_visidata", -- You can also override the default filetype, if you want
    dimensions = {
        x = 1,
        y = 0,
        height = 0.9,
        width = 0.9,
    },
})

function _G.fterm.visidata()
    visidata.cmd = "visidata " .. vim.fn.expand("%") -- if vim.bo.filetype == "csv" then
    visidata:toggle()
    -- end
end

Thank you!

numToStr commented 2 years ago

You could use :run(<cmd>) method like this

local fterm = require("FTerm")
local visidata = fterm:new({
    ft = "fterm_visidata", -- You can also override the default filetype, if you want
    dimensions = {
        x = 1,
        y = 0,
        height = 0.9,
        width = 0.9,
    },
})

function _G.fterm.visidata()
    visidata:run({"visidata ", vim.fn.expand("%"))
end
fedoranvar commented 2 years ago

compared my currenti implementation (with cmd) against run:

cmd is faster, because it launches directly command, but run launches at first shell and then command in it, and also i've added ;exit after command, because quitting my command doesn't quit fterm shell by default

so, still, dynamic cmd would be prefferable if it can be. Thank you!

fedoranvar commented 2 years ago

'ive digged a little bit in code and found soluton:

local fterm = require("FTerm")
local visidata = fterm:new({
    ft = "fterm_visidata", -- You can also override the default filetype, if you want
    dimensions = {
        x = 1,
        y = 0,
        height = 0.9,
        width = 0.9,
    },
})

function _G.fterm.visidata()
    visidata.config.cmd = "visidata " .. vim.fn.expand("%")
    if vim.bo.filetype == "csv" then
        visidata:toggle()
    end
end