repositories / dotfiles

dotfiles

bugabingas dorkfiles

owned by bugabinga

feat(neovim): let a pivi conversation show who said what

74c19c7c5a9333f29310c41ca5f83b86e994e3a8

Oliver Krylow <o.krylow@isp-insoft.de> ·

SSH signature · not checked

Browse filesCompare with HEAD

Changed files (7)

neovim/lua/bugabinga/nugu/init.lua

Modified
209 209 _.BugabingaCodeActionSourceImports = _.DiagnosticHint
210 210 _.BugabingaCodeActionDefault = _.DiagnosticHint
212 +-- pivi conversation
213 +-- A user message is the one thing worth finding while scrolling, so it alone
214 +-- carries a background and a solid bar. Everything else stays quiet, and tool
215 +-- work is the quietest.
216 +_.PiviUser { bg = P.ui_backdrop }
217 +_.PiviUserBar { fg = P.content_accent, bold = true }
218 +_.PiviUserLabel { fg = P.content_accent, bold = true }
219 +_.PiviAssistantLabel { fg = P.content_focus }
220 +-- The output recedes furthest; the outcome beside the name stays scannable.
221 +_.PiviTool { fg = P.content_unfocus }
222 +_.PiviToolIcon { fg = P.content_focus }
223 +_.PiviToolOutcome { fg = P.content_minor }
224 +_.PiviError { fg = P.error }
225 +_.PiviErrorIcon { fg = P.error, bold = true }
212 227 -- bugabinga debug viewer
213 228 _.DbgViewerKind = _.Title
214 229 _.DbgViewerTime = _.Comment

neovim/lua/bugabinga/pivi/AGENTS.md

Modified
112 112 31. **A local in Lua is not visible before its declaration.** Two functions have now been used above their definition and resolved to `nil` at runtime; if a helper is called from an earlier function, define it earlier.
114 -32. **Contract tests guard assumptions,** not features. They skip unless `pi` is on `PATH` and opt-in is set. When Pi changes behavior, they fail before users do.
114 +32. **Roles are named, not coloured.** `bugabinga.pivi.highlight` defines `PiviUser`, `PiviUserBar`, `PiviUserLabel`, `PiviAssistantLabel`, `PiviTool`, `PiviToolIcon`, `PiviToolOutcome`, `PiviError` and `PiviErrorIcon` as defaults and re-applies them on `ColorScheme`; nugu defines them for real.
115 + The intent any theme must keep: only a user message carries a background, because it is the one thing a reader scrolls back to find. An answer takes no colour at all. Tool output recedes furthest, and its outcome stays scannable.
116 + Never reach for `Comment` to mean dim: in nugu it is bold, italic and important, which is the opposite.
118 +33. **Contract tests guard assumptions,** not features. They skip unless `pi` is on `PATH` and opt-in is set. When Pi changes behavior, they fail before users do.
116 120 ## Commands

neovim/lua/bugabinga/pivi/highlight.lua

Added
new file mode 100644
1 +--- What the parts of a conversation look like.
2 +--- pivi names the roles and leaves the colours to the colour scheme: every
3 +--- group below is a default, so a theme that defines one wins without pivi
4 +--- knowing anything about it.
5 +---
6 +--- The intent, which any theme should keep: a user message is the one thing
7 +--- that has to be findable while scrolling, so it carries a background and a
8 +--- solid bar. Everything else stays quiet, and tool work is quietest of all.
10 +local GROUPS = {
11 + PiviUser = { from = 'CursorLine', take = 'bg', },
12 + PiviUserBar = { from = 'Special', take = 'fg', bold = true, },
13 + PiviUserLabel = { from = 'Special', take = 'fg', bold = true, },
14 + PiviAssistantLabel = { from = 'Function', take = 'fg', },
15 + PiviTool = { from = 'NonText', take = 'fg', },
16 + PiviToolIcon = { from = 'Function', take = 'fg', },
17 + PiviToolOutcome = { from = 'NonText', take = 'fg', },
18 + PiviError = { from = 'ErrorMsg', take = 'fg', },
19 + PiviErrorIcon = { from = 'ErrorMsg', take = 'fg', bold = true, },
22 +local resolved = function( name )
23 + local ok, value = pcall( vim.api.nvim_get_hl, 0, { name = name, link = false, } )
24 + if not ok then return {} end
25 + return value or {}
26 +end
28 +--- Defines every pivi group that the colour scheme has not.
29 +local apply = function()
30 + for name, recipe in pairs( GROUPS ) do
31 + local source = resolved( recipe.from )
32 + local colour = source[recipe.take]
34 + local options = { default = true, bold = recipe.bold, }
35 + if colour then options[recipe.take] = colour end
37 + pcall( vim.api.nvim_set_hl, 0, name, options )
38 + end
39 +end
41 +local names = function()
42 + local found = {}
43 + for name in pairs( GROUPS ) do table.insert( found, name ) end
44 + table.sort( found )
45 + return found
46 +end
48 +apply()
50 +vim.api.nvim_create_autocmd( 'ColorScheme', {
51 + group = vim.api.nvim_create_augroup( 'pivi_highlight', { clear = true, } ),
52 + desc = 'Keep pivi colours in step with the colour scheme.',
53 + callback = apply,
54 +} )
56 +return {
57 + groups = GROUPS,
58 + names = names,
59 + apply = apply,

neovim/lua/bugabinga/pivi/render.lua

Modified
2 2 local entry = require 'bugabinga.pivi.entry'
3 3 local transcript = require 'bugabinga.pivi.transcript'
4 4 local tool = require 'bugabinga.pivi.tool'
5 5 local glyph = require 'std.icon'
6 +require 'bugabinga.pivi.highlight'
7 8 local NAMESPACE = 'pivi-render'
9 10 --- How each entry kind is framed.
10 11 --- Framing is decoration, never buffer text, so the transcript keeps the
11 12 --- message exactly as it was authored and copying it stays faithful.
13 +--- A user message is the thing a reader scrolls back to find, so it is the
14 +--- only one carrying a background and a solid bar. An answer is the ordinary
15 +--- text of the buffer and takes no colour at all. Tool work recedes.
12 16 local FRAMES = {
13 17 prompt = {
14 18 label = glyph.user .. ' you',
15 - label_hl = 'Title',
16 - prefix = '\u{258c} ',
17 - prefix_hl = 'Identifier',
19 + label_hl = 'PiviUserLabel',
20 + prefix = '\u{2588} ',
21 + prefix_hl = 'PiviUserBar',
22 + line_hl = 'PiviUser',
18 23 every_line = true,
20 - answer = { label = glyph.star .. ' pi', label_hl = 'Title', },
21 - tool = { prefix = '\u{25b8} ', prefix_hl = 'Function', line_hl = 'Comment', },
22 - error = { prefix = glyph.error .. ' ', prefix_hl = 'ErrorMsg', },
25 + answer = { label = glyph.star .. ' pi', label_hl = 'PiviAssistantLabel', },
26 + tool = { prefix = '\u{25b8} ', prefix_hl = 'PiviToolIcon', line_hl = 'PiviTool', },
27 + error = { prefix = glyph.error .. ' ', prefix_hl = 'PiviErrorIcon', line_hl = 'PiviError', },
25 30 local namespace = function()
95 100 local ended = tool.outcome( item.meta )
96 101 if ended then
97 102 pcall( vim.api.nvim_buf_set_extmark, bufnr, namespace(), item.first - 1, 0, {
98 - virt_text = { { ' ' .. ended, item.meta.failed and 'ErrorMsg' or 'Comment', }, },
103 + virt_text = { { ' ' .. ended, item.meta.failed and 'PiviError' or 'PiviToolOutcome', }, },
99 104 virt_text_pos = 'eol',
100 105 right_gravity = false,
101 106 priority = 60,

neovim/lua/bugabinga/pivi/skills/pivi/SKILL.md

Modified
42 42 - Markdown is rendered rather than shown as markup, and fenced code is presented in its own language.
43 43 - Every submission, answer, tool call and error is an entry with its own bounds and metadata.
44 44 - Tool output lands inside its tool entry and arrives collapsed; the conversation itself stays open.
45 +- Roles are visually distinct: a user message carries a background and a solid bar so it can be found while scrolling, an answer is ordinary text, and tool work recedes. The colours belong to the colour scheme through the `Pivi*` highlight groups.
45 46 - A tool entry states what it acted on and, once finished, how it ended: an edit shows how many lines it added and removed, a truncated result says so, a failure says so.
46 47 - A completed edit shows its change, with added and removed lines distinct, and `:Pi diff` or `gD` opens it as a real editor comparison.
47 48 - Going to an edit lands on its first changed line; going from a truncated result reaches the file its full output was spilled to.

neovim/tests/bugabinga/pivi/editor/highlight_spec.lua

Added
new file mode 100644
1 +---@diagnostic disable: duplicate-set-field, need-check-nil, param-type-mismatch, assign-type-mismatch, redundant-parameter
2 +local highlight = require 'bugabinga.pivi.highlight'
3 +local render = require 'bugabinga.pivi.render'
4 +local entry = require 'bugabinga.pivi.entry'
5 +local transcript = require 'bugabinga.pivi.transcript'
6 +local fake = dofile 'tests/helpers/pivi_fake.lua'
8 +local of = function( name )
9 + return vim.api.nvim_get_hl( 0, { name = name, link = false, } )
10 +end
12 +local block = function( bufnr, kind, meta, lines )
13 + local id = entry.open( bufnr, kind, meta )
14 + for _, text in ipairs( lines ) do entry.line( bufnr, id, text ) end
15 + entry.close( bufnr, id )
16 + render.decorate( bufnr, id )
17 + return id
18 +end
20 +--- Every line highlight applied inside an entry, keyed by line.
21 +local lines_of = function( bufnr )
22 + local found = {}
23 + for _, mark in ipairs( render.marks( bufnr ) ) do
24 + local group = ( mark[4] or {} ).line_hl_group
25 + if group then found[mark[2] + 1] = group end
26 + end
27 + return found
28 +end
30 +local texts_of = function( bufnr )
31 + local found = {}
32 + for _, mark in ipairs( render.marks( bufnr ) ) do
33 + local details = mark[4] or {}
34 + for _, chunk in ipairs( details.virt_text or {} ) do
35 + table.insert( found, chunk[2] )
36 + end
37 + for _, virtual in ipairs( details.virt_lines or {} ) do
38 + for _, chunk in ipairs( virtual ) do table.insert( found, chunk[2] ) end
39 + end
40 + end
41 + return found
42 +end
44 +describe( 'pivi highlight', function()
45 + local bufnr
47 + before_each( function()
48 + bufnr = transcript.ensure 'coloured'
49 + transcript.clear( bufnr )
50 + end )
52 + after_each( function()
53 + transcript.forget_all()
54 + fake.close_extra_windows()
55 + end )
57 + it( 'defines a group for every role', function()
58 + for _, name in ipairs( highlight.names() ) do
59 + assert.is_true( next( of( name ) ) ~= nil, name .. ' has no colour' )
60 + end
61 + end )
63 + it( 'leaves a colour scheme in charge', function()
64 + vim.api.nvim_set_hl( 0, 'PiviUser', { bg = '#123456', } )
66 + highlight.apply()
68 + assert.equal( tonumber( '123456', 16 ), of( 'PiviUser' ).bg )
69 + end )
71 + it( 'makes a user message the only one with a background', function()
72 + local prompt = block( bufnr, 'prompt', { text = 'find me', }, { 'find me', } )
73 + local answer = block( bufnr, 'answer', {}, { 'an answer', 'over two lines', } )
74 + local tool = block( bufnr, 'tool', { tool = 'read', }, { 'read a.lua', } )
76 + local applied = lines_of( bufnr )
77 + local first = select( 1, entry.range( bufnr, prompt ) )
78 + local answered = select( 1, entry.range( bufnr, answer ) )
79 + local worked = select( 1, entry.range( bufnr, tool ) )
81 + assert.equal( 'PiviUser', applied[first] )
82 + assert.is_nil( applied[answered] )
83 + assert.equal( 'PiviTool', applied[worked] )
85 + -- The background is what makes it findable, so it must actually be one.
86 + assert.is_truthy( of( 'PiviUser' ).bg )
87 + assert.is_nil( of( 'PiviTool' ).bg )
88 + end )
90 + it( 'carries the user background across every line of the message', function()
91 + local prompt = block( bufnr, 'prompt', { text = 'two lines', }, { 'first', 'second', } )
92 + local first, last = entry.range( bufnr, prompt )
93 + local applied = lines_of( bufnr )
95 + for row = first, last do
96 + assert.equal( 'PiviUser', applied[row], ( 'line %d is not marked' ):format( row ) )
97 + end
98 + end )
100 + it( 'gives each role its own labelling', function()
101 + block( bufnr, 'prompt', { text = 'a', }, { 'a', } )
102 + block( bufnr, 'answer', {}, { 'b', } )
103 + block( bufnr, 'tool', { tool = 'read', }, { 'read a.lua', } )
105 + local used = {}
106 + for _, group in ipairs( texts_of( bufnr ) ) do used[group] = true end
108 + assert.is_true( used.PiviUserLabel )
109 + assert.is_true( used.PiviUserBar )
110 + assert.is_true( used.PiviAssistantLabel )
111 + assert.is_true( used.PiviToolIcon )
112 + end )
114 + it( 'keeps tool work quieter than an answer', function()
115 + -- An answer is ordinary buffer text; tool output is deliberately dimmed.
116 + local answer = block( bufnr, 'answer', {}, { 'an answer', } )
117 + local applied = lines_of( bufnr )
119 + assert.is_nil( applied[select( 1, entry.range( bufnr, answer ) )] )
120 + assert.is_truthy( of( 'PiviTool' ).fg )
121 + end )
123 + it( 'marks a failure without borrowing the tool colour', function()
124 + local failed = block( bufnr, 'error', { error = 'boom', }, { 'boom', } )
125 + local applied = lines_of( bufnr )
127 + assert.equal( 'PiviError', applied[select( 1, entry.range( bufnr, failed ) )] )
128 + end )
129 +end )

neovim/tests/bugabinga/pivi/editor/render_spec.lua

Modified
111 111 local first = select( 1, entry.range( bufnr, id ) )
112 - assert.equal( 'Comment', groups[first] )
113 - assert.equal( 'Comment', groups[first + 1] )
112 + assert.equal( 'PiviTool', groups[first] )
113 + assert.equal( 'PiviTool', groups[first + 1] )
114 114 assert.equal( 'DiffDelete', groups[first + 2] )
115 115 assert.equal( 'DiffAdd', groups[first + 3] )