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
35 changes: 24 additions & 11 deletions wayshot/src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,35 @@
use std::path::PathBuf;

use clap::{Parser, arg, builder::TypedValueParser};

use clap::{
Parser, arg,
builder::{
Styles, TypedValueParser,
styling::{AnsiColor, Effects},
},
};
use eyre::WrapErr;

use crate::utils::EncodingFormat;

fn get_styles() -> Styles {
Styles::styled()
.header(AnsiColor::Yellow.on_default() | Effects::BOLD)
.usage(AnsiColor::Green.on_default() | Effects::BOLD)
.literal(AnsiColor::Blue.on_default() | Effects::BOLD)
.placeholder(AnsiColor::Green.on_default())
}

#[derive(Parser)]
#[command(version, about)]
#[command(version, about, styles=get_styles())]
pub struct Cli {
/// Custom output path can be of the following types:
/// 1. Directory (Default naming scheme is used for the image output).
/// Custom screenshot file path can be of the following types:
/// 1. Directory (Default naming scheme is used for the screenshot file).
/// 2. Path (Encoding is automatically inferred from the extension).
/// 3. `-` (Indicates writing to terminal [stdout]).
#[arg(value_name = "OUTPUT", verbatim_doc_comment)]
#[arg(value_name = "FILE", verbatim_doc_comment)]
pub file: Option<PathBuf>,

/// Copy image to clipboard. Can be used simultaneously with [OUTPUT] or stdout.
/// Copy image to clipboard. Can be used simultaneously with [FILE].
/// Wayshot persists in the background offering the image till the clipboard is overwritten.
#[arg(long, verbatim_doc_comment)]
pub clipboard: bool,
Expand All @@ -33,20 +46,20 @@ pub struct Cli {
#[arg(short, long)]
pub cursor: bool,

/// Set image encoder, by default uses the file extension from the OUTPUT
/// Set image encoder, by default uses the file extension from the FILE
/// positional argument. Otherwise defaults to png.
#[arg(long, verbatim_doc_comment, visible_aliases = ["extension", "format", "output-format"], value_name = "FILE_EXTENSION")]
#[arg(long, verbatim_doc_comment, visible_aliases = ["extension", "format", "file-format"], value_name = "FILE_EXTENSION")]
pub encoding: Option<EncodingFormat>,

/// List all valid outputs
#[arg(short, long, alias = "listoutputs")]
#[arg(short, long, alias = "list-outputs")]
pub list_outputs: bool,

/// Choose a particular output/display to screenshot
#[arg(short, long, conflicts_with = "slurp")]
pub output: Option<String>,

/// Present a fuzzy selector for output/display selection
#[arg(long, alias = "chooseoutput", conflicts_with_all = ["slurp", "output"])]
#[arg(long, alias = "choose-output", conflicts_with_all = ["slurp", "output"])]
pub choose_output: bool,
}
14 changes: 9 additions & 5 deletions wayshot/src/wayshot.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use std::{
io::{BufWriter, Cursor, Write, stdout},
io::{self, BufWriter, Cursor, Write},
process::Command,
};

Expand Down Expand Up @@ -36,7 +36,7 @@ fn main() -> Result<()> {
let cli = cli::Cli::parse();
tracing_subscriber::fmt()
.with_max_level(cli.log_level)
.with_writer(std::io::stderr)
.with_writer(io::stderr)
.init();

let input_encoding = cli
Expand All @@ -58,11 +58,17 @@ fn main() -> Result<()> {

let wayshot_conn = WayshotConnection::new()?;

let stdout = io::stdout();
let mut writer = BufWriter::new(stdout.lock());

if cli.list_outputs {
let valid_outputs = wayshot_conn.get_all_outputs();
for output in valid_outputs {
tracing::info!("{:#?}", output.name);
writeln!(writer, "{}", output.name)?;
}

writer.flush()?;

return Ok(());
}

Expand Down Expand Up @@ -132,8 +138,6 @@ fn main() -> Result<()> {
} else if stdout_print {
let mut buffer = Cursor::new(Vec::new());
image_buffer.write_to(&mut buffer, requested_encoding.into())?;
let stdout = stdout();
let mut writer = BufWriter::new(stdout.lock());
writer.write_all(buffer.get_ref())?;
image_buf = Some(buffer);
}
Expand Down