CodeSampleX

示例

github.com/chzyer/readline v1.5.1: Config

已验证示例 — golang github.com/chzyer/readline v1.5.1: Config. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: readline.Config.Init establishes default…

sha256:9759d6707465ad5c5b3b8959b93809f94a69abeef62bcac92be2c0a4ce17db37

本网络只提供一件事:能构建的样本。它在沙箱中运行并保留签名回执。它不评级、不担保——同样的代码能否在你的环境构建,它没有测量过。 提交了通过的契约回执的不同签名密钥数量。为 1 表示只有作者;大于 1 表示还有其他人构建过。密钥是自行生成的,背后没有注册身份,因此计的是密钥而非人。 MIT-0

执行证据

声明的环境与签名的运行分开呈现,你可以看到这个样本究竟运行了什么、在哪里运行。

证据依据
签名契约通过
验证回执
1
构建过它的签名密钥
1
声明的环境 linux 24 · ubuntu · glibc 2.39 x64 go

验证运行环境

环境 契约 阶段 运行日期
go 1.26 · linux debian/x64 · docker ed25519:c1973797be207ac4 PASS compile:SKIPPED · contract:PASS · load:PASS · resolve:PASS
CONTAINER_RUN · golang@1golang:1.26@sha256:e30143be198a…
2026-09-06

案例

HOW
目标
verify github.com/chzyer/readline.Config in pkg:golang/github.com/chzyer/readline@v1.5.1
包
符号
  • github.com/chzyer/readline.Config
创建时间
2026-09-06T01:39:30Z

契约

  1. readline.Config.Init establishes default parameters including HistoryLimit and interrupt prompts
  2. readline.Config.Clone yields an independent configuration instance preserving settings
  3. readline.Config.SetListener registers custom input event hook callback on configuration
  4. readline.Config.SetPainter attaches custom syntax and display painter to configuration
  5. readline.Config with FuncFilterInputRune intercepts and transforms input stream runes
  6. readline.Config with UniqueEditLine and VimMode configures editor mode and line persistence flags

文件

  • PROMPT.md
  • csx.json
  • go.mod
  • go.sum
  • main.go
  • spec.json
  • test/contract.go

下载源代码构件 (tar.gz)

源代码

PROMPT.md
Clean-room public code sample — generation instructions

Write a brand-new, minimal, self-contained code sample in this clean-room directory.
Do not copy, paraphrase, or reference any existing project source. Work only from this spec.

A csx.json manifest scaffold already exists. Do not recreate it from memory. Preserve its case.goal, packages and symbols; fill its empty case.contract with exact assertions and correct its environment, commands and verifierAdapter for the files you generate.

Goal: verify github.com/chzyer/readline.Config in pkg:golang/github.com/chzyer/readline@v1.5.1
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/github.com/chzyer/readline@v1.5.1
Demonstrate these symbols/APIs:
  - github.com/chzyer/readline.Config

Rules:
  - One focused purpose; the smallest project that proves the goal.
  - Include a contract test (test/contract.*) that runs OFFLINE and exits 0 exactly when the goal behavior works.
  - Pin every dependency with a lockfile so resolution is reproducible.
  - No secrets, credentials, or tokens. No real URLs (only example.com or localhost). No absolute paths.
  - No personal names, emails, company names, or project identifiers of any kind.
  - No binaries and no generated output (node_modules, dist, target, venv, .git, .env).
  - Keep it under 200 files and 256KB packed.
csx.json
{"case":{"caseId":"case:sha256:8147b984c3d370f75f75cefb91211372b6b35dcb735d68320e53e642d5c60701","contract":["readline.Config.Init establishes default parameters including HistoryLimit and interrupt prompts","readline.Config.Clone yields an independent configuration instance preserving settings","readline.Config.SetListener registers custom input event hook callback on configuration","readline.Config.SetPainter attaches custom syntax and display painter to configuration","readline.Config with FuncFilterInputRune intercepts and transforms input stream runes","readline.Config with UniqueEditLine and VimMode configures editor mode and line persistence flags"],"goal":"verify github.com/chzyer/readline.Config in pkg:golang/github.com/chzyer/readline@v1.5.1","kind":"HOW","packages":["pkg:golang/github.com/chzyer/readline@v1.5.1"],"schemaVersion":1,"symbols":["github.com/chzyer/readline.Config"]},"contractCommand":["go","run","./test"],"environment":{"arch":"x64","distro":"ubuntu","ecosystem":"golang","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","schemaVersion":1},"license":"MIT-0","packages":["pkg:golang/github.com/chzyer/readline@v1.5.1"],"schemaVersion":1,"subject":"pkg:golang/github.com/chzyer/readline@v1.5.1","symbols":["github.com/chzyer/readline.Config"],"verifierAdapter":"golang@1"}
go.mod
module sample

go 1.26.6

require github.com/chzyer/readline v1.5.1

require golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 // indirect
go.sum
github.com/chzyer/logex v1.2.1 h1:XHDu3E6q+gdHgsdTPH6ImJMIp436vR6MPtH8gP05QzM=
github.com/chzyer/logex v1.2.1/go.mod h1:JLbx6lG2kDbNRFnfkgvh4eRJRPX1QCoOIWomwysCBrQ=
github.com/chzyer/readline v1.5.1 h1:upd/6fQk4src78LMRzh5vItIt361/o4uq553V8B5sGI=
github.com/chzyer/readline v1.5.1/go.mod h1:Eh+b79XXUwfKfcPLepksvw2tcLE/Ct21YObkaSkeBlk=
github.com/chzyer/test v1.0.0 h1:p3BQDXSxOhOG0P9z6/hGnII4LGiEPOYBhs8asl/fC04=
github.com/chzyer/test v1.0.0/go.mod h1:2JlltgoNkt4TW/z9V/IzDdFaMTM2JPIi26O1pF38GC8=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5 h1:y/woIyUBFbpQGKS0u1aHF/40WUDnek3fPOyD08H5Vng=
golang.org/x/sys v0.0.0-20220310020820-b874c991c1a5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
main.go
package main

import (
	"bytes"
	"fmt"
	"io"
	"strings"

	"github.com/chzyer/readline"
)

// HighlightPainter implements readline.Painter to format line input.
type HighlightPainter struct{}

func (hp *HighlightPainter) Paint(line []rune, pos int) []rune {
	return line
}

func main() {
	// Configure readline instance with custom settings
	cfg := &readline.Config{
		Prompt:                 "demo> ",
		HistoryLimit:           100,
		DisableAutoSaveHistory: true,
		Stdin:                  io.NopCloser(strings.NewReader("hello readline\n")),
		Stdout:                 &bytes.Buffer{},
		FuncFilterInputRune: func(r rune) (rune, bool) {
			return r, true
		},
	}

	cfg.SetPainter(&HighlightPainter{})
	cfg.SetListener(func(line []rune, pos int, key rune) ([]rune, int, bool) {
		return line, pos, true
	})

	rl, err := readline.NewEx(cfg)
	if err != nil {
		fmt.Printf("Initialization failed: %v\n", err)
		return
	}
	defer rl.Close()

	line, err := rl.Readline()
	if err != nil {
		fmt.Printf("Readline error: %v\n", err)
		return
	}
	fmt.Printf("Read: %s\n", line)
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/chzyer/readline.Config in pkg:golang/github.com/chzyer/readline@v1.5.1",
  "kind": "HOW",
  "packages": [
    "pkg:golang/github.com/chzyer/readline@v1.5.1"
  ],
  "symbols": [
    "github.com/chzyer/readline.Config"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"fmt"
	"io"
	"os"
	"strings"

	"github.com/chzyer/readline"
)

type customPainter struct {
	paintCallCount int
}

func (cp *customPainter) Paint(line []rune, pos int) []rune {
	cp.paintCallCount++
	return []rune(strings.ToUpper(string(line)))
}

func main() {
	if err := runContract(); err != nil {
		fmt.Fprintf(os.Stderr, "Contract verification failed: %v\n", err)
		os.Exit(1)
	}
	fmt.Println("All readline.Config contract assertions passed successfully.")
}

func runContract() error {
	// Assertion 1: readline.Config.Init establishes default parameters including HistoryLimit and interrupt prompts
	cfg := &readline.Config{}
	if err := cfg.Init(); err != nil {
		return fmt.Errorf("cfg.Init() failed: %w", err)
	}
	if cfg.HistoryLimit != 500 {
		return fmt.Errorf("expected default HistoryLimit=500, got %d", cfg.HistoryLimit)
	}
	if cfg.InterruptPrompt != "^C" {
		return fmt.Errorf("expected default InterruptPrompt='^C', got %q", cfg.InterruptPrompt)
	}
	if cfg.EOFPrompt != "^D" {
		return fmt.Errorf("expected default EOFPrompt='^D', got %q", cfg.EOFPrompt)
	}
	if cfg.AutoComplete == nil {
		return fmt.Errorf("expected non-nil default AutoComplete")
	}

	// Assertion 2: readline.Config.Clone yields an independent configuration instance preserving settings
	baseCfg := &readline.Config{
		Prompt:       "prompt1> ",
		HistoryLimit: 250,
		VimMode:      true,
	}
	clonedCfg := baseCfg.Clone()
	if clonedCfg.Prompt != "prompt1> " || clonedCfg.HistoryLimit != 250 || !clonedCfg.VimMode {
		return fmt.Errorf("cloned config values do not match original")
	}
	baseCfg.Prompt = "prompt2> "
	if clonedCfg.Prompt != "prompt1> " {
		return fmt.Errorf("modifying base config affected cloned config prompt")
	}

	// Assertion 3: readline.Config.SetListener registers custom input event hook callback on configuration
	listenerCfg := &readline.Config{}
	var listenerInvoked bool
	listenerCfg.SetListener(func(line []rune, pos int, key rune) ([]rune, int, bool) {
		listenerInvoked = true
		return append(line, '+'), pos + 1, true
	})
	if listenerCfg.Listener == nil {
		return fmt.Errorf("expected listenerCfg.Listener to be non-nil after SetListener")
	}
	newLine, newPos, ok := listenerCfg.Listener.OnChange([]rune("cmd"), 3, 'x')
	if !ok || !listenerInvoked || string(newLine) != "cmd+" || newPos != 4 {
		return fmt.Errorf("listener execution failed or produced unexpected state")
	}

	// Assertion 4: readline.Config.SetPainter attaches custom syntax and display painter to configuration
	painterCfg := &readline.Config{}
	p := &customPainter{}
	painterCfg.SetPainter(p)
	if painterCfg.Painter != p {
		return fmt.Errorf("expected painterCfg.Painter to equal set painter")
	}
	painted := painterCfg.Painter.Paint([]rune("syntax"), 0)
	if string(painted) != "SYNTAX" || p.paintCallCount != 1 {
		return fmt.Errorf("custom painter output or call count mismatch")
	}

	// Assertion 5: readline.Config with FuncFilterInputRune intercepts and transforms input stream runes
	var filterStdout bytes.Buffer
	filterCfg := &readline.Config{
		Prompt: "> ",
		Stdin:  io.NopCloser(strings.NewReader("a#b#c\n")),
		Stdout: &filterStdout,
		Stderr: io.Discard,
		FuncFilterInputRune: func(r rune) (rune, bool) {
			if r == '#' {
				return 0, false // drop '#'
			}
			if r >= 'a' && r <= 'z' {
				return r - 32, true // convert to uppercase
			}
			return r, true
		},
	}
	filterRl, err := readline.NewEx(filterCfg)
	if err != nil {
		return fmt.Errorf("failed to create readline with FuncFilterInputRune: %w", err)
	}
	defer filterRl.Close()

	filteredLine, err := filterRl.Readline()
	if err != nil {
		return fmt.Errorf("Readline with FuncFilterInputRune failed: %w", err)
	}
	if filteredLine != "ABC" {
		return fmt.Errorf("expected 'ABC', got %q", filteredLine)
	}

	// Assertion 6: readline.Config with UniqueEditLine and VimMode configures editor mode and line persistence flags
	var flagStdout bytes.Buffer
	flagCfg := &readline.Config{
		Prompt:                 "flags> ",
		Stdin:                  io.NopCloser(strings.NewReader("command\n")),
		Stdout:                 &flagStdout,
		Stderr:                 io.Discard,
		UniqueEditLine:         true,
		VimMode:                true,
		DisableAutoSaveHistory: true,
		HistorySearchFold:      true,
	}
	if !flagCfg.UniqueEditLine || !flagCfg.VimMode || !flagCfg.DisableAutoSaveHistory || !flagCfg.HistorySearchFold {
		return fmt.Errorf("configuration flags not retained on Config struct")
	}
	flagRl, err := readline.NewEx(flagCfg)
	if err != nil {
		return fmt.Errorf("NewEx failed with custom editor and history flags: %w", err)
	}
	defer flagRl.Close()

	flagLine, err := flagRl.Readline()
	if err != nil {
		return fmt.Errorf("Readline failed with custom flags: %w", err)
	}
	if flagLine != "command" {
		return fmt.Errorf("expected 'command', got %q", flagLine)
	}

	return nil
}

原始种子者

匿名