codeview

package
v0.34.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: May 25, 2026 License: MIT Imports: 5 Imported by: 0

README

Code view

A syntax-tinted source-code block with optional line numbers and per-line marks.

code-view preview

Install

glyph add code-view

This copies code-view.go (and its test file) into your repo at the path your glyph.json aliases declare. After install, the file is yours: edit it, refactor it, rename it. There is no code-view library to keep in sync.

Hello, world

package main

import (
	"fmt"

	codeview "github.com/truffle-dev/glyph/components/code-view"
)

func main() {
	src := `func main() {
    fmt.Println("hello, glyph")
}`
	fmt.Println(codeview.Render(codeview.Block{
		Source:     src,
		Language:   codeview.LangGo,
		ShowGutter: true,
		Marks:      map[int]codeview.LineMark{2: codeview.MarkHighlight},
	}))
}

API surface

Package: codeview

Types

  • Block
  • Language
  • LineMark

Constants

  • LangPlain, LangGo, LangJS, LangTS, LangPy, LangRust, LangJSON, LangBash
  • MarkNone, MarkHighlight, MarkAdded, MarkRemoved, MarkWarning, MarkError

Functions

  • Render

Dependencies

  • glyph component theme (installed automatically)
  • github.com/charmbracelet/bubbletea@v1.3.10
  • github.com/charmbracelet/lipgloss@v1.1.0

Notes

Stateless rendering primitive. Build a codeview.Block and pass it to Render. The tokenizer is intentionally tiny — it tints keywords, double-quoted strings, integer/float literals, and line comments for Go, JavaScript/TypeScript, Python, Rust, JSON, and Bash. Production-quality highlighting belongs in a separate component that wraps a real lexer like chroma or tree-sitter.

Use Marks to call out specific lines: MarkHighlight paints a focus row, MarkAdded/MarkRemoved tint the gutter for diff hunks, MarkError underlines a failing line. Set MaxWidth > 0 to truncate long lines with an ellipsis so the block fits inside a fixed-width panel.

See also

License

MIT, same as the rest of glyph.

Documentation

Overview

Package codeview renders a single block of source code with optional line numbers, gutter highlights, and a small built-in tokenizer that tints keywords, strings, numbers, and comments.

The package is stateless. Callers build a Block, call Render, and place the result inside their own View(). Compose Block.Line with .Highlight, .Underline, or .Gutter to mark up specific rows (current cursor line, diff hunks, failing-test stack frame, etc.).

The tokenizer is intentionally tiny. It is a "good enough" lexer for quick reads: Go, JavaScript/TypeScript, Python, Rust, JSON, Bash, and any language that uses // or # line comments and double-quoted strings. Production-quality syntax highlighting belongs in a separate component that wraps a real lexer (chroma, tree-sitter) — codeview's bar is to make a peek-into-a-file surface readable inside an operator dashboard.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Render

func Render(b Block) string

Render returns the multi-line styled string. Empty source returns "".

func Tokenize

func Tokenize(line string, lang Language) string

Tokenize returns a single styled line for the given language without any gutter, mark, or row chrome. Intended for components that own their own row layout (an editable buffer with its own cursor) and only want to reuse the tokenizer. Empty line returns "".

Types

type Block

type Block struct {
	Source     string
	Language   Language
	ShowGutter bool // render line-number gutter
	StartLine  int  // 1-based line number for the first row; 0 means 1
	Marks      map[int]LineMark
	MaxWidth   int // 0 means no truncation; otherwise hard-wrap with ellipsis
}

Block is the input to Render. Mark a subset of lines via Marks; keys are 1-based line numbers matching the rendered gutter.

type Language

type Language string

Language selects the tokenizer dialect. Unknown languages fall through to LangPlain which only highlights numbers and string literals.

const (
	LangPlain Language = ""
	LangGo    Language = "go"
	LangJS    Language = "js"
	LangTS    Language = "ts"
	LangPy    Language = "py"
	LangRust  Language = "rust"
	LangJSON  Language = "json"
	LangBash  Language = "bash"
)

type LineMark

type LineMark int

LineMark tags one source line for special rendering.

const (
	MarkNone      LineMark = iota
	MarkHighlight          // current/focus line — surface-strong background
	MarkAdded              // diff: line added — success-tinted gutter
	MarkRemoved            // diff: line removed — error-tinted gutter
	MarkWarning            // call out a line without diff semantics
	MarkError              // call out a failing line
)

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL