all repos — dotfiles @ 9dd42020ffc168010885c72a34221accd361d4d0

linux dotfiles

config/nvim-old/lua/jq.lua (view raw)

 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
local kmap = vim.keymap.set
local ucmd = vim.api.nvim_create_user_command

function buf_text(bufnr)
  if bufnr == nil then
    bufnr = vim.api.nvim_win_get_buf(0)
  end
  local lines = vim.api.nvim_buf_get_lines(bufnr, 0, vim.api.nvim_buf_line_count(bufnr), true)
  local text = ''
  for i, line in ipairs(lines) do
    text = text .. line .. '\n'
  end
  return text
end

function set_buf_text(text, bufnr)
  if bufnr == nil then
    bufnr = vim.fn.bufnr('%')
  end

  if type(text) == 'string' then
    text = vim.fn.split(text, '\n')
  end

  vim.api.nvim_buf_set_lines(
    bufnr,
    0,
    -1,
    false,
    text
  )
end

function jq_filter(json_bufnr, jq_filter)
  -- spawn jq and pipe in json, returning the output text
  local modified = vim.bo[json_bufnr].modified
  local fname = vim.fn.bufname(json_bufnr)

  if (not modified) and fname ~= '' then
    -- the following should be faster as it lets jq read the file contents
    return vim.fn.system({ 'jq', jq_filter, fname })
  else
    local json = buf_text(json_bufnr)
    return vim.fn.system({ 'jq', jq_filter }, json)
  end
end

function Jq_command(horizontal)
  local splitcmd = 'vnew'
  if horizontal == true then
    splitcmd = 'new'
  end

  local json_bufnr = vim.fn.bufnr()

  vim.cmd(splitcmd)
  vim.cmd'set filetype=conf'
  set_buf_text('# JQ filter: press <CR> to execute it\n\n.')
  vim.cmd'normal!G'
  local jq_bufnr = vim.fn.bufnr()
  local jq_winnr = vim.fn.bufwinid(jq_bufnr)

  vim.cmd(splitcmd)
  vim.cmd'set filetype=json'
  local result_bufnr = vim.fn.bufnr()


  vim.fn.win_gotoid(jq_winnr)

  -- setup keybinding autocmd in the filter buffer:
  kmap(
    'n',
    '<CR>',
    function()
      local filter = buf_text(jq_bufnr)
      set_buf_text(jq_filter(json_bufnr, filter), result_bufnr)
    end,
    { buffer = jq_bufnr }
  )
end

ucmd('Jq', function()
  Jq_command(false)
end, {})

ucmd('Jqhorizontal', function()
  Jq_command(true)
end, {})