common: Include the recordings' size in the listing.
[gps-watch.git] / src / common / logger.rs
index 022b155f7d1b90b9667d5291dd4b8da9115b5dbb..1c982afa96163cb68714d491c10a3f0d3217593f 100644 (file)
@@ -144,10 +144,22 @@ fn cmp_sector_header_indices(a: u16, b: u16,
     let header_a = &sector_header[a as usize];
     let header_b = &sector_header[b as usize];
 
-    // Latest entries come first.
-    if header_a.start_time > header_b.start_time {
+    if header_a.starts_recording() && header_b.starts_recording() {
+        // Latest entries come first.
+        if header_a.start_time > header_b.start_time {
+            -1
+        } else if header_a.start_time < header_b.start_time {
+            1
+        } else {
+            0
+        }
+    } else if header_a.starts_recording() {
+        -1
+    } else if header_b.starts_recording() {
+        1
+    } else if a < b {
         -1
-    } else if header_a.start_time < header_b.start_time {
+    } else if a > b {
         1
     } else {
         0
@@ -187,56 +199,11 @@ impl<'a> SectorHeaderIter<'a> {
             indices: [0; NUM_SECTORS]
         };
 
-        let mut num_used = 0;
-
-        // Put the indices of the used directory entries at the beginning
-        // of the array. Ignore the unused ones since we are not going
-        // to sort them anyway.
         for i in 0..NUM_SECTORS {
-            let sector_header = &iter.sector_header[i];
-
-            if sector_header.starts_recording() {
-                iter.indices[num_used] = i as u16;
-                num_used += 1;
-            }
+            iter.indices[i] = i as u16;
         }
 
-        let num_elts_to_sort = num_used;
-
-        if num_elts_to_sort != 0 {
-            // Sort the used directory entries.
-            iter.sort(num_elts_to_sort);
-        }
-
-        // Now put the indices of the unused directory entries in the array.
-        if num_used == 0 {
-            for i in 0..NUM_SECTORS {
-                iter.indices[i] = i as u16;
-            }
-        } else {
-            let latest_used = iter.indices[0] as usize;
-            let mut offset_unused = num_used;
-
-            // First put the entries that come after the latest one in use...
-            for i in (latest_used + 1)..NUM_SECTORS {
-                let sector_header = &iter.sector_header[i];
-
-                if !sector_header.is_in_use() {
-                    iter.indices[offset_unused] = i as u16;
-                    offset_unused += 1;
-                }
-            }
-
-            // ... then wrap around if necessary.
-            for i in 0..latest_used {
-                let sector_header = &iter.sector_header[i];
-
-                if !sector_header.is_in_use() {
-                    iter.indices[offset_unused] = i as u16;
-                    offset_unused += 1;
-                }
-            }
-        }
+        iter.sort(NUM_SECTORS);
 
         // XXX:
         // Need to handle those sectors that don't start recordings
@@ -252,9 +219,7 @@ impl<'a> SectorHeaderIter<'a> {
         }
 
         for i in (1..num_elts_to_sort).rev() {
-            let t = self.indices[0];
-            self.indices[0] = self.indices[i];
-            self.indices[i] = t;
+            self.indices.swap(0, i);
 
             downheap(&mut self.indices, 0, i - 1, self.sector_header);
         }
@@ -589,6 +554,17 @@ impl<'a> Logger<'a> {
                 continue;
             }
 
+            let mut num_data_sectors = 0;
+
+            for d in 1..NUM_SECTORS {
+                let wrapped_index = ((index + d) & (NUM_SECTORS - 1)) as usize;
+                let other_sector_header = &self.sector_header[wrapped_index];
+
+                if other_sector_header.belongs_to(sector_header.recording_id) {
+                    num_data_sectors += 1;
+                }
+            }
+
             let mut date_time_s = [b' '; 19];
 
             if let Some(tm) = Time::from_unix_time(sector_header.start_time) {
@@ -596,6 +572,13 @@ impl<'a> Logger<'a> {
                 tm.fmt_time(&mut date_time_s[11..]);
             }
 
+            let recording_size = (num_data_sectors + 1) * (SECTOR_SIZE >> 10);
+
+            let mut recording_size_s = [b'0'; 9];
+            let recording_size_s_len = fmt_u32_pad(&mut recording_size_s,
+                                                   recording_size as u32,
+                                                   8, b' ');
+
             let mut recording_id_s = [b'0'; 9];
             let recording_id_s_len =
                 fmt_u32_pad(&mut recording_id_s,
@@ -603,7 +586,8 @@ impl<'a> Logger<'a> {
                             8, b' ');
 
             tx_buf.write(&date_time_s);
-            tx_buf.write(b"       ");
+            tx_buf.write(&recording_size_s[0..recording_size_s_len]);
+            tx_buf.write(b"K");
             tx_buf.write(&recording_id_s[0..recording_id_s_len]);
             tx_buf.write(b"\n");