Skip to content

Commit

Permalink
Add pre-commit formatting check script, run it on existing files.
Browse files Browse the repository at this point in the history
  • Loading branch information
gilbertoabram committed Dec 9, 2024
1 parent dbef689 commit cab58a8
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 37 deletions.
17 changes: 17 additions & 0 deletions .githooks/pre-commit
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#!/bin/sh

# stash anything that we are not committing now.
git stash --keep-index --include-untracked

if cargo fmt --check --verbose; then
echo "Formatting OK"
ret = 0
else
echo "Code formatting issues. Please run 'cargo fmt' before commit"
ret = 1
fi

# restore other modified files from stash
git stash pop

exit $ret
31 changes: 16 additions & 15 deletions twinleaf-tools/src/bin/tio-monitor.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,12 @@
use twinleaf::tio;
use twinleaf::data::{ColumnData, Device};
use tio::proto::DeviceRoute;
use tio::proxy;
use tio::util;
use twinleaf::data::{ColumnData, Device};
use twinleaf::tio;

use getopts::Options;
use std::env;
use pancurses::*;

use std::env;

fn tio_opts() -> Options {
let mut opts = Options::new();
Expand Down Expand Up @@ -59,14 +58,17 @@ fn run_monitor(args: &[String]) {
window.refresh();
noecho();
let mut y_position = 3;
loop{

loop {
let sample = device.next();

let name = format!("Device Name: {} Serial: {} Session ID: {}", sample.device.name, sample.device.serial_number, sample.device.session_id);
window.mvprintw(1,0, &name);
let name = format!(
"Device Name: {} Serial: {} Session ID: {}",
sample.device.name, sample.device.serial_number, sample.device.session_id
);
window.mvprintw(1, 0, &name);

for col in &sample.columns{
for col in &sample.columns {
let string = format!(
" {}: {}",
col.desc.name,
Expand All @@ -76,22 +78,21 @@ fn run_monitor(args: &[String]) {
ColumnData::Float(x) => format!("{:.3}", x),
ColumnData::Unknown => "?".to_string(),
}
);
);

if sample.stream.stream_id == 0x02 {
y_position += 1;
}

window.mvprintw(y_position, 0, &string);
window.mvprintw(y_position, 0, &string);
window.refresh();
}
y_position = 3;
}
}

fn main(){
fn main() {
let args: Vec<String> = env::args().collect();

run_monitor(&args);
}

45 changes: 23 additions & 22 deletions twinleaf-tools/src/bin/tio-tool.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use tio::proto::DeviceRoute;
use tio::proxy;
use tio::util;
use twinleaf::data::{ColumnData, DeviceDataParser};
use twinleaf::tio;
use twinleaf::data::{DeviceDataParser, ColumnData};

use std::env;
use std::fs::File;
use std::io::prelude::*;
use std::fs::OpenOptions;
use std::io::prelude::*;

use getopts::Options;

Expand Down Expand Up @@ -435,7 +435,7 @@ fn log_data_dump(args: &[String]) {
}
}

//match
//match
fn match_value(data: ColumnData) -> String {
let data_type = match data {
ColumnData::Int(x) => format!("{}", x),
Expand All @@ -452,8 +452,8 @@ fn log_csv(args: &[String]) -> std::io::Result<()> {
let output_name = args.get(3).unwrap_or(&args[2]);

let s = output_name.replace("csv", "");
let path= format!("{}{}.csv", s, &args[1]).to_string();
let path = format!("{}{}.csv", s, &args[1]).to_string();

let mut file = OpenOptions::new().append(true).create(true).open(path)?;
let mut streamhead: bool = false;
let mut first: bool = true;
Expand All @@ -470,44 +470,46 @@ fn log_csv(args: &[String]) -> std::io::Result<()> {
for col in &sample.columns {
let time = format!("{:.6}", sample.timestamp_end());
let value = match_value(col.value.clone());

//write in column names
if !streamhead{
if !streamhead {
let timehead = format!("{},", "time");
let _= file.write_all(timehead.as_bytes());
let _ = file.write_all(timehead.as_bytes());

for col in &sample.columns {
let mut header = format!("{},", col.desc.name);

if col.desc.name == sample.columns[&sample.columns.len() -1].desc.name.clone() {

if col.desc.name
== sample.columns[&sample.columns.len() - 1].desc.name.clone()
{
header = format!("{}", col.desc.name);
}

file.write_all(header.as_bytes())?;
}
file.write_all(b"\n")?;
streamhead = true;
}

//write in data
let timefmt = format!("{},", time);
let mut formatted_value = format!("{},", value );
if first{
let _= file.write_all(timefmt.as_bytes());
let mut formatted_value = format!("{},", value);
if first {
let _ = file.write_all(timefmt.as_bytes());
first = false;
}

if value == match_value(sample.columns[&sample.columns.len() - 1].value.clone()) {
if value
== match_value(sample.columns[&sample.columns.len() - 1].value.clone())
{
formatted_value = format!("{}", value);
}

file.write_all(formatted_value.as_bytes())?;

}
file.write_all(b"\n")?;
first = true;
}

}
}
}
Expand Down Expand Up @@ -587,8 +589,7 @@ fn main() {
log_data_dump(&args[2..]); //.unwrap();
}
"log-csv" => {

let _= log_csv(&args[1..]); //.unwrap();
let _ = log_csv(&args[1..]); //.unwrap();
}
"firmware-upgrade" => {
firmware_upgrade(&args[2..]); //.unwrap();
Expand Down

0 comments on commit cab58a8

Please sign in to comment.