feat(args): add --section argument for tui

This commit is contained in:
Orhun Parmaksız 2022-01-28 02:09:40 +03:00
parent 04b056c202
commit cb362c5f19
No known key found for this signature in database
GPG key ID: F83424824B3E4B90
3 changed files with 25 additions and 10 deletions

View file

@ -43,13 +43,13 @@ pub struct App<'a> {
impl<'a> App<'a> {
/// Constructs a new instance.
pub fn new(sysctl: &'a mut Sysctl, search_query: Option<String>) -> Self {
pub fn new(sysctl: &'a mut Sysctl) -> Self {
let mut app = Self {
running: true,
search_mode: search_query.is_some(),
input: search_query,
input: None,
input_time: None,
input_cursor: 0,
search_mode: false,
docs_scroll_amount: 0,
options: None,
parameter_list: SelectableList::default(),
@ -66,11 +66,6 @@ impl<'a> App<'a> {
sysctl,
};
app.parameter_list.items = app.sysctl.parameters.clone();
if app.search_mode {
app.search();
app.input = None;
app.search_mode = false;
}
#[cfg(feature = "clipboard")]
{
app.clipboard = match DisplayServer::select().try_context() {
@ -93,7 +88,7 @@ impl<'a> App<'a> {
}
/// Performs a search operation in the kernel parameter list.
fn search(&mut self) {
pub fn search(&mut self) {
let section = self
.section_list
.selected()

View file

@ -1,5 +1,6 @@
use getopts::Options;
use std::path::PathBuf;
use systeroid_core::sysctl::section::Section;
/// Help message for the arguments.
const HELP_MESSAGE: &str = r#"
@ -18,6 +19,8 @@ pub struct Args {
pub tick_rate: u64,
/// Path of the Linux kernel documentation.
pub kernel_docs: Option<PathBuf>,
/// Sysctl section to filter.
pub section: Option<Section>,
/// Query to search on startup.
pub search_query: Option<String>,
/// Do not parse/show Linux kernel documentation.
@ -40,6 +43,7 @@ impl Args {
"set the path of the kernel documentation",
"<path>",
);
opts.optopt("s", "section", "set the section to filter", "<section>");
opts.optopt("q", "query", "set the query to search", "<query>");
opts.optflag("n", "no-docs", "do not show the kernel documentation");
opts.optflag("h", "help", "display this help and exit");
@ -73,6 +77,7 @@ impl Args {
.ok()?
.unwrap_or(250),
kernel_docs: matches.opt_str("D").map(PathBuf::from),
section: matches.opt_str("s").map(Section::from),
search_query: matches.opt_str("q"),
no_docs: matches.opt_present("n"),
})

View file

@ -48,7 +48,22 @@ pub fn run<Output: Write>(args: Args, output: Output) -> Result<()> {
if !args.no_docs {
sysctl.update_docs_from_cache(args.kernel_docs.as_ref(), &Cache::init()?)?;
}
let mut app = App::new(&mut sysctl, args.search_query);
let mut app = App::new(&mut sysctl);
if let Some(section) = args.section {
app.section_list.state.select(Some(
app.section_list
.items
.iter()
.position(|r| r == &section.to_string())
.unwrap_or(0),
));
app.search();
}
if args.search_query.is_some() {
app.input = args.search_query;
app.search();
app.input = None;
}
while app.running {
terminal.draw(|frame| ui::render(frame, &mut app))?;
match event_handler.next()? {