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

feat: allow passing of format_fn to set what is logged

Open dlvhdr opened this issue 3 years ago • 3 comments

Signed-off-by: Dolev Hadar [email protected]

dlvhdr avatar Oct 29 '22 21:10 dlvhdr

@PatschD, I created a draft PR to allow for passing of a format_fn. The format_fn by default is what you have right now, which is passed as the args of the print_function:

    format_fn = function(t)
      return '"'
          .. "file: "
          .. t.filename
          .. "~"
          .. "line: "
          .. t.line_nr
          .. t.breadcrumbs
          .. "~"
          .. t.current_text
          .. '", '
          .. t.current_text
    end,

But, using a custom format_fn I can get nice log messages:

  use({
    "/Users/dolevh/code/personal/github.com/PatschD/zippy.nvim",
    config = function()
      require("zippy").setup({
        ["typescriptreact"] = {
          format_fn = function(t)
            local filename = t.filename
            local line_nr = t.line_nr
            local breadcrumbs = t.breadcrumbs
            local current_text = t.current_text

            local t = '"%c[🐛 DEBUG]%c[📂 %s #%s]%c %s", "color: #9ece6a", "color: #a9b1d6", '
              .. '"'
              .. filename
              .. '", '
              .. '"'
              .. line_nr
              .. '", "color: white", '
              .. '"'
              .. current_text
              .. '", '
              .. current_text
            print(t)

            return t
          end,
        },
      })
    end,
  })

Which results in: image

The code is just a POC, so just wanted to pass it by you to see if this direction is good. One can argue the right solution is to replace the print_function entirely and have it receive the args. Let me know what you think!

P.S my lua formatter kinda muddied the PR.

dlvhdr avatar Oct 29 '22 21:10 dlvhdr

hey, this looks really interesting, and in general, I am very open to merging your suggestions.

I think what would still need to be addressed:

  • at the moment, the print_function option is just the language-specific print function, like console.log() for js, and print() for python. You changed it to console.log() for python, but that doesn't work.

  • The current format_fn is language specific now. So we need a global default function as a fallback if no custom format function is provided. I assume that could be something like:

M.default_format_fn = function (t)
...
end

-- in M.insert_print
local format_fn = M.language_keys["options"]["format_fn"] or M.default_format_fn

ccbiozhaw avatar Oct 30 '22 08:10 ccbiozhaw

Yeah sure, I'll make the changes!

dlvhdr avatar Oct 30 '22 21:10 dlvhdr