Lines
91.45 %
Functions
53.13 %
Branches
100 %
//! The category report as CSV: fetched with `x`, shown read-only, copied by OSC 52 or saved locally.
use std::fs::{self, OpenOptions};
use std::io::{self, ErrorKind, Write};
use std::path::{Path, PathBuf};
use cli_core::render::{WireValue, parse_wire};
use cli_core::report_form::csv_call;
use forms::FormValues;
use scripting::nomiscript::Value;
use super::App;
use crate::clipboard::osc52;
use crate::form::Form;
use crate::modal::{ConfirmAction, Csv, Modal};
use crate::route::RouteCtx;
use crate::tabs::reports::{ReportKind, ReportsTab};
use crate::view::ViewId;
use crate::window::WindowId;
impl App {
/// Ask for the CSV of the shown category report.
pub fn request_report_csv(&mut self) {
let Some(window) = self.front_or_say(ViewId::Reports) else {
return;
};
let Some((kind, values)) = self.last_report(window) else {
self.set_status("no report run yet");
if kind != ReportKind::Breakdown {
self.set_status("CSV is only for the category report");
}
let Some(seq) = self
.win_mut::<ReportsTab>(window)
.map(|tab| tab.csv_seq.advance())
else {
let ctx = RouteCtx::ReportCsv {
seq,
file_name: default_file_name(&values),
if self
.dispatch_eval(window, ctx, csv_call(kind.descriptor(), &values))
.is_none()
{
self.set_status("console not connected");
pub(super) fn deliver_report_csv(
&mut self,
window: WindowId,
seq: u64,
file_name: String,
wire: &str,
) {
if !self
.win::<ReportsTab>(window)
.is_some_and(|tab| tab.csv_seq.awaits(seq))
match parse_wire(wire) {
Ok(WireValue::Value(Value::String(text))) if self.dialog_free("CSV") => {
self.open_dialog_in(window, Modal::Csv(Csv::new(file_name, text)));
Ok(WireValue::Value(Value::String(_))) => {}
Ok(WireValue::Value(_)) => self.set_status("the CSV reply is not text"),
Err(e) => self.set_status(format!("CSV failed: {e}")),
/// Queue the shown CSV for the clipboard of the terminal.
pub fn copy_csv(&mut self) {
let Some(Modal::Csv(csv)) = self.overlays.top() else {
match osc52(&csv.text) {
Ok(bytes) => {
self.outbound.push(bytes);
self.set_status("CSV copied to the clipboard");
Err(e) => self.set_status(e.to_string()),
/// The raw terminal output queued since the last call.
pub fn take_outbound(&mut self) -> Vec<Vec<u8>> {
std::mem::take(&mut self.outbound)
/// Open the save-path prompt; saving files is for the local transport only.
pub fn begin_csv_save(&mut self) {
if !self.local_files {
self.set_status("save is not available over ssh");
let form = Form::csv_save(&csv.file_name);
self.overlays.push(Modal::Form(form));
pub(crate) fn save_csv(&mut self, values: &FormValues) {
match values
.text("path")
.map(str::trim)
.filter(|path| !path.is_empty())
Some(path) => self.write_csv(PathBuf::from(path), false),
None => self.set_status("no file name given"),
pub(crate) fn overwrite_csv(&mut self, path: PathBuf) {
self.write_csv(path, true);
fn write_csv(&mut self, path: PathBuf, overwrite: bool) {
match write_file(&path, &csv.text, overwrite) {
Ok(()) => self.set_status(format!("saved {}", path.display())),
Err(e) if e.kind() == ErrorKind::AlreadyExists => {
let prompt = format!("Overwrite {}?", path.display());
self.overlays
.push(Modal::confirm(prompt, ConfirmAction::OverwriteCsv(path)));
Err(e) => self.set_status(format!("save failed: {e}")),
fn write_file(path: &Path, text: &str, overwrite: bool) -> io::Result<()> {
if !overwrite {
return OpenOptions::new()
.write(true)
.create_new(true)
.open(path)?
.write_all(text.as_bytes());
let name = path.file_name().unwrap_or_default().to_string_lossy();
let staged = path.with_file_name(format!(".{name}.tmp-{}", std::process::id()));
let result = fs::write(&staged, text).and_then(|()| fs::rename(&staged, path));
if result.is_err() {
let _ = fs::remove_file(&staged);
result
fn default_file_name(values: &FormValues) -> String {
let part = |key, fallback| {
values
.text(key)
.filter(|text| !text.is_empty())
.unwrap_or(fallback)
.to_string()
format!(
"category-{}-{}.csv",
part("from", "start"),
part("to", "end")
)