From 72a7b55c7fcd72f5b9cfde00ac8ef9f0995dd063 Mon Sep 17 00:00:00 2001 From: alemidev Date: Wed, 28 Dec 2022 03:53:37 +0100 Subject: [PATCH] chore: moved config in another file with pub field --- src/app.rs | 92 ++++++++++----------------------------------------- src/config.rs | 56 +++++++++++++++++++++++++++++++ src/main.rs | 26 +++++++++------ 3 files changed, 89 insertions(+), 85 deletions(-) create mode 100644 src/config.rs diff --git a/src/app.rs b/src/app.rs index cfd814e..3d2558a 100644 --- a/src/app.rs +++ b/src/app.rs @@ -1,67 +1,31 @@ -use tui::{style::Color, widgets::GraphType, symbols}; -// use crate::parser::SampleParser; +use std::{io::{self, ErrorKind}, time::{Duration, Instant}}; +use tui::{ + style::Color, widgets::GraphType, symbols, + backend::Backend, + widgets::{Block, Chart, Axis, Dataset}, + Terminal, text::Span, style::{Style, Modifier}, layout::Alignment +}; +use crossterm::event::{self, Event, KeyCode, KeyModifiers}; -pub enum Dimension { - X, Y -} - -#[derive(Default)] -pub struct ChartNames { - x: String, - y: String, -} +use libpulse_simple_binding::Simple; +use libpulse_binding::{stream::Direction, def::BufferAttr}; +use libpulse_binding::sample::{Spec, Format}; -pub struct ChartBounds { - x: [f64;2], - y: [f64;2], -} - -impl Default for ChartBounds { - fn default() -> Self { - ChartBounds { x: [0.0, 0.0], y: [0.0, 0.0] } - } -} - -pub struct ChartReferences { - pub x: Vec<(f64, f64)>, - pub y: Vec<(f64, f64)>, -} - -impl Default for ChartReferences { - fn default() -> Self { - ChartReferences { - x: vec![(0.0, 0.0), (0.0, 1.0)], - y: vec![(0.5, 1.0), (0.5, -1.0)] - } - } -} - -pub struct AppConfig { - pub title: String, - pub axis_color: Color, - pub palette: Vec, - - scale: u32, - width: u32, - vectorscope: bool, - pub references: bool, - pub triggering: bool, - - pub marker_type: symbols::Marker, - pub graph_type: GraphType, -} +use crate::Args; +use crate::config::{ChartNames, ChartBounds, ChartReferences, AppConfig, Dimension}; +use crate::parser::{SampleParser, Signed16PCM}; pub struct App { pub cfg: AppConfig, pub references: ChartReferences, - bounds: ChartBounds, - names: ChartNames, + pub bounds: ChartBounds, + pub names: ChartNames, } impl App { - fn update_values(&mut self) { + pub fn update_values(&mut self) { if self.cfg.vectorscope { self.names.x = "left -".into(); self.names.y = "| right".into(); @@ -94,18 +58,6 @@ impl App { } } - pub fn vectorscope(&self) -> bool { - self.cfg.vectorscope - } - - pub fn scale(&self) -> u32 { - self.cfg.scale - } - - pub fn width(&self) -> u32 { - self.cfg.width - } - pub fn scatter(&self) -> bool { match self.cfg.graph_type { GraphType::Scatter => true, @@ -130,15 +82,6 @@ impl App { // ] // } - pub fn graph_type(&self) -> GraphType { - self.cfg.graph_type - } - - pub fn set_vectorscope(&mut self, vectorscope: bool) { - self.cfg.vectorscope = vectorscope; - self.update_values(); - } - pub fn update_scale(&mut self, increment: i32) { if increment > 0 || increment.abs() < self.cfg.scale as i32 { self.cfg.scale = ((self.cfg.scale as i32) + increment) as u32; @@ -163,6 +106,7 @@ impl From::<&crate::Args> for App { scale: args.range, width: args.buffer / 4, // TODO It's 4 because 2 channels and 2 bytes per sample! triggering: args.triggering, + threshold: args.threshold, vectorscope: args.vectorscope, references: !args.no_reference, marker_type, graph_type, diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..396a7ea --- /dev/null +++ b/src/config.rs @@ -0,0 +1,56 @@ +use tui::{style::Color, widgets::GraphType, symbols}; + +// use crate::parser::SampleParser; + +pub enum Dimension { + X, Y +} + +#[derive(Default)] +pub struct ChartNames { + pub x: String, + pub y: String, +} + + +pub struct ChartBounds { + pub x: [f64;2], + pub y: [f64;2], +} + +impl Default for ChartBounds { + fn default() -> Self { + ChartBounds { x: [0.0, 0.0], y: [0.0, 0.0] } + } +} + +pub struct ChartReferences { + pub x: Vec<(f64, f64)>, + pub y: Vec<(f64, f64)>, +} + +impl Default for ChartReferences { + fn default() -> Self { + ChartReferences { + x: vec![(0.0, 0.0), (0.0, 1.0)], + y: vec![(0.5, 1.0), (0.5, -1.0)] + } + } +} + +pub struct AppConfig { + pub title: String, + pub axis_color: Color, + pub palette: Vec, + + pub scale: u32, + pub width: u32, + pub vectorscope: bool, + pub references: bool, + + pub triggering: bool, + pub threshold: f64, + + pub marker_type: symbols::Marker, + pub graph_type: GraphType, +} diff --git a/src/main.rs b/src/main.rs index f16f59b..0fc7916 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ mod parser; mod app; +mod config; mod music; use std::{io::{self, ErrorKind}, time::{Duration, Instant}}; @@ -25,6 +26,7 @@ use parser::{SampleParser, Signed16PCM}; use crate::app::App; use crate::music::Note; +use crate::config::Dimension; /// A simple oscilloscope/vectorscope for your terminal #[derive(Parser, Debug)] @@ -194,16 +196,17 @@ fn run_app(args: Args, terminal: &mut Terminal) -> Result<(), io let mut measures; - if app.vectorscope() { + if app.cfg.vectorscope { measures = vec![]; for chunk in channels.chunks(2) { let mut tmp = vec![]; for i in 0..chunk[0].len() { tmp.push((chunk[0][i] as f64, chunk[1][i] as f64)); } + // split it in two so the math downwards still works the same let pivot = tmp.len() / 2; + measures.push(tmp[pivot..].to_vec()); // put more recent first measures.push(tmp[..pivot].to_vec()); - measures.push(tmp[pivot..].to_vec()); } } else { measures = vec![vec![]; channels.len()]; @@ -221,11 +224,11 @@ fn run_app(args: Args, terminal: &mut Terminal) -> Result<(), io datasets.push(data_set("", &app.references.y, app.cfg.marker_type, GraphType::Line, app.cfg.axis_color)); } - let ds_names = if app.vectorscope() { vec!["2", "1"] } else { vec!["R", "L"] }; + let ds_names = if app.cfg.vectorscope { vec!["1", "2"] } else { vec!["R", "L"] }; let palette : Vec = app.cfg.palette.iter().rev().map(|x| x.clone()).collect(); for (i, ds) in measures.iter().rev().enumerate() { - datasets.push(data_set(ds_names[i], ds, app.cfg.marker_type, app.graph_type(), palette[i % palette.len()])); + datasets.push(data_set(ds_names[i], ds, app.cfg.marker_type, app.cfg.graph_type, palette[i % palette.len()])); } fps += 1; @@ -240,8 +243,8 @@ fn run_app(args: Args, terminal: &mut Terminal) -> Result<(), io let size = f.size(); let chart = Chart::new(datasets) .block(block(&app, args.sample_rate as f32, framerate)) - .x_axis(axis(&app, app::Dimension::X)) // TODO allow to have axis sometimes? - .y_axis(axis(&app, app::Dimension::Y)); + .x_axis(axis(&app, Dimension::X)) // TODO allow to have axis sometimes? + .y_axis(axis(&app, Dimension::Y)); f.render_widget(chart, size) })?; @@ -261,8 +264,8 @@ fn run_app(args: Args, terminal: &mut Terminal) -> Result<(), io KeyCode::Char('-') => app.update_scale(1000), KeyCode::Char('+') => app.update_scale(-100), KeyCode::Char('_') => app.update_scale(100), - KeyCode::Char('v') => app.set_vectorscope(!app.vectorscope()), - KeyCode::Char('s') => app.set_scatter(!app.scatter()), + KeyCode::Char('v') => app.cfg.vectorscope = !app.cfg.vectorscope, + KeyCode::Char('s') => app.set_scatter(!app.scatter()), // TODO no funcs KeyCode::Char('h') => app.cfg.references = !app.cfg.references, KeyCode::Char('t') => app.cfg.triggering = !app.cfg.triggering, KeyCode::Up => {}, @@ -271,6 +274,7 @@ fn run_app(args: Args, terminal: &mut Terminal) -> Result<(), io } } } + app.update_values(); } } @@ -303,7 +307,7 @@ fn data_set<'a>( .data(&data) } -fn axis(app: &App, dim: app::Dimension) -> Axis { +fn axis(app: &App, dim: Dimension) -> Axis { let mut a = Axis::default(); if app.cfg.references { a = a.title(Span::styled(app.name(&dim), Style::default().fg(Color::Cyan))); @@ -320,10 +324,10 @@ fn block(app: &App, sample_rate: f32, framerate: u32) -> Block { Span::styled( format!( "TUI {} -- {}{} mode -- range {} -- {} samples -- {:.1} kHz -- {} fps", - if app.vectorscope() { "Vectorscope" } else { "Oscilloscope" }, + if app.cfg.vectorscope { "Vectorscope" } else { "Oscilloscope" }, if app.cfg.triggering { "triggered " } else { "" }, if app.scatter() { "scatter" } else { "line" }, - app.scale(), app.width(), sample_rate / 1000.0, framerate, + app.cfg.scale, app.cfg.width, sample_rate / 1000.0, framerate, ), Style::default().add_modifier(Modifier::BOLD).fg(Color::Yellow)) ).title_alignment(Alignment::Center);