Writing a function get the text equivalent of a <tab>

For a plugin I’m writing, I need to get the text equivalent of a <tab>. However, tabs in Neovim has configured by a bunch of different options. I want to get validation from someone else if my logic here is correct or if there is a better way to do this.

local function tab()
    if vim.bo.expandtab then
        return string.rep(" ", vim.fn.shiftwidth())
    else
        return "	" -- tab character
    end
end

Note: I’m alright with ignore :h 'vartabstop'

There is strdisplaywidth() which works even when there is 'vartabstop', and also allows you specify a column where the tab character starts.

strdisplaywidth() doesn’t really help with turning it into text though, it just tells me the visual representation.

Oh, I didn’t read your question carefully. This is indeed a bit hard, as what a <Tab> inserts also depends on 'smarttab' and 'softtabstop'. The code for inserting a <Tab> is over 200 lines long.

Ended up working about this to avoid calculating the text. The function felt too brittle.