Skip to content

Commit

Permalink
refactor(object): make object name and COSPAR ID optional
Browse files Browse the repository at this point in the history
- Change Object.name and Object.cospar_id to Option<String>
- Update Object::from_elements to handle potential missing values
- Modify ObjectInformation and WorldMap widgets to display "Unknown" when name or COSPAR ID is unavailable
  • Loading branch information
ShenMian committed Jan 4, 2025
1 parent 87c2952 commit 958f327
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 20 deletions.
25 changes: 11 additions & 14 deletions src/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ use chrono::{DateTime, Datelike, Timelike, Utc};
#[derive(Clone, Debug)]
pub struct Object {
/// The name of the object.
name: String,
name: Option<String>,
/// The COSPAR ID of the object.
cospar_id: String,
cospar_id: Option<String>,
/// The NORAD ID of the object.
norad_id: u64,

Expand Down Expand Up @@ -36,14 +36,8 @@ pub struct Object {
impl Object {
pub fn from_elements(elements: sgp4::Elements) -> Self {
Self {
name: elements
.object_name
.clone()
.unwrap_or("Unknown".to_string()),
cospar_id: elements
.international_designator
.clone()
.unwrap_or("Unknown".to_string()),
name: elements.object_name.clone(),
cospar_id: elements.international_designator.clone(),
norad_id: elements.norad_id,
epoch: DateTime::from_naive_utc_and_offset(elements.datetime, Utc),
drag_term: elements.drag_term,
Expand All @@ -58,14 +52,17 @@ impl Object {
}
}

pub fn name(&self) -> &String {
&self.name
/// The name of the object.
pub fn name(&self) -> Option<&String> {
self.name.as_ref()
}

pub fn cospar_id(&self) -> &String {
&self.cospar_id
/// The COSPAR ID of the object.
pub fn cospar_id(&self) -> Option<&String> {
self.cospar_id.as_ref()
}

/// The COSPAR ID of the object.
pub fn norad_id(&self) -> u64 {
self.norad_id
}
Expand Down
15 changes: 13 additions & 2 deletions src/widgets/object_information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,8 @@ impl ObjectInformation<'_> {
}

fn render_table(&self, buf: &mut Buffer, state: &mut ObjectInformationState, index: usize) {
const UNKNOWN_NAME: &str = "Unknown";

let object = &self.satellites_state.objects[index];
let object_state = object.predict(Utc::now()).unwrap();

Expand All @@ -76,8 +78,17 @@ impl ObjectInformation<'_> {
.name();

state.items = Vec::from([
("Name", object.name().clone()),
("COSPAR ID", object.cospar_id().clone()),
(
"Name",
object.name().unwrap_or(&UNKNOWN_NAME.to_string()).clone(),
),
(
"COSPAR ID",
object
.cospar_id()
.unwrap_or(&UNKNOWN_NAME.to_string())
.clone(),
),
("NORAD ID", object.norad_id().to_string()),
("Longitude", format!("{:9.4}°", object_state.longitude())),
("Latitude", format!("{:9.4}°", object_state.latitude())),
Expand Down
28 changes: 24 additions & 4 deletions src/widgets/world_map.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ impl WorldMap<'_> {
const MAP_COLOR: Color = Color::Gray;
const TRAJECTORY_COLOR: Color = Color::LightBlue;
const SATELLIT_SYMBOL: &'static str = "+";
const UNKNOWN_NAME: &'static str = "UNK";

fn render_block(&self, area: Rect, buf: &mut Buffer, state: &mut WorldMapState) {
let block = Block::bordered().title("World map".blue());
Expand All @@ -49,9 +50,19 @@ impl WorldMap<'_> {
// Draw satellites
for object in self.satellites_state.objects.iter() {
let line = if state.selected_object.is_none() {
Self::SATELLIT_SYMBOL.light_red() + format!(" {}", object.name()).white()
Self::SATELLIT_SYMBOL.light_red()
+ format!(
" {}",
object.name().unwrap_or(&Self::UNKNOWN_NAME.to_string())
)
.white()
} else {
Self::SATELLIT_SYMBOL.red() + format!(" {}", object.name()).dark_gray()
Self::SATELLIT_SYMBOL.red()
+ format!(
" {}",
object.name().unwrap_or(&Self::UNKNOWN_NAME.to_string())
)
.dark_gray()
};
let state = object.predict(Utc::now()).unwrap();
ctx.print(state.position[0], state.position[1], line);
Expand Down Expand Up @@ -101,7 +112,11 @@ impl WorldMap<'_> {
state.position[0],
state.position[1],
Self::SATELLIT_SYMBOL.light_green().slow_blink()
+ format!(" {}", selected.name()).white(),
+ format!(
" {}",
selected.name().unwrap_or(&Self::UNKNOWN_NAME.to_string())
)
.white(),
);
} else if let Some(hovered_object_index) = state.hovered_object {
let hovered = &self.satellites_state.objects[hovered_object_index];
Expand All @@ -113,7 +128,12 @@ impl WorldMap<'_> {
state.position[1],
Self::SATELLIT_SYMBOL.light_red().reversed()
+ " ".into()
+ hovered.name().clone().white().reversed(),
+ hovered
.name()
.unwrap_or(&Self::UNKNOWN_NAME.to_string())
.clone()
.white()
.reversed(),
);
}
})
Expand Down

0 comments on commit 958f327

Please sign in to comment.