CodeSampleX

示例

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

已验证示例 — golang github.com/chzyer/readline v1.5.1: NewEx. contract 在 go 1.26 · linux debian/x64 · docker 上运行并通过: readline.NewEx creates a valid Instance from…

sha256:2e8b97f27f0e940b10efa0c3e2729d0d9357aa6ac69f1f737418786e6fb57a0c

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

执行证据

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

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

验证运行环境

环境 契约 阶段 运行日期
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.NewEx in pkg:golang/github.com/chzyer/readline@v1.5.1
包
符号
  • github.com/chzyer/readline.NewEx
环境
go 1.26.6
创建时间
2026-09-06T02:27:53Z

契约

  1. readline.NewEx creates a valid Instance from a custom Config with specified Prompt and streams
  2. readline.NewEx reads sequential lines from configured custom Stdin
  3. readline.NewEx returns io.EOF on Readline when configured Stdin is exhausted
  4. readline.NewEx instance allows dynamic prompt updates via SetPrompt
  5. readline.NewEx accepts AutoComplete prefix completer in Config
  6. readline.NewEx instance generates masked password config via GenPasswordConfig
  7. readline.NewEx instance Close method cleanly releases terminal resources

文件

  • 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.NewEx 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.NewEx

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:6141b1523971cc4fcb4ae0e32f7205e17c1b945aae27e1ef4108dfd98b49aedd","contract":["readline.NewEx creates a valid Instance from a custom Config with specified Prompt and streams","readline.NewEx reads sequential lines from configured custom Stdin","readline.NewEx returns io.EOF on Readline when configured Stdin is exhausted","readline.NewEx instance allows dynamic prompt updates via SetPrompt","readline.NewEx accepts AutoComplete prefix completer in Config","readline.NewEx instance generates masked password config via GenPasswordConfig","readline.NewEx instance Close method cleanly releases terminal resources"],"goal":"verify github.com/chzyer/readline.NewEx 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.NewEx"]},"contractCommand":["go","run","./test"],"environment":{"arch":"amd64","compiler":"go","compilerVersion":"1.26.6","distro":"ubuntu","ecosystem":"golang","language":"go","languageVersion":"1.26.6","libc":"glibc","libcVersion":"2.39","os":"linux","osVersionBucket":"24","packageManager":"go","packageManagerVersion":"1.26.6","runtime":"go","runtimeVersion":"1.26.6","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.NewEx"],"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"
)

func main() {
	// Demonstrates configuring and using readline.NewEx with custom parameters
	completer := readline.NewPrefixCompleter(
		readline.PcItem("help"),
		readline.PcItem("version"),
		readline.PcItem("exit"),
	)

	// In an interactive shell os.Stdin and os.Stdout are typically used,
	// but NewEx allows supplying custom io.ReadCloser and io.Writer streams.
	input := strings.NewReader("help\nversion\nexit\n")
	output := &bytes.Buffer{}

	cfg := &readline.Config{
		Prompt:                 "demo> ",
		Stdin:                  io.NopCloser(input),
		Stdout:                 output,
		AutoComplete:           completer,
		HistoryLimit:           100,
		DisableAutoSaveHistory: true,
	}

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

	for {
		line, err := rl.Readline()
		if err != nil {
			if err == io.EOF {
				break
			}
			fmt.Printf("read error: %v\n", err)
			break
		}
		fmt.Printf("received line: %s\n", line)
	}
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify github.com/chzyer/readline.NewEx 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.NewEx"
  ]
}
test/contract.go
package main

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

	"github.com/chzyer/readline"
)

func main() {
	// Contract 1: readline.NewEx creates a valid Instance from a custom Config with specified Prompt and streams
	{
		in := io.NopCloser(strings.NewReader("ping\npong\n"))
		out := &bytes.Buffer{}
		cfg := &readline.Config{
			Prompt:                 "prompt> ",
			Stdin:                  in,
			Stdout:                 out,
			HistoryLimit:           50,
			DisableAutoSaveHistory: true,
		}

		rl, err := readline.NewEx(cfg)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Contract 1 failed: readline.NewEx returned error: %v\n", err)
			os.Exit(1)
		}
		if rl == nil {
			fmt.Fprintln(os.Stderr, "Contract 1 failed: readline.NewEx returned nil instance")
			os.Exit(1)
		}
		if rl.Config == nil || rl.Config.Prompt != "prompt> " {
			fmt.Fprintf(os.Stderr, "Contract 1 failed: rl.Config.Prompt got %q, want %q\n", rl.Config.Prompt, "prompt> ")
			_ = rl.Close()
			os.Exit(1)
		}
		_ = rl.Close()
	}

	// Contract 2: readline.NewEx reads sequential lines from configured custom Stdin
	{
		in := io.NopCloser(strings.NewReader("first line\nsecond line\n"))
		out := &bytes.Buffer{}
		cfg := &readline.Config{
			Prompt:                 "> ",
			Stdin:                  in,
			Stdout:                 out,
			DisableAutoSaveHistory: true,
		}

		rl, err := readline.NewEx(cfg)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Contract 2 failed: NewEx error: %v\n", err)
			os.Exit(1)
		}

		line1, err := rl.Readline()
		if err != nil || line1 != "first line" {
			fmt.Fprintf(os.Stderr, "Contract 2 failed: line 1 got %q (err: %v), want %q\n", line1, err, "first line")
			_ = rl.Close()
			os.Exit(1)
		}

		line2, err := rl.Readline()
		if err != nil || line2 != "second line" {
			fmt.Fprintf(os.Stderr, "Contract 2 failed: line 2 got %q (err: %v), want %q\n", line2, err, "second line")
			_ = rl.Close()
			os.Exit(1)
		}
		_ = rl.Close()
	}

	// Contract 3: readline.NewEx returns io.EOF on Readline when configured Stdin is exhausted
	{
		in := io.NopCloser(strings.NewReader("only-one\n"))
		out := &bytes.Buffer{}
		cfg := &readline.Config{
			Prompt:                 "> ",
			Stdin:                  in,
			Stdout:                 out,
			DisableAutoSaveHistory: true,
		}

		rl, err := readline.NewEx(cfg)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Contract 3 failed: NewEx error: %v\n", err)
			os.Exit(1)
		}

		_, err = rl.Readline()
		if err != nil {
			fmt.Fprintf(os.Stderr, "Contract 3 failed: first read error: %v\n", err)
			_ = rl.Close()
			os.Exit(1)
		}

		_, err = rl.Readline()
		if err != io.EOF {
			fmt.Fprintf(os.Stderr, "Contract 3 failed: expected io.EOF, got: %v\n", err)
			_ = rl.Close()
			os.Exit(1)
		}
		_ = rl.Close()
	}

	// Contract 4: readline.NewEx instance allows dynamic prompt updates via SetPrompt
	{
		in := io.NopCloser(strings.NewReader("sample\n"))
		out := &bytes.Buffer{}
		cfg := &readline.Config{
			Prompt:                 "initial> ",
			Stdin:                  in,
			Stdout:                 out,
			DisableAutoSaveHistory: true,
		}

		rl, err := readline.NewEx(cfg)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Contract 4 failed: NewEx error: %v\n", err)
			os.Exit(1)
		}

		rl.SetPrompt("updated> ")
		line, err := rl.Readline()
		if err != nil || line != "sample" {
			fmt.Fprintf(os.Stderr, "Contract 4 failed: Readline after SetPrompt failed: %v\n", err)
			_ = rl.Close()
			os.Exit(1)
		}
		_ = rl.Close()
	}

	// Contract 5: readline.NewEx accepts AutoComplete prefix completer in Config
	{
		completer := readline.NewPrefixCompleter(
			readline.PcItem("get"),
			readline.PcItem("set"),
		)
		in := io.NopCloser(strings.NewReader("get\n"))
		out := &bytes.Buffer{}
		cfg := &readline.Config{
			Prompt:                 "> ",
			Stdin:                  in,
			Stdout:                 out,
			AutoComplete:           completer,
			DisableAutoSaveHistory: true,
		}

		rl, err := readline.NewEx(cfg)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Contract 5 failed: NewEx with AutoComplete failed: %v\n", err)
			os.Exit(1)
		}

		if rl.Config.AutoComplete == nil {
			fmt.Fprintln(os.Stderr, "Contract 5 failed: AutoComplete is nil in rl.Config")
			_ = rl.Close()
			os.Exit(1)
		}

		line, err := rl.Readline()
		if err != nil || line != "get" {
			fmt.Fprintf(os.Stderr, "Contract 5 failed: Readline got %q, want 'get'\n", line)
			_ = rl.Close()
			os.Exit(1)
		}
		_ = rl.Close()
	}

	// Contract 6: readline.NewEx instance generates masked password config via GenPasswordConfig
	{
		in := io.NopCloser(strings.NewReader("data\n"))
		out := &bytes.Buffer{}
		cfg := &readline.Config{
			Prompt:                 "pass> ",
			Stdin:                  in,
			Stdout:                 out,
			DisableAutoSaveHistory: true,
		}

		rl, err := readline.NewEx(cfg)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Contract 6 failed: NewEx error: %v\n", err)
			os.Exit(1)
		}

		pwdCfg := rl.GenPasswordConfig()
		if pwdCfg == nil {
			fmt.Fprintln(os.Stderr, "Contract 6 failed: GenPasswordConfig returned nil")
			_ = rl.Close()
			os.Exit(1)
		}
		if !pwdCfg.EnableMask {
			fmt.Fprintln(os.Stderr, "Contract 6 failed: EnableMask should be true in GenPasswordConfig")
			_ = rl.Close()
			os.Exit(1)
		}
		_ = rl.Close()
	}

	// Contract 7: readline.NewEx instance Close method cleanly releases terminal resources
	{
		in := io.NopCloser(strings.NewReader(""))
		out := &bytes.Buffer{}
		cfg := &readline.Config{
			Prompt:                 "> ",
			Stdin:                  in,
			Stdout:                 out,
			DisableAutoSaveHistory: true,
		}

		rl, err := readline.NewEx(cfg)
		if err != nil {
			fmt.Fprintf(os.Stderr, "Contract 7 failed: NewEx error: %v\n", err)
			os.Exit(1)
		}

		if err := rl.Close(); err != nil {
			fmt.Fprintf(os.Stderr, "Contract 7 failed: rl.Close returned error: %v\n", err)
			os.Exit(1)
		}
	}

	fmt.Println("All contract assertions passed successfully.")
}

原始种子者

匿名