numtide / treefmt

one CLI to format your repo [maintainers=@zimbatm,@brianmcgee]
https://treefmt.com
MIT License
606 stars 37 forks source link

what are the stdio flags that a plugin needs to support ? #425

Open gedw99 opened 2 weeks ago

gedw99 commented 2 weeks ago

Hey,

pretty cool stuff.

I have a file format that is just a text format for a Science Project, and wrote a golang based Fmt thing that parses the file and spits out into via STDOUT.

I had a look at the docs at https://treefmt.com/formatter-spec, but did not see the stdio args needed.

Can you let me know the args for input and output that it needs to support to work with treefmt ?

gedw99 commented 2 weeks ago

I got a Test harness setup but:

treefmt .  --verbose -vv --on-unmatched debug
DEBU format: config-file=/Users/apple/workspace/go/src/numtide__treefmt/.test/dsh/treefmt.toml tree-root=/Users/apple/workspace/go/src/numtide__treefmt/.test/dsh
DEBU cache: formatter '%s' has changed dsh=size 2473522=modTime 2024-10-05 19:16:22.402481699 +1000 AEST=cachedSize 2420530=cachedModTime 2024-10-05 18:28:44.353360791 +1000 AEST="missing value"
DEBU format | dsh: match: /Users/apple/workspace/go/numtide__treefmt/.test/dsh/test-02-sub.dsh
DEBU format | dsh: match: /Users/apple/workspace/go/numtide__treefmt/.test/dsh/test-02.dsh
DEBU format: no formatter for path: test-dsh.toml
DEBU format | dsh: match: /Users/apple/workspace/go/numtide__treefmt/.test/dsh/test.dsh
DEBU format: no formatter for path: treefmt.toml
DEBU cache: finished generating change set in 124.375µs
DEBU format | dsh: executing: /Users/apple/workspace/go/numtide__treefmt/.dep/dshfmt -w test-02-sub.dsh test-02.dsh test.dsh
ERRO format | dsh: failed to apply with options '[-w]': exit status 2
dsh error:
flag provided but not defined: -w
Usage of /Users/apple/workspace/go/src/numtide__treefmt/.bin/dshfmt
  -dump
        dump raw parsed data
  -fsort
        show keyword frequencies
  -i string
        indent (default "\t")
  -ksort
        show keyword counts
  -v    verbose diagnostics

dshfmt is my binary.

Seems it needs -w to be supported and then sub call into one of my current args that are above maybe ?

zimbatm commented 2 weeks ago

If dshfmt wrapping another formatter?

Try changing it so it takes the list of files to format as an argument, and then writes the result directly to the file if the content has changed.

Here is a small wrapper in case dshfmt is a wrapper

#!/usr/bin/env bash

# Iterate on each file passed as arguments
for file in "$@"; do
  original="$(< "$file")
  # mystdioformatter is the original formatter that only supports stdio 
  new=$(mystdioformatter < "$file")
  # only write back the change if the content has changed, to adhere to the formatter spec
  if [[ $original != $new ]]; then
    echo -n "$new" > "$file"
  fi
done
gedw99 commented 2 weeks ago

Thanks !!

dshfmt Is golang binary with stdio support .

it only supports a single file at a time being passed in as the arg with no flag .

If you happen to know I am golang example I am look at that .

prefer not to have to use a bank wrapper as then it will not work on all OS ..

gedw99 commented 2 weeks ago

Thanks !!

dshfmt Is golang binary with stdio support .

it only supports a single file at a time being passed in as the arg with no flag .

If you happen to know I am golang example I am look at that .

prefer not to have to use a bank wrapper as then it will not work on all OS ..