Skip to content

Commit

Permalink
island data parse set to build time
Browse files Browse the repository at this point in the history
- made structs for more readability
- formatted code with whitespace
- changed a bunch of usize to u32
  • Loading branch information
Quicksilver151 committed Jan 27, 2023
1 parent 3568834 commit ca9419c
Show file tree
Hide file tree
Showing 3 changed files with 74 additions and 56 deletions.
50 changes: 24 additions & 26 deletions src/functions/edit.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,21 @@
use crate::*;

// pub static ATOLLS_DAT: &str = include_str!("../data/atolls.csv");
pub static ISLAND_DAT: &str = include_str!("../data/islands.csv");

pub fn edit() {
// start new buffer
print!("\x1b[?1049h");
println!("EDIT MODE\n changes are made to the config file\n");

let atoll_data : Vec<AtollData> = ATOLL_DATA.iter().map(|x| AtollData::new_from_array(*x)).collect();
let raw_island_data: Vec<String> = get_vec_from_db(ISLAND_DAT);

// [row][column: 0,1,2] (0 = atoll_index, 1=name, 2=dhi_name)
// let atoll_data: Vec<Vec<&str>> = raw_atoll_data
// .iter()
// .map(|x| x.split(';').collect())
// .collect();

// [row][coloumn: 0,2,3,4] (0 = time index, 2=atoll, 3=name, 4=dhi_name)
let island_data: Vec<Vec<&str>> = raw_island_data
let atoll_data : Vec<AtollData> = ATOLL_DATA
.iter()
.map(|x| AtollData::new_from_array(*x))
.collect();
let island_data: Vec<IslandData> = ISLAND_DATA
.iter()
.map(|x| x.split(';').collect())
.map(|x| IslandData::new_from_array(*x))
.collect();


clear_screen();
// atoll title
// println!("Index\tName\tDhiName");
Expand All @@ -36,10 +29,11 @@ pub fn edit() {
atoll_data
.iter()
.for_each(|atoll| println!("{0: <5} | {1: <10} | {2: <10}", atoll.index, atoll.en_code, atoll.dh_code));
println!("Input a number from the first colum to select Atoll(1-20) or select a timeset(42-82):");
let selected_atoll_index: usize =
get_number_input().expect("Must be a non zero positive integer");
let selected_time_index: usize;

println!("\nInput a number from the first colum to select Atoll(1-20) or select a timeset(42-82):");

let selected_atoll_index: u32 = get_number_input().expect("Must be a non zero positive integer");
let selected_time_index : u32;

if std::ops::RangeInclusive::new(1, 20).contains(&selected_atoll_index) {
clear_screen();
Expand All @@ -49,26 +43,30 @@ pub fn edit() {
"Index", "Timeset", "Island Name", "Dhi Name"
);
println!("-------------------------------------------");

let mut i = 0;
let mut selectables: Vec<usize> = vec![];
let mut selectables: Vec<u32> = vec![];

// print island list
for island in island_data.iter() {
if island[2].parse::<usize>().unwrap_or(1) == selected_atoll_index {
if island.atoll == selected_atoll_index{
i += 1;
selectables.append(&mut vec![island[0].parse::<usize>().unwrap_or(41)]);
selectables.append(&mut vec![island.timeset]);
println!(
"{0: <5} | {1: <7} | {2: <15} | {3: <10}",
i, island[0], island[3], island[4]
i, island.timeset, island.en_name, island.dh_name
);
}
}

println!("Input a number from the first column to select prefered timeset:");
selected_time_index = selectables[get_number_input().unwrap()];
} else if std::ops::RangeInclusive::new(41, 82).contains(&selected_atoll_index) {
println!("\nInput a number from the first column to select your island:");
selected_time_index = selectables[get_number_input().unwrap() as usize];

}
else if std::ops::RangeInclusive::new(41, 82).contains(&selected_atoll_index) {
selected_time_index = selected_atoll_index;
} else {
}
else {
println!("\x1b[?1049l");

panic!("value not within range");
Expand Down
4 changes: 2 additions & 2 deletions src/functions/extras.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,14 @@ pub fn get_current_time(format: &TimeFormat) -> (u32, u32, u32, String) {
}

// input management
pub fn get_number_input() -> Result<usize, std::num::ParseIntError> {
pub fn get_number_input() -> Result<u32, std::num::ParseIntError> {
let mut input_text = String::new();
std::io::stdin()
.read_line(&mut input_text)
.expect("failed to read from stdin");

let trimmed = input_text.trim();
trimmed.parse::<usize>()
trimmed.parse::<u32>()
}

// get db data
Expand Down
76 changes: 48 additions & 28 deletions src/structs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,16 @@ use crate::*;

#[derive(Default, Debug, Serialize, Deserialize)]
pub struct Config {
pub island_index: usize,
pub island_index: u32,
pub island_name: String,
}

#[derive(Serialize,Deserialize)]
pub struct AtollData{
pub index: u32,
pub en_code: String,
pub dh_code: String,
pub ar_code: String,
}

impl AtollData{
pub fn new_from_array(data: [&str;4]) -> AtollData{
let new_atoll_data: AtollData = AtollData {
Expand All @@ -27,23 +25,45 @@ impl AtollData{
}
}

pub struct IslandData{
pub timeset: u32,
pub index: u32,
pub atoll: u32,
pub en_name: String,
pub dh_name: String,
pub ar_name: String,
}
impl IslandData{
pub fn new_from_array(data: [&str;10]) -> IslandData{
let new_island_data: IslandData = IslandData {
timeset: data[0].parse::<u32>().unwrap(),
index: data[1].parse::<u32>().unwrap(),
atoll: data[2].parse::<u32>().unwrap(),
en_name: data[3].to_string(),
dh_name: data[4].to_string(),
ar_name: data[5].to_string(),
};

new_island_data

}

}


#[derive(Debug)]
pub struct PrayerData {
pub island_index: u32,
pub day: u32,
pub fajr: u32,
pub sun: u32,
pub day: u32,
pub fajr: u32,
pub sun: u32,
pub dhuhur: u32,
pub asr: u32,
pub asr: u32,
pub magrib: u32,
pub isha: u32,
pub isha: u32,
}

impl PrayerData {
// fn new() -> PrayerData{
// PrayerData { island_index: 0, day: 0, fajr: 0, sun: 0, dhuhur: 0, asr: 0, magrib: 0, isha: 0}
// }


pub fn new_from_array(val: &[u32; 8]) -> PrayerData {
PrayerData {
island_index: val[0],
Expand All @@ -56,7 +76,7 @@ impl PrayerData {
isha: val[7],
}
}

pub fn vec_from_island_set(&self) -> Vec<i32> {
let mut val = vec![0; 6];
val[0] = self.fajr as i32;
Expand All @@ -65,20 +85,20 @@ impl PrayerData {
val[3] = self.asr as i32;
val[4] = self.magrib as i32;
val[5] = self.isha as i32;

val
}

pub fn flag_formatted_output(&self, flag: &Flag) {
let (flag, pt_vec) = (flag, self.vec_from_island_set());

// some temporary inits
let names = vec!["Fajr", "Sun", "Dhuhur", "Asr", "Magrib", "Isha"];

// optional title
if flag.title && !flag.tui && !flag.edit {
let (hour, minute, second, time) = get_current_time(&flag.time);

println!("Salat_MV-cli");
println!("---------------------");
println!(
Expand All @@ -92,7 +112,7 @@ impl PrayerData {
println!("---------------------");
println!();
}

for (i, pt) in pt_vec.iter().enumerate() {
match flag.disp {
// only numbers or with info
Expand Down Expand Up @@ -165,7 +185,7 @@ impl TimeConversion for i32 {
self.to_string()
}
}

fn to_12(self) -> (u32, String) {
let half = if self > 11 { "pm" } else { "am" };
if self > 12 {
Expand All @@ -174,17 +194,17 @@ impl TimeConversion for i32 {
(self as u32, half.to_string())
}
}

fn minutes_to_time(self, time_format: &TimeFormat) -> String {
let minute = &self % 60;
let mut hour = self as u32 / 60;
let mut period = "".to_string();

match time_format {
TimeFormat::TWHour => (hour, period) = hour.to_12(),
TimeFormat::TFHour => {}
}

let hour = hour.add_zero();
let minute = minute.add_zero();
format!("{}:{} {}", hour, minute, period)
Expand All @@ -201,7 +221,7 @@ impl TimeConversion for u32 {
self.to_string()
}
}

fn to_12(self) -> (u32, String) {
let half = if self > 11 { "pm" } else { "am" };
if self > 12 {
Expand All @@ -210,17 +230,17 @@ impl TimeConversion for u32 {
(self, half.to_string())
}
}

fn minutes_to_time(self, time_format: &TimeFormat) -> String {
let minute = &self % 60;
let mut hour = self / 60;
let mut period = "".to_string();

match time_format {
TimeFormat::TWHour => (hour, period) = hour.to_12(),
TimeFormat::TFHour => {}
}

let hour = hour.add_zero();
let minute = minute.add_zero();
format!("{}:{} {}", hour, minute, period)
Expand Down

0 comments on commit ca9419c

Please sign in to comment.