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
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,13 @@ because it turns other people's test suites red.

### Added

- **Copy command in the window.** The Single batch, Presets, Several batches
and Tools screens put on the clipboard the `tfg` command that does what the
screen would do, so a set you found in the window can go into a script or a
pipeline. It is written for PowerShell on Windows and for bash or zsh on
macOS and Linux, and it starts `tfg` the way your computer finds it. On
Several batches it saves the recipe of the screen as a file in the folder
the terminal is open in, then runs it.
- **Sample files to download on the website.** One file of every format at
exactly 1 MB, corrupt PDF, Word, Excel, PowerPoint, JPG, PNG, MP4 and ZIP
files, CSV, JSON and Excel tables for an import, and placeholder pictures in
Expand Down
4 changes: 3 additions & 1 deletion CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,9 @@ whose test suites depend on it.
`internal/guard` is also broken on purpose by an entry in
`internal/guard/mutation/entries.py`, and the `mutation` check on a pull
request runs the entries the change reaches. A new test needs an entry there,
or a line in `internal/guard/mutationcoverage_test.go` saying why it has none.
or a line saying why it has none: in `internal/guard/provenbyprobe_test.go`
for a test proven by a canary or by a break made by hand, in
`internal/guard/mutationcoverage_test.go` for one that is not proven yet.
- **Words a user reads are English, with a flat hyphen and no semicolons.** That
covers the README, the changelog, `--help`, every message, and every comment
in the code. A test enforces the punctuation.
Expand Down
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -895,6 +895,20 @@ beside earlier runs, sits under the output directory and stays on unless you
turn it off. With it off, such a run is refused, the same as on the command line
without `--number-if-taken`.

Copy command, on the Single batch, Presets, Several batches and Tools screens, puts on the
clipboard the `tfg` command that does what the screen would do, so a set you
found in the window can go into a script or a pipeline. On Several batches it
copies two commands: one saves the recipe of the screen as
`tfg-recipe-<12 characters>.yaml` in the folder the terminal is open in, and
the other runs `tfg generate` on that file. The name comes from the recipe
itself, so pasting the same commands again writes the same file. It is written for
PowerShell on Windows and for bash or zsh on macOS and Linux. It starts with
`tfg` when a terminal finds it by that name, with the full path of a `tfg` next
to the window when that is where it is, and the window says so when it finds
neither. A value with a double quote in it needs PowerShell 7.3 or later rather
than Windows PowerShell 5.1, and the window says that too. Settings the window would
refuse are refused here as well, under their boxes, and nothing is copied.

<p>
<img src=".github/window-presets.png" width="49%" alt="The Presets screen of the window, with the list of presets open">
<img src=".github/window-settings.png" width="49%" alt="One batch of log files in the window, with the settings the log format takes">
Expand Down
148 changes: 148 additions & 0 deletions internal/commandline/commandline.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// Package commandline writes the command that does what a screen of the window
// would do, for a person to paste into a terminal.
//
// # Why this exists
//
// The window and the command line run the same engine, and a person who has
// found the settings they want on a screen should be able to take them to a
// script or a pipeline without retyping them, which is the copy button on
// every screen of the window. The window decides what the command says and this
// package decides how it is written, because writing is the part that differs
// between systems and is easy to get wrong in a way that runs something else.
//
// # What this package is NOT
//
// It runs nothing, and it never finds anything by starting a process. It writes
// a line for a person to read and to paste. A window that ran the command line
// would be a second way to do everything, and the two would answer differently
// the first time one of them changed. Finding where tfg is reads PATH and asks
// the file system, the way the shell itself will - see program.go.
//
// It does not judge the values either. Whether a size is too small for a format
// is a question for the engine, and the command answers it when it runs, in the
// same words the window uses.
package commandline

import "strings"

// Line is a command being written out.
//
// The order things are added is the order they appear, because a person reads
// this to understand what the window would do, and a line whose flags arrive in
// a different order on every press is one they have to read again each time.
// Go randomises the order of a map by decision, so nothing here is built from one.
type Line struct {
verb []string
words []word
}

// word is one flag, one flag with its value, or one argument.
type word struct {
flag string
value string
valued bool
setting string
}

// New starts a line for a verb, such as "generate" or "tool checksum".
func New(verb ...string) *Line { return &Line{verb: verb} }

// Flag adds a flag with a value, and adds NOTHING when the value is empty.
//
// Empty means the person left the setting alone, and a flag carrying an empty
// value is not the same request: --seed with nothing after it is an error,
// while no --seed at all is what the window would do. setting is the key of
// the box the value came from, so that a value the line cannot carry is
// reported under that box.
func (l *Line) Flag(name, value, setting string) *Line {
if value == "" {
return l
}
l.words = append(l.words, word{flag: name, value: value, valued: true, setting: setting})
return l
}

// Switch adds a flag that carries no value, when it is on.
func (l *Line) Switch(name string, on bool) *Line {
if on {
l.words = append(l.words, word{flag: name})
}
return l
}

// Argument adds what the verb acts on, such as the file a tool reads. An empty
// one is left out, for the reason Flag gives.
func (l *Line) Argument(value, setting string) *Line {
if value == "" {
return l
}
l.words = append(l.words, word{value: value, valued: true, setting: setting})
return l
}

// Written is a line as text, and what somebody pasting it should know.
type Written struct {
// Text is the line itself, for the clipboard.
Text string
// Program is where the line found tfg on this computer.
Program Where
// OlderPowerShell lists the settings whose values Windows PowerShell 5.1,
// and PowerShell 7 before 7.3, hand to a program differently from
// PowerShell 7.3 and later, in the order they appear - see
// olderPowerShellDiffers. Empty for sh.
OlderPowerShell []string
// File is the name of the file the text saves before it runs tfg, in the
// folder the terminal is in - see Script. Empty for a single line.
File string
}

// Write is the line for one shell, started the way this computer starts tfg.
//
// It fails only on a value no line can carry, and the error is an *Unwritable
// naming the setting, so the window can mark the box it came from.
func (l *Line) Write(p Program, s Shell) (Written, error) {
parts := append([]string{p.word(s)}, l.verb...)
out := Written{Program: p.Where}
for _, w := range l.words {
text, err := w.write(s)
if err != nil {
return Written{}, err
}
parts = append(parts, text)
if s == PowerShell && w.valued && olderPowerShellDiffers(w.value) {
out.OlderPowerShell = append(out.OlderPowerShell, w.setting)
}
}
out.Text = strings.Join(parts, " ")
return out, nil
}

// write is one word as the shell should read it.
//
// A value that starts with a dash is joined to its flag as one quoted word,
// --name=-a.txt, and this is measured rather than tidy (docs, the analysis of
// 2026-11-25, section 2). Both versions of PowerShell split a bare -a.txt into
// two arguments, and read --% as the end of everything they parse, quoted or
// not - after which the rest of the line goes to the program as it stands. The
// joined form is the one that came through whole in both, and the flag package
// takes it. The same form in sh keeps the line one shape on every system.
func (w word) write(s Shell) (string, error) {
if !w.valued {
return w.flag, nil
}
if err := writable(w.value, w.setting); err != nil {
return "", err
}
switch {
case strings.HasPrefix(w.value, "-") && w.flag == "":
// An argument has no flag to be joined to. The window hands over
// absolute paths, which never start with a dash, so this is a
// refusal rather than a line that would be read as a flag.
return "", &Unwritable{Setting: w.setting, Reason: LeadingDash}
case strings.HasPrefix(w.value, "-"):
return quote(w.flag+"="+w.value, s), nil
case w.flag == "":
return quote(w.value, s), nil
}
return w.flag + " " + quote(w.value, s), nil
}
63 changes: 63 additions & 0 deletions internal/commandline/names.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package commandline

import (
"sort"
"strings"
)

// The verbs and flags a screen of the window has an equivalent for.
//
// Here rather than typed in the window, so that the names live in one place,
// and that they are the names the command line really takes is held by the
// guard that pastes a copied line into the command line and compares the files
// it makes with the files the window makes - copycommand_test.go. A name that
// drifted would be a flag the command line refuses, and that guard goes red.
const (
Generate = "generate"
Tool = "tool"

Clean = "--clean"
Count = "--count"
Damage = "--damage"
Format = "--format"
ID = "--id"
Memory = "--memory"
NameTemplate = "--name"
NumberIfTaken = "--number-if-taken"
Out = "--out"
Preset = "--preset"
Seed = "--seed"
Set = "--set"
Size = "--size"
)

// Named is the flag for a parameter of a preset or a setting of a tool, which
// are flags named after themselves.
func Named(name string) string { return "--" + name }

// DamageValue writes one damage as --damage takes it: the name, then a colon
// and its settings as name=value separated by commas.
//
// The settings go in the order of their names, because the flag reads them into
// a map and the order means nothing to it, and a line that kept the order of a
// map would differ on every press. A comma inside a value cannot be written,
// because the flag splits on every comma and has no way to escape one -
// internal/cli, damageSettings - so that is refused under the setting it is in.
func DamageValue(id string, values map[string]string) (string, error) {
names := make([]string, 0, len(values))
for name, value := range values {
if strings.Contains(value, ",") {
return "", &Unwritable{Setting: name, Reason: CommaInDamage}
}
names = append(names, name)
}
if len(names) == 0 {
return id, nil
}
sort.Strings(names)
pairs := make([]string, 0, len(names))
for _, name := range names {
pairs = append(pairs, name+"="+values[name])
}
return id + ":" + strings.Join(pairs, ","), nil
}
Loading
Loading