Skip to content

Commit 69891ed

Browse files
authored
Default Config path to CWD if not specified; Update README.md (#26)
dtsfmt < input.dts > output.dts now works without needing to specify a filename argument. Fixes #19
1 parent 6af969c commit 69891ed

File tree

2 files changed

+35
-12
lines changed

2 files changed

+35
-12
lines changed

README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,22 @@ dtsfmt .
3232
The following configuration options are available for dtsfmt. Configuration should
3333
be added to a `.dtsfmtrc.toml` file at the root of your project.
3434

35+
36+
```toml
37+
indent_str = " " # Optional, used for each indent level when printing.
38+
# Default is two spaces.
39+
```
40+
3541
```toml
3642
layout = "kinesis:adv360" # Required
3743
# Available options are ["kinesis:adv360", "sweep", "moergo:glove80"]
3844
```
3945

46+
```toml
47+
warn_on_unhandled_tokens = false # Optional
48+
# Used to check if an input file contains any tokens not handled by the parser/printer.
49+
```
50+
4051
## Ignoring code
4152

4253
You can add a `.dtsfmtignore` file at the root of your project to exclude files
@@ -54,10 +65,10 @@ list of unformatted files, if any.
5465
dtsfmt --check .
5566
```
5667

57-
### `--emit`
68+
### `--stdin`
5869

59-
You can change the way dtsfmt emits the changes with the `--emit` flag.
70+
If passed the `--stdin` flag dtsfmt will read from stdin and write to stdout.
6071

6172
```bash
62-
dtsfmt --emit=stdout
73+
dtsfmt --stdin < input.dts > output.dts
6374
```

src/main.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ enum FormattingStatus {
1515
Unchanged,
1616
}
1717

18-
#[derive(Parser)]
18+
#[derive(Clone, Parser)]
1919
#[command(author, version, about, long_about = None)]
2020
struct Cli {
2121
/// Check for formatting errors without writing to the file
@@ -28,10 +28,10 @@ struct Cli {
2828

2929
/// The file to format
3030
#[arg(index = 1, value_name = "FILE")]
31-
file_path: PathBuf,
31+
file_path: Option<PathBuf>,
3232
}
3333

34-
fn format_fs(cli: &Cli, config: &Config) -> bool {
34+
fn format_fs(cli: &Cli, config: &Config, dir_path: &Path) -> bool {
3535
let mut emitter = create_emitter(false);
3636
let mut has_errors = false;
3737

@@ -40,7 +40,7 @@ fn format_fs(cli: &Cli, config: &Config) -> bool {
4040
types.add("devicetree", "*.keymap").unwrap();
4141
types.select("devicetree");
4242

43-
for result in WalkBuilder::new(&cli.file_path)
43+
for result in WalkBuilder::new(dir_path)
4444
.types(types.build().unwrap())
4545
.add_custom_ignore_filename(".dtsfmtignore")
4646
.standard_filters(false)
@@ -64,14 +64,14 @@ fn format_fs(cli: &Cli, config: &Config) -> bool {
6464
has_errors
6565
}
6666

67-
fn format_stdin(cli: &Cli, config: &Config) -> bool {
67+
fn format_stdin(cli: &Cli, config: &Config, dir_path: &Path) -> bool {
6868
let mut emitter = create_emitter(true);
6969
let mut buffer = String::new();
7070
io::stdin().read_to_string(&mut buffer).expect("Failed to read stdin");
7171

7272
// If the file is ignored, we need to print the original content unchanged
7373
// since we still need to return content when running in stdin mode.
74-
let status = if is_ignored(&cli.file_path) {
74+
let status = if is_ignored(dir_path) {
7575
print_original(cli, &mut emitter, &buffer)
7676
} else {
7777
format(PathBuf::from("stdin"), buffer, &mut emitter, config, cli.check)
@@ -82,12 +82,24 @@ fn format_stdin(cli: &Cli, config: &Config) -> bool {
8282

8383
fn main() {
8484
let cli = Cli::parse();
85-
let config = Config::parse(&cli.file_path.to_path_buf());
85+
86+
let cfg_path = match &cli.file_path {
87+
Some(path) => path.clone(),
88+
None => std::env::current_dir().expect("Couldn't read CWD"),
89+
};
90+
let config = Config::parse(&cfg_path);
91+
92+
// If no path was specified (likely with --stdin) then default to the
93+
// current working directory.
94+
let dir_path = match &cli.file_path {
95+
Some(path) => path.clone(),
96+
None => std::env::current_dir().expect("Couldn't read CWD"),
97+
};
8698

8799
let has_errors = if cli.stdin {
88-
format_stdin(&cli, &config)
100+
format_stdin(&cli, &config, &dir_path)
89101
} else {
90-
format_fs(&cli, &config)
102+
format_fs(&cli, &config, &dir_path)
91103
};
92104

93105
if cli.check && has_errors {

0 commit comments

Comments
 (0)