Skip to content

Commit 34f1b2e

Browse files
author
8go
committed
added streaming to --message via shortcut letter '_'
see --help for more details.
1 parent 90fe432 commit 34f1b2e

File tree

6 files changed

+124
-27
lines changed

6 files changed

+124
-27
lines changed

Cargo.lock

Lines changed: 21 additions & 21 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
[package]
44
name = "matrix-commander"
5-
version = "0.1.23"
5+
version = "0.1.24"
66
edition = "2021"
77
description = "simple but convenient CLI-based Matrix client app for sending and receiving"
88
documentation = "https://docs.rs/matrix-commander"

README.md

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,18 @@ Options:
287287
Keyboard input is limited to one line. The stdin indicator '-' may
288288
appear in any position, i.e. -m 'start' '-' 'end' will send 3
289289
messages out of which the second one is read from stdin. The stdin
290-
indicator '-' may appear only once overall in all arguments
290+
indicator '-' may appear only once overall in all arguments. '-'
291+
reads everything that is in the pipe in one swoop and sends a single
292+
message. Similar to '-', another shortcut character is '_'. The
293+
special character '_' is used for streaming data via a pipe on stdin.
294+
With '_' the stdin pipe is read line-by-line and each line is treated
295+
as a separate message and sent right away. The program waits for pipe
296+
input until the pipe is closed. E.g. Imagine a tool that generates
297+
output sporadically 24x7. It can be piped, i.e. streamed, into
298+
matrix- commander, and matrix-commander stays active, sending all
299+
input instantly. If you want to send the literal letter '_' then
300+
escape it and send '\_'. '_' can be used only once. And either '-' or
301+
'_' can be used
291302
--markdown
292303
There are 3 message formats for '--message'. Plain text, MarkDown,
293304
and Code. By default, if no command line options are specified,

VERSION

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
0.1.23
1+
0.1.24

help.txt

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -196,7 +196,18 @@ Options:
196196
Keyboard input is limited to one line. The stdin indicator '-' may
197197
appear in any position, i.e. -m 'start' '-' 'end' will send 3
198198
messages out of which the second one is read from stdin. The stdin
199-
indicator '-' may appear only once overall in all arguments
199+
indicator '-' may appear only once overall in all arguments. '-'
200+
reads everything that is in the pipe in one swoop and sends a single
201+
message. Similar to '-', another shortcut character is '_'. The
202+
special character '_' is used for streaming data via a pipe on stdin.
203+
With '_' the stdin pipe is read line-by-line and each line is treated
204+
as a separate message and sent right away. The program waits for pipe
205+
input until the pipe is closed. E.g. Imagine a tool that generates
206+
output sporadically 24x7. It can be piped, i.e. streamed, into
207+
matrix- commander, and matrix-commander stays active, sending all
208+
input instantly. If you want to send the literal letter '_' then
209+
escape it and send '\_'. '_' can be used only once. And either '-' or
210+
'_' can be used
200211
--markdown
201212
There are 3 message formats for '--message'. Plain text, MarkDown,
202213
and Code. By default, if no command line options are specified,

src/main.rs

Lines changed: 77 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -730,6 +730,7 @@ pub struct Args {
730730
#[arg(long, action = clap::ArgAction::Count, default_value_t = 0u8, )]
731731
verbose: u8,
732732

733+
// Todo
733734
/// Disable encryption for a specific action. By default encryption is
734735
/// turned on wherever possible. E.g. rooms created will be created
735736
/// by default with encryption enabled. To turn encryption off for a
@@ -967,6 +968,20 @@ pub struct Args {
967968
/// i.e. -m 'start' '-' 'end'
968969
/// will send 3 messages out of which the second one is read from stdin.
969970
/// The stdin indicator '-' may appear only once overall in all arguments.
971+
/// '-' reads everything that is in the pipe in one swoop and
972+
/// sends a single message.
973+
/// Similar to '-', another shortcut character
974+
/// is '_'. The special character '_' is used for
975+
/// streaming data via a pipe on stdin. With '_' the stdin
976+
/// pipe is read line-by-line and each line is treated as
977+
/// a separate message and sent right away. The program
978+
/// waits for pipe input until the pipe is closed. E.g.
979+
/// Imagine a tool that generates output sporadically
980+
/// 24x7. It can be piped, i.e. streamed, into matrix-
981+
/// commander, and matrix-commander stays active, sending
982+
/// all input instantly. If you want to send the literal
983+
/// letter '_' then escape it and send '\_'. '_' can be
984+
/// used only once. And either '-' or '_' can be used.
970985
#[arg(short, long, num_args(0..), )]
971986
message: Vec<String>,
972987

@@ -2449,6 +2464,16 @@ pub(crate) async fn cli_verify(client: &Client, ap: &Args) -> Result<(), Error>
24492464
crate::verify(client).await
24502465
}
24512466

2467+
fn trim_newline(s: &mut String) -> &mut String {
2468+
if s.ends_with('\n') {
2469+
s.pop();
2470+
if s.ends_with('\r') {
2471+
s.pop();
2472+
}
2473+
}
2474+
return s;
2475+
}
2476+
24522477
/// Handle the --message CLI argument
24532478
pub(crate) async fn cli_message(client: &Client, ap: &Args) -> Result<(), Error> {
24542479
info!("Message chosen.");
@@ -2479,16 +2504,64 @@ pub(crate) async fn cli_message(client: &Client, ap: &Args) -> Result<(), Error>
24792504
io::stdin().read_to_string(&mut line)?;
24802505
}
24812506
line
2507+
} else if msg == r"_" {
2508+
let mut eof = false;
2509+
while !eof {
2510+
let mut line = String::new();
2511+
match io::stdin().read_line(&mut line) {
2512+
// If this function returns Ok(0), the stream has reached EOF.
2513+
Ok(n) => {
2514+
if n == 0 {
2515+
eof = true;
2516+
debug!("Reached EOF of pipe stream.");
2517+
} else {
2518+
debug!(
2519+
"Read {n} bytes containing \"{}\\n\" from pipe stream.",
2520+
trim_newline(&mut line.clone())
2521+
);
2522+
match message(
2523+
client,
2524+
&[line],
2525+
&ap.room,
2526+
ap.code,
2527+
ap.markdown,
2528+
ap.notice,
2529+
ap.emote,
2530+
)
2531+
.await
2532+
{
2533+
Ok(()) => {
2534+
debug!("message from pipe stream sent successfully");
2535+
}
2536+
Err(ref e) => {
2537+
error!(
2538+
"Error: sending message from pipe stream reported {}",
2539+
e
2540+
);
2541+
}
2542+
}
2543+
}
2544+
}
2545+
Err(ref e) => {
2546+
error!("Error: reading from pipe stream reported {}", e);
2547+
}
2548+
}
2549+
}
2550+
"".to_owned()
24822551
} else if msg == r"\-" {
24832552
"-".to_string()
2553+
} else if msg == r"\_" {
2554+
"_".to_string()
24842555
} else if msg == r"\-\-" {
24852556
"--".to_string()
24862557
} else if msg == r"\-\-\-" {
24872558
"---".to_string()
24882559
} else {
24892560
msg.to_string()
24902561
};
2491-
fmsgs.push(fmsg);
2562+
if !fmsg.is_empty() {
2563+
fmsgs.push(fmsg);
2564+
}
24922565
}
24932566
if fmsgs.is_empty() {
24942567
return Ok(()); // nothing to do
@@ -2860,7 +2933,9 @@ async fn main() -> Result<(), Error> {
28602933

28612934
eprintln!("WARNING: Incompatible change between version v0.1.20 and v0.1.21.");
28622935
eprintln!("If your credentials file was created with Python, don't do anything.");
2863-
eprintln!("If your credentials file was created with Rust, follow either Option 1 or Option 2.");
2936+
eprintln!(
2937+
"If your credentials file was created with Rust, follow either Option 1 or Option 2."
2938+
);
28642939
eprintln!("Option 1: Please edit your Rust-created credentials file (usually at $HOME/.local/share/matrix-commander-rs/credentials.json).");
28652940
eprintln!("Replace \"room_default\" with \"room_id\".");
28662941
eprintln!("Option 2: Alternatively you can delete the credentials file and create a new one by logging in again. ");

0 commit comments

Comments
 (0)