Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

For #152 - Remove csv dependency #156

Merged
merged 1 commit into from
Apr 4, 2024
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
22 changes: 0 additions & 22 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ subprocess = "0.2"
chrono = "0.4"
hostname = "0.3"
clap = { version = "4.5", features = ["derive"] }
csv = "1.3"
log = "0.4"
env_logger = "0.11"
page_size = "0.6"
Expand Down
23 changes: 15 additions & 8 deletions src/ps.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@ use crate::jobs;
use crate::nvidia;
use crate::procfs;
use crate::procfsapi;
use crate::util::three_places;
use crate::util::{csv_quote,three_places};

use csv::{Writer, WriterBuilder};
use std::collections::{HashMap, HashSet};
use std::env;
use std::io::{self, Result, Write};
Expand Down Expand Up @@ -424,9 +423,7 @@ fn do_create_snapshot(

// Once we start printing we'll print everything and not check the interrupted flag any more.

let mut writer = WriterBuilder::new()
.flexible(true)
.from_writer(io::stdout());
let mut writer = io::stdout();

let hostname = hostname::get().unwrap().into_string().unwrap();
const VERSION: &str = env!("CARGO_PKG_VERSION");
Expand Down Expand Up @@ -609,8 +606,8 @@ struct PrintParameters<'a> {
opts: &'a PsOptions<'a>,
}

fn print_record<W: io::Write>(
writer: &mut Writer<W>,
fn print_record(
writer: &mut dyn io::Write,
params: &PrintParameters,
proc_info: &ProcInfo,
) -> Result<bool> {
Expand Down Expand Up @@ -679,7 +676,17 @@ fn print_record<W: io::Write>(
if proc_info.rolledup > 0 {
fields.push(format!("rolledup={}", proc_info.rolledup));
}
writer.write_record(fields)?;

let mut s = "".to_string();
for f in fields {
if !s.is_empty() {
s += ","
}
s += &csv_quote(&f);
}
s += "\n";

writer.write(s.as_bytes())?;

Ok(true)
}
34 changes: 34 additions & 0 deletions src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,3 +71,37 @@ pub fn chunks(input: &str) -> (Vec<usize>, Vec<&str>) {
pub fn three_places(n: f64) -> f64 {
(n * 1000.0).round() / 1000.0
}

// If the value contains a , or " then quote the string, and double every "
pub fn csv_quote(s: &str) -> String {
let mut t = "".to_string();
let mut must_quote = false;
for c in s.chars() {
match c {
'"' => {
t.push(c);
t.push(c);
must_quote = true;
}
',' => {
t.push(c);
must_quote = true;
}
_ => {
t.push(c);
}
}
}
if must_quote {
t = "\"".to_string() + &t + "\""
}
t
}

#[test]
pub fn csv_quote_test() {
assert!(&csv_quote("abcde") == "abcde");
assert!(&csv_quote(r#"abc,de"#) == r#""abc,de""#);
assert!(&csv_quote(r#"abc"de"#) == r#""abc""de""#);
assert!(&csv_quote(r#"abc""de"#) == r#""abc""""de""#);
}
Loading