libaki_mline/lib.rs
1/*!
2the match line, regex text filter like a grep of linux command.
3
4# Features
5
6- the match line, regex text filter like a grep of linux command.
7- minimum support rustc 1.65.0 (897e37553 2022-11-02)
8
9# Command help
10
11```text
12aki-mline --help
13```
14
15```text
16Usage:
17 aki-mline [options]
18
19match line, regex text filter like a grep.
20
21Options:
22 --color <when> use markers to highlight the matching strings
23 -e, --exp <exp> regular expression
24 -s, --str <string> simple string match
25 -i, --inverse output non-matching lines.
26
27 -H, --help display this help and exit
28 -V, --version display version information and exit
29
30Option Parameters:
31 <when> 'always', 'never', or 'auto'
32 <exp> regular expression
33 <string> simple string, non regular expression
34
35Environments:
36 AKI_MLINE_COLOR_SEQ_ST color start sequence specified by ansi
37 AKI_MLINE_COLOR_SEQ_ED color end sequence specified by ansi
38```
39
40# Quick install
41
421. you can install this into cargo bin path:
43
44```text
45cargo install aki-mline
46```
47
482. you can build debian package:
49
50```text
51cargo deb
52```
53
54and install **.deb** into your local repository of debian package.
55
56# Examples
57
58## Command line example 1
59
60Extract "`arm.*-gnu`" from the rustup target list
61
62```text
63rustup target list | aki-mline -e "arm.*-gnu"
64```
65
66result output :
67
68![out rustup image]
69
70[out rustup image]: https://raw.githubusercontent.com/aki-akaguma/aki-mline/main/img/out-rustup-1.png
71
72
73## Command line example 2
74
75Extract "`apple`" from the rustup target list
76
77```text
78rustup target list | aki-mline -s "apple"
79```
80
81result output :
82
83![out rustup image 2]
84
85[out rustup image 2]: https://raw.githubusercontent.com/aki-akaguma/aki-mline/main/img/out-rustup-2.png
86
87# Library example
88
89See [`fn execute()`] for this library examples.
90
91[`fn execute()`]: crate::execute
92
93*/
94#[macro_use]
95extern crate anyhow;
96
97pub mod conf;
98mod run;
99mod util;
100
101use flood_tide::HelpVersion;
102use runnel::*;
103
104const TRY_HELP_MSG: &str = "Try --help for help.";
105
106///
107/// execute mline
108///
109/// params:
110/// - sioe: stream in/out/err
111/// - program: program name. etc. "mline"
112/// - args: parameter arguments.
113///
114/// return:
115/// - ok: ()
116/// - err: anyhow
117///
118/// example:
119///
120/// ```
121/// use runnel::RunnelIoeBuilder;
122///
123/// let r = libaki_mline::execute(&RunnelIoeBuilder::new().build(),
124/// "mline", ["-e", "Error:.*"]);
125/// ```
126///
127pub fn execute<I, S>(sioe: &RunnelIoe, prog_name: &str, args: I) -> anyhow::Result<()>
128where
129 I: IntoIterator<Item = S>,
130 S: AsRef<std::ffi::OsStr>,
131{
132 execute_with_env(sioe, prog_name, args, vec![("", "")])
133}
134
135///
136/// execute mline with environments
137///
138/// params:
139/// - sioe: stream in/out/err
140/// - program: program name. etc. "mline"
141/// - args: parameter arguments.
142/// - env: environments array.
143///
144/// return:
145/// - ok: ()
146/// - err: anyhow
147///
148/// example:
149///
150/// ```rust
151/// use runnel::RunnelIoeBuilder;
152///
153/// let r = libaki_mline::execute_with_env(&RunnelIoeBuilder::new().build(),
154/// "mline",
155/// ["-e", "Error:.*"],
156/// vec![
157/// ("AKI_MLINE_COLOR_SEQ_ST", "<S>"),
158/// ("AKI_MLINE_COLOR_SEQ_ED","<E>"),
159/// ]
160/// );
161/// ```
162///
163pub fn execute_with_env<I, S, IKV, K, V>(
164 sioe: &RunnelIoe,
165 prog_name: &str,
166 args: I,
167 env: IKV,
168) -> anyhow::Result<()>
169where
170 I: IntoIterator<Item = S>,
171 S: AsRef<std::ffi::OsStr>,
172 IKV: IntoIterator<Item = (K, V)>,
173 K: AsRef<std::ffi::OsStr>,
174 V: AsRef<std::ffi::OsStr>,
175{
176 let args: Vec<String> = args
177 .into_iter()
178 .map(|s| s.as_ref().to_string_lossy().into_owned())
179 .collect();
180 let args_str: Vec<&str> = args.iter().map(|s| s.as_str()).collect();
181 let env_cnf: conf::EnvConf = env.into();
182 //
183 match conf::parse_cmdopts(prog_name, &args_str) {
184 Ok(conf) => run::run(sioe, &conf, &env_cnf),
185 Err(errs) => {
186 if let Some(err) = errs.iter().find(|e| e.is_help() || e.is_version()) {
187 sioe.pg_out().write_line(err.to_string())?;
188 Ok(())
189 } else {
190 Err(anyhow!("{errs}\n{TRY_HELP_MSG}"))
191 }
192 }
193 }
194}