-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
331 additions
and
1 deletion.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,3 +4,4 @@ version = "0.1.0" | |
edition = "2021" | ||
|
||
[dependencies] | ||
clap = { version = "4.5.4", features = ["derive"] } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
.PHONY: all | ||
all: | ||
cargo build | ||
|
||
.PHONY: fmt | ||
fmt: | ||
cargo fmt | ||
|
||
.PHONY: lint | ||
lint: | ||
cargo clippy |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,50 @@ | ||
use clap::{Parser, Subcommand}; | ||
use std::io::{Read, Write}; | ||
|
||
#[derive(Parser)] | ||
struct Cli { | ||
#[command(subcommand)] | ||
command: Commands, | ||
} | ||
|
||
#[derive(Subcommand)] | ||
enum Commands { | ||
Cat { files: Option<Vec<String>> }, | ||
} | ||
|
||
fn main() { | ||
println!("Hello, world!"); | ||
let cli = Cli::parse(); | ||
|
||
match &cli.command { | ||
Commands::Cat { files } => { | ||
if let Some(files) = files { | ||
// stdout のロックを取得 | ||
let stdout = std::io::stdout(); | ||
let mut stdout = stdout.lock(); | ||
|
||
// files を入力順に stdout に出力する | ||
// 最後のファイルは改行をしない | ||
let mut iter = files.iter().peekable(); | ||
while let Some(file) = iter.next() { | ||
let content = std::fs::read_to_string(file).unwrap(); | ||
if iter.peek().is_none() { | ||
write!(&mut stdout, "{}", content).unwrap(); | ||
} else { | ||
writeln!(&mut stdout, "{}", content).unwrap(); | ||
} | ||
} | ||
} else { | ||
// files の指定がない場合は標準入力から読み取る | ||
let mut buffer = String::new(); | ||
let stdin = std::io::stdin(); | ||
let mut stdin = stdin.lock(); | ||
stdin.read_to_string(&mut buffer).unwrap(); | ||
|
||
// 標準出力に書き込む | ||
let stdout = std::io::stdout(); | ||
let mut stdout = stdout.lock(); | ||
write!(&mut stdout, "{}", buffer).unwrap(); | ||
} | ||
} | ||
} | ||
} |