From b8cbc00956dafbcefc19b7f79069227cfd41eec1 Mon Sep 17 00:00:00 2001 From: luofei Date: Wed, 19 Oct 2022 13:07:42 +0800 Subject: [PATCH] parse output content by header label --- Cargo.toml | 1 + src/lib.rs | 8 ++++++-- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 227a61f..c37b879 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -16,3 +16,4 @@ strum = "0.24.0" strum_macros = "0.24.0" itertools = "0.10.3" default-env = "0.1" +regex = "1.6.0" diff --git a/src/lib.rs b/src/lib.rs index 16716ab..515279c 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,7 @@ //! Homepage: use std::str::FromStr; use std::process::ExitStatus; +use regex::Regex; use strum_macros::EnumString; use std::io::{Read, Error, ErrorKind}; @@ -105,10 +106,13 @@ pub fn list_units (type_filter: Option<&str>, state_filter: Option<&str>) -> std } let mut result : Vec = Vec::new(); let content = systemctl_capture(args)?; - let lines = content.lines(); + let mut lines = content.lines(); + let seperator = Regex::new("\\s{2,}").unwrap(); + let header = lines.next().unwrap(); + let header_len = seperator.split(header).count(); for l in lines.skip(1) { // header labels let parsed : Vec<_> = l.split_ascii_whitespace().collect(); - if parsed.len() == 2 { + if parsed.len() == header_len { result.push(parsed[0].to_string()) } }