CodeSampleX

サンプル

golang.org/x/term v0.41.0: IsTerminal, NewTerminal, Terminal.ReadLine

検証済みサンプル — golang golang.org/x/term v0.41.0: IsTerminal, NewTerminal, Terminal.ReadLine. go 1.26 · linux debian/x64 · docker で contract を実行し、成功しました.

sha256:314f9c9b2ea0e8be9b24998f1bda60c5a4edb21850fecfc4c390fcd308544676

このネットワークが提供するのは一つだけです。ビルドされるサンプル。サンドボックスで実行し、署名済みの受領証を保管します。等級はつけず、何も保証しません — 同じコードがあなたの環境でビルドされるかは測定していません。 合格した契約受領証を提出した異なる署名鍵の数です。1 なら作者だけ、2 以上なら他の誰かもビルドしています。鍵は自己生成で背後に登録された身元がないため、数えているのは人ではなく鍵です。 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-10-14

ケース

HOW
ゴール
verify pkg:golang/golang.org/x/term@v0.41.0
パッケージ
シンボル
  • term.IsTerminal
  • term.NewTerminal
  • term.Terminal.ReadLine
  • term.Terminal.Write
  • term.Terminal.SetPrompt
  • term.Terminal.SetSize
作成日
2026-10-14T16:37:12Z

コントラクト

  1. term.IsTerminal returns false for negative and pipe file descriptors
  2. term.NewTerminal reads CRLF- and LF-terminated lines via ReadLine and returns io.EOF on exhausted input
  3. Terminal.SetPrompt, Terminal.SetSize, and Terminal.Write update terminal state and emit output with populated Escape sequences

ファイル

  • 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 pkg:golang/golang.org/x/term@v0.41.0
Kind: HOW

Use EXACTLY these public packages and versions:
  - pkg:golang/golang.org/x/term@v0.41.0

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:ce12620bac04408eb6e584fc22a2e2491a328797cca50287973a0091a0500a53","contract":["term.IsTerminal returns false for negative and pipe file descriptors","term.NewTerminal reads CRLF- and LF-terminated lines via ReadLine and returns io.EOF on exhausted input","Terminal.SetPrompt, Terminal.SetSize, and Terminal.Write update terminal state and emit output with populated Escape sequences"],"goal":"verify pkg:golang/golang.org/x/term@v0.41.0","kind":"HOW","packages":["pkg:golang/golang.org/x/term@v0.41.0"],"schemaVersion":1,"symbols":["term.IsTerminal","term.NewTerminal","term.Terminal.ReadLine","term.Terminal.Write","term.Terminal.SetPrompt","term.Terminal.SetSize"]},"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/golang.org/x/term@v0.41.0"],"schemaVersion":1,"subject":"pkg:golang/golang.org/x/term@v0.41.0","symbols":["term.IsTerminal","term.NewTerminal","term.Terminal.ReadLine","term.Terminal.Write","term.Terminal.SetPrompt","term.Terminal.SetSize"],"verifierAdapter":"golang@1"}
go.mod
module example.com/sample

go 1.25.0

require golang.org/x/term v0.41.0

require golang.org/x/sys v0.42.0 // indirect
go.sum
golang.org/x/sys v0.42.0 h1:omrd2nAlyT5ESRdCLYdm3+fMfNFE/+Rf4bDIQImRJeo=
golang.org/x/sys v0.42.0/go.mod h1:4GL1E5IUh+htKOUEOaiffhrAeqysfVGipDYzABqnCmw=
golang.org/x/term v0.41.0 h1:QCgPso/Q3RTJx2Th4bDLqML4W6iJiaXFq2/ftQF13YU=
golang.org/x/term v0.41.0/go.mod h1:3pfBgksrReYfZ5lvYM0kSO0LIkAl4Yl2bXOkKP7Ec2A=
main.go
package main

import (
	"bytes"
	"fmt"
	"io"

	"golang.org/x/term"
)

type rwBuffer struct {
	r io.Reader
	w *bytes.Buffer
}

func (b *rwBuffer) Read(p []byte) (int, error) {
	return b.r.Read(p)
}

func (b *rwBuffer) Write(p []byte) (int, error) {
	return b.w.Write(p)
}

func main() {
	out := &bytes.Buffer{}
	rw := &rwBuffer{
		r: bytes.NewBufferString("status\r\n"),
		w: out,
	}
	t := term.NewTerminal(rw, "> ")
	line, err := t.ReadLine()
	fmt.Printf("isTerminal(-1)=%v line=%q err=%v resetLen=%d\n",
		term.IsTerminal(-1), line, err, len(t.Escape.Reset))
}
spec.json
{
  "schemaVersion": 1,
  "goal": "verify pkg:golang/golang.org/x/term@v0.41.0",
  "kind": "HOW",
  "packages": [
    "pkg:golang/golang.org/x/term@v0.41.0"
  ]
}
test/contract.go
package main

import (
	"bytes"
	"errors"
	"fmt"
	"io"
	"os"

	"golang.org/x/term"
)

type duplexBuffer struct {
	in  *bytes.Buffer
	out *bytes.Buffer
}

func (d *duplexBuffer) Read(p []byte) (int, error) {
	return d.in.Read(p)
}

func (d *duplexBuffer) Write(p []byte) (int, error) {
	return d.out.Write(p)
}

func fail(msg string, args ...any) {
	fmt.Fprintf(os.Stderr, "FAIL: "+msg+"\n", args...)
	os.Exit(1)
}

func main() {
	// 1. term.IsTerminal returns false for negative and non-terminal file descriptors.
	if term.IsTerminal(-1) {
		fail("expected term.IsTerminal(-1) to return false")
	}
	rPipe, wPipe, err := os.Pipe()
	if err != nil {
		fail("os.Pipe failed: %v", err)
	}
	defer rPipe.Close()
	defer wPipe.Close()
	if term.IsTerminal(int(rPipe.Fd())) || term.IsTerminal(int(wPipe.Fd())) {
		fail("expected term.IsTerminal to return false for pipe file descriptors")
	}

	// 2. term.NewTerminal reads CRLF- and LF-terminated lines and returns io.EOF when input is exhausted.
	ioBuf := &duplexBuffer{
		in:  bytes.NewBufferString("first line\r\nsecond line\n"),
		out: &bytes.Buffer{},
	}
	terminal := term.NewTerminal(ioBuf, "prompt> ")
	line1, err := terminal.ReadLine()
	if err != nil || line1 != "first line" {
		fail("expected first ReadLine to return (\"first line\", nil), got (%q, %v)", line1, err)
	}
	line2, err := terminal.ReadLine()
	if err != nil || line2 != "second line" {
		fail("expected second ReadLine to return (\"second line\", nil), got (%q, %v)", line2, err)
	}
	_, err = terminal.ReadLine()
	if !errors.Is(err, io.EOF) {
		fail("expected ReadLine on exhausted input to return io.EOF, got %v", err)
	}

	// 3. Terminal.SetPrompt, Terminal.SetSize, Terminal.Write, and Terminal.Escape behave as expected.
	ioBuf.out.Reset()
	terminal.SetPrompt("cmd$ ")
	if err := terminal.SetSize(100, 40); err != nil {
		fail("expected SetSize(100, 40) to succeed, got %v", err)
	}
	n, err := terminal.Write([]byte("notice\n"))
	if err != nil || n != len("notice\n") {
		fail("expected Write to return (7, nil), got (%d, %v)", n, err)
	}
	if !bytes.Contains(ioBuf.out.Bytes(), []byte("notice")) {
		fail("expected terminal output buffer to contain written message, got %q", ioBuf.out.String())
	}
	if len(terminal.Escape.Reset) == 0 || len(terminal.Escape.Red) == 0 {
		fail("expected Terminal.Escape ANSI codes to be populated")
	}

	fmt.Println("PASS")
}

オリジンシーダー

匿名