Gabsha/dcm-nvim

A nvim plugin to browse dicom file metadata

★ 0Forks 0LuaGitHub ↗Compare
dcmdicomnvimnvim-plugin

README

dcm-nvim

DICOM metadata viewer for Neovim 0.12+. Displays .dcm file headers as an expandable table with cursor-row highlighting.

screenshot

Requirements

  • Neovim ≥ 0.12
  • Python 3 with pydicom (uv tool install pydicom)

Install

With the built-in plugin manager (Neovim 0.12+)

vim.pack.add({
    "Gabsha/dcm-nvim",
    config = function()
        require("dcm").setup({
            tag_width = 14,
            name_width = 32,
            auto_open = true,
        })
    end,
})

With lazy.nvim

{
    "Gabsha/dcm-nvim",
    ft = "dcm",
    config = function()
        require("dcm").setup({
            auto_open = true,
        })
    end,
}

Usage

Open any .dcm file — the raw/binary buffer is replaced in-place with the rendered metadata table (no binary content is ever shown). If you invoke :DcmOpen <file> for a file that isn't the current buffer, the viewer opens in a right-side split instead:

nvim scan.dcm

Commands

Command Description
:DcmOpen [file] Open viewer for a DICOM file
:DcmToggle Toggle viewer on/off
:DcmClose Close all DICOM viewers

Viewer Keymaps

Key Action
<CR> Expand / collapse a list-valued row
q Close viewer
j / k Navigate rows
<C-d> Page down
<C-u> Page up

Example table output

┌──────────────┬──────────────────────────────────┬────────────────────┐
│ Tag          │ Name (VR)                        │ Value              │
├──────────────┼──────────────────────────────────┼────────────────────┤
│ (0008,0008)  │ ImageType (CS)                   │ ["ORIGINAL", ...]  │
│ (0010,0010)  │ PatientName (PN)                 │ Doe^John           │
│ (0010,0030)  │ PatientBirthDate (DA)            │ 19700101           │
│ (0020,000E)  │ StudyInstanceUID (UI)            │ 1.2.3.4.5.6...     │
│ ...          │ ...                              │ ...                │
└──────────────┴──────────────────────────────────┴────────────────────┘

Rows with ▶ can be expanded with <CR>. Expanded rows show ▼ and list all contained values. This includes DICOM Sequences (VR SQ), which are nested datasets: expanding a sequence reveals its [n] Item rows, and expanding an Item reveals its own tags -- indented one level further. Since sequences can nest arbitrarily deep (an Item can itself contain another Sequence), this works recursively to any depth, and every row can be expanded/collapsed independently of its siblings and parents.

Configuration

require("dcm").setup({
    tag_width    = 14,      -- width of the (xxxx,xxxx) column
    name_width   = 32,      -- width of Name (VR) column
    auto_open    = true,    -- auto-open on FileType=dcm
    expand_key   = "<CR>",  -- key to expand/collapse
    close_key    = "q",     -- key to close viewer
})

Customise highlights

vim.api.nvim_set_hl(0, "DcmCurrentRow", { bg = "#3a3a5c", bold = true })
vim.api.nvim_set_hl(0, "DcmHeader",     { fg = "#c0caf5", bold = true })
vim.api.nvim_set_hl(0, "DcmExpandable", { underline = true, sp = "#7aa2f7" })
vim.api.nvim_set_hl(0, "DcmChild",      { fg = "#9aa5ce" })

Available groups: DcmHeader, DcmSeparator, DcmFooter, DcmRow, DcmExpandable, DcmExpanded, DcmChild, DcmCurrentRow.

How it works

  1. ftdetect/dcm.lua sets filetype=dcm for .dcm files
  2. An autocmd fires on FileType dcm and calls pydicom via vim.system()
  3. Pydicom parses the DICOM header and returns JSON
  4. The JSON is rendered into a scratch buffer as a scrollable table
  5. Rows with list values, or DICOM Sequences (nested datasets), are expandable via <CR> -- sequences recurse to arbitrary depth
  6. Cursor movement triggers row highlighting via extmarks

License

MIT

Contributors

Gabsha

Issues