Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions docs/guides/host-core.md
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ provides the headless pair picker. It returns:
`presentation` defaults.
- Actions: `setBefore`, `setAfter`, `swap`, and `retry`.

**Revision labels.** An `ExtractionChoice`'s `label` names its revision in one
line, `main · "Add auth" · 2 days ago`: the name (an identifier, which pane
chips set in the monospace face), a detail (the commit's subject in quotes, or
the short commit beside a named ref) and its age. `formatRevisionLabel(parts)`
writes one, `parseRevisionLabel(label)` reads it back
(`{name, detail?, subject?, when?}`; any text is all `name`),
`revisionLabelName` is the name a compact picker shows, and `revisionAge(date)`
is the age (`5 minutes ago`).

`useComparisonSession(options)` keeps what the person chooses while they move
between pairs: the mode, the swipe divider, highlighting, the scope, find, the
connector and loop. A pair picker's view remounts for each new comparison, so
Expand Down
2 changes: 2 additions & 0 deletions packages/host/core/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ export type {
ViewKind,
} from './view-model.js'
export {useViewChoice} from './view-choice.js'
export {formatRevisionLabel, parseRevisionLabel, revisionAge, revisionLabelName} from './revision-label.js'
export type {RevisionLabelParts} from './revision-label.js'
export {handlePlaybackShortcut, isPlaybackShortcut, rovingIndex, spaceKeeping} from './keyboard.js'
export type {KeyEventLike} from './keyboard.js'
export type {ViewChoice} from './view-choice.js'
Expand Down
2 changes: 1 addition & 1 deletion packages/host/core/src/pair.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import type {PresentationDefaults} from './projection.js'
export interface ExtractionChoice {
/** Stable key, for example a commit SHA or `working-tree`. */
key: string
/** Label shown in the pair picker, for example `main · 1a2b3c4d`. */
/** Label shown in the pair picker, for example `main · "Add auth" · 2 days ago` (`formatRevisionLabel`); pickers and pane chips read its parts back (`parseRevisionLabel`). */
label: string
document: ExtractionDocument
}
Expand Down
53 changes: 53 additions & 0 deletions packages/host/core/src/revision-label.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
// How hosts name a revision in one line: `main · "Add auth" · 2 days ago`. Pair pickers, pane labels and chips read
// these parts back, so the format and its parsing live here, once.

/** A revision label's parts: its name (an identifier: a branch, tag, commit or `HEAD~1`), its detail, and its age. */
export interface RevisionLabelParts {
name: string
/** The commit's subject in quotes (`"Add auth"`), or the short commit beside a named ref. */
detail?: string | undefined
/** How long ago it was (`revisionAge`): `2 days ago`, `just now`. */
when?: string | undefined
}

const separator = ' · '
const agePattern = /\bago$|^just now$/

/** A revision label from its parts: `main · "Add auth" · 2 days ago`, leaving out the parts it lacks. */
export const formatRevisionLabel = ({name, detail, when}: RevisionLabelParts): string =>
[name, detail, when].filter(Boolean).join(separator)

/**
* A revision label's parts (`formatRevisionLabel` reversed). Any text works: without separators it is all `name`; the
* last part is `when` only if it reads as an age.
*/
export function parseRevisionLabel(label: string): RevisionLabelParts & {subject?: string | undefined} {
const parts = label.split(separator)
const name = parts.shift()?.trim() || label
const when = parts.length && agePattern.test(parts.at(-1)!) ? parts.pop() : undefined
const detail = parts.length ? parts.join(separator) : undefined
const subject = detail && /^".*"$/.test(detail) ? detail.slice(1, -1) : undefined
return {name, ...(detail ? {detail} : {}), ...(subject ? {subject} : {}), ...(when ? {when} : {})}
}

/** A revision label's name alone (`main`), for compact pickers and chips. */
export const revisionLabelName = (label: string): string => parseRevisionLabel(label).name

/** How long ago an ISO date was, coarsely: `just now`, `5 minutes ago`, `2 days ago`; undefined for no date. */
export function revisionAge(date: string | undefined, now = Date.now()): string | undefined {
const time = date ? Date.parse(date) : NaN
if (!Number.isFinite(time)) return undefined
const seconds = Math.max(0, Math.round((now - time) / 1000))
const units: [string, number][] = [
['year', 31_536_000],
['month', 2_592_000],
['day', 86_400],
['hour', 3600],
['minute', 60],
]
for (const [unit, size] of units) {
const count = Math.floor(seconds / size)
if (count >= 1) return `${count} ${unit}${count === 1 ? '' : 's'} ago`
}
return 'just now'
}
18 changes: 2 additions & 16 deletions packages/host/core/src/stage/chip-text.tsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,5 @@
import type {CSSProperties, ReactNode} from 'react'

/** A chip label's parts: the revision's name (an identifier), its commit subject, and its age. */
export interface ChipParts {
name: string
detail?: string | undefined
when?: string | undefined
}

/** Splits a revision label (`name · detail · when`, see the Inspector's `revisions.ts`) into its parts. */
export function chipParts(label: string): ChipParts {
const parts = label.split(' · ')
const name = parts.shift() ?? ''
const when = parts.length && /\bago$|^just now$/.test(parts[parts.length - 1]!) ? parts.pop() : undefined
return {name, detail: parts.length ? parts.join(' · ') : undefined, when}
}
import {parseRevisionLabel} from '../revision-label.js'

const mono = 'var(--projections-font-family-mono, ui-monospace, SFMono-Regular, Menlo, monospace)'
const hash = /^[0-9a-f]{7,}$/
Expand Down Expand Up @@ -49,7 +35,7 @@ const trimmed = (style: CSSProperties, grow = false): CSSProperties =>
export function ChipText({label, words, hidden}: {label: ReactNode; words: boolean; hidden: CSSProperties}): ReactNode {
if (typeof label !== 'string') return label
if (words) return <span style={trimmed({color: nameColour})}>{label}</span>
const {name, detail, when} = chipParts(label)
const {name, detail, when} = parseRevisionLabel(label)
const subject = detail?.replace(/^"(.*)"$/, '$1')
return (
<>
Expand Down
4 changes: 3 additions & 1 deletion packages/host/core/src/stage/labels.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type {Side} from '../modes.js'
import {paneGeometry} from './arrangement.js'
import {ChipText} from './chip-text.js'
import {useWholePixels} from './whole-pixels.js'
import {parseRevisionLabel} from '../revision-label.js'

const srOnly: CSSProperties = {
position: 'absolute',
Expand All @@ -25,7 +26,8 @@ const genericLabels = new Set(['Before', 'After', 'A', 'B'])
*/
function RevisionText({label}: {label: ReactNode}) {
if (typeof label !== 'string' || genericLabels.has(label.trim())) return <>{label}</>
const [name = '', ...rest] = label.split(' · ')
const {name, detail, when} = parseRevisionLabel(label)
const rest = [detail, when].filter(Boolean)
return (
<>
<span style={{fontFamily: 'var(--projections-font-family-mono, ui-monospace, SFMono-Regular, Menlo, monospace)'}}>{name}</span>
Expand Down
12 changes: 6 additions & 6 deletions packages/host/core/src/ui/pickers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import {Label} from './components/label.js'
import {NativeSelect} from './components/native-select.js'
import {cn} from './lib/utils.js'
import {ToolbarPopover} from './toolbar-popover.js'
import {revisionLabelName} from '../revision-label.js'

// Choosing another pair remounts the picker (the view is recomputed), so the control the person used gets focus back.
let pendingFocus: {slot: 'before' | 'after' | 'swap'; until: number} | undefined
Expand All @@ -16,7 +17,6 @@ const remember = (slot: 'before' | 'after' | 'swap') => {
}

/** The short part of a revision label: "v1 · base" → "v1". */
const shortLabel = (label: string) => label.split(' · ')[0] ?? label

/**
* A revision select. `short` draws a compact trigger: the short revision name (truncated, with the full one as its
Expand Down Expand Up @@ -98,7 +98,7 @@ export function PairPicker({
</Label>
<RevisionSelect
short={short}
label={pair.before ? shortLabel(pair.before.label) : 'None'}
label={pair.before ? revisionLabelName(pair.before.label) : 'None'}
ref={before}
id={`${id}-before`}
className={select}
Expand Down Expand Up @@ -138,7 +138,7 @@ export function PairPicker({
</Label>
<RevisionSelect
short={short}
label={shortLabel(pair.after.label)}
label={revisionLabelName(pair.after.label)}
ref={after}
id={`${id}-after`}
className={select}
Expand Down Expand Up @@ -177,12 +177,12 @@ export function PairPicker({
<span className="pj:min-w-0 pj:truncate">
{comparing ? (
<>
{shortLabel(pair.before!.label)}
{revisionLabelName(pair.before!.label)}
<span className="pj:px-1 pj:text-muted-foreground">→</span>
{shortLabel(pair.after.label)}
{revisionLabelName(pair.after.label)}
</>
) : (
shortLabel(pair.after.label)
revisionLabelName(pair.after.label)
)}
</span>
<ChevronDown className="pj:size-3.5! pj:shrink-0 pj:text-muted-foreground" />
Expand Down
21 changes: 21 additions & 0 deletions packages/host/core/test/revision-label.test.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import {test} from 'node:test'
import assert from 'node:assert/strict'
import {formatRevisionLabel, parseRevisionLabel, revisionAge, revisionLabelName} from '../dist/index.js'

test('a revision label is its name, detail and age, and reads back into them', () => {
const label = formatRevisionLabel({name: 'main', detail: '"Add auth · login"', when: '2 days ago'})
assert.equal(label, 'main · "Add auth · login" · 2 days ago')
assert.deepEqual(parseRevisionLabel(label), {name: 'main', detail: '"Add auth · login"', subject: 'Add auth · login', when: '2 days ago'})
assert.deepEqual(parseRevisionLabel('feature/x · 1a2b3c4d'), {name: 'feature/x', detail: '1a2b3c4d'})
assert.deepEqual(parseRevisionLabel('Working tree'), {name: 'Working tree'})
assert.equal(revisionLabelName('v3 · latest push'), 'v3')
})

test('a revision’s age is coarse, and absent without a date', () => {
const now = Date.parse('2026-11-20T12:00:00Z')
assert.equal(revisionAge('2026-11-20T11:59:30Z', now), 'just now')
assert.equal(revisionAge('2026-11-20T11:55:00Z', now), '5 minutes ago')
assert.equal(revisionAge('2026-11-18T12:00:00Z', now), '2 days ago')
assert.equal(revisionAge(undefined, now), undefined)
assert.equal(revisionAge('not a date', now), undefined)
})
23 changes: 4 additions & 19 deletions packages/host/inspector/src/app/revisions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import type {Diagnostic, ExtractionDocument} from '@githubnext/projections-schemas'
import {formatRevisionLabel, revisionAge} from '@githubnext/projections-host'
import type {ExtractionChoice} from '@githubnext/projections-host'
import type {InspectorResultIdentity} from '../control.js'
import type {InspectorRevision} from '../types.js'
Expand All @@ -17,24 +18,8 @@ const shaPattern = /^[0-9a-f]{40}$|^[0-9a-f]{64}$/i
*/
const namesCommit = (ref: string, commit: string) =>
shaPattern.test(ref) || ref === commit || (/^[0-9a-f]{7,39}$/i.test(ref) && commit.toLowerCase().startsWith(ref.toLowerCase()))
/** How long ago an ISO date was, coarsely: `just now`, `5 minutes ago`, `2 days ago`. */
export function age(date: string | undefined, now = Date.now()): string | undefined {
const time = date ? Date.parse(date) : NaN
if (!Number.isFinite(time)) return undefined
const seconds = Math.max(0, Math.round((now - time) / 1000))
const units: [string, number][] = [
['year', 31_536_000],
['month', 2_592_000],
['day', 86_400],
['hour', 3600],
['minute', 60],
]
for (const [unit, size] of units) {
const count = Math.floor(seconds / size)
if (count >= 1) return `${count} ${unit}${count === 1 ? '' : 's'} ago`
}
return 'just now'
}
/** How long ago an ISO date was (`revisionAge`): `just now`, `5 minutes ago`, `2 days ago`. */
export const age = revisionAge
/**
* A revision named for people, never a bare commit: its ref (or `Working tree`, or the short commit when the ref is
* one), the commit's subject and its age when the runner listed it, for example `main · "Add auth" · 2 days ago`.
Expand All @@ -54,7 +39,7 @@ export function choiceLabel(
const when = age(known?.date, now)
// Without the commit's subject, a named ref keeps its commit beside it.
const detail = known?.title ? `"${known.title}"` : name === commit ? undefined : commit
return [name, detail, when].filter(Boolean).join(' · ')
return formatRevisionLabel({name, detail, when})
}
const suppliedPrefix = 'Supplied as extractor output in '
/**
Expand Down
4 changes: 3 additions & 1 deletion packages/host/vscode-ui/src/pickers.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,14 @@ import {Overlay} from './overlay/overlay.js'
import {QuickPick} from './overlay/quick-pick.js'
import {uiCodicons} from './codicons.js'
import type {ToolbarLayout} from './layout/steps.js'
import {revisionLabelName} from '@githubnext/projections-host'

/** A pair picker's form, named as the default UI names it: two dropdowns, two with short names, or one compare action. */
export type PairForm = 'full' | 'short' | 'merged'

/** A revision's short name: its label up to the first " · ", for example "v1" for "v1 · base". */
export const shortRevisionLabel = (label: string) => label.split(' · ')[0]!.trim() || label
/** A revision label's name alone (`revisionLabelName`), for compact pickers. */
export const shortRevisionLabel = revisionLabelName

/** One revision dropdown: VS Code's dropdown (`vscode-single-select`), with the full name in each option's description. */
function RevisionSelect({
Expand Down
Loading