| 1 | //! Terminal-native underwater field for the Codewhale transcript. |
| 2 | //! |
| 3 | //! The field is atmosphere, never content: ordinary shell cells share its |
| 4 | //! water column while semantic surfaces such as selections, errors, and code |
| 5 | //! keep their own backgrounds. It belongs to the `underwater` theme alone |
| 6 | //! (`ThemeId::Underwater`); every other theme leaves the terminal's ground |
| 7 | //! untouched. Motion inside the field remains governed separately by |
| 8 | //! `low_motion`/`fancy_animations`. |
| 9 | |
| 10 | mod launch; |
| 11 | |
| 12 | use std::time::Duration; |
| 13 | |
| 14 | use codewhale_ratatui::{ |
| 15 | MotionMode, OceanColumn as NativeColumn, OceanPhase, OceanRamp as NativeRamp, |
| 16 | }; |
| 17 | use ratatui::{buffer::Buffer, layout::Rect, style::Color}; |
| 18 | |
| 19 | use crate::tui::underwater::ShellPhase; |
| 20 | use codewhale_palette::UiTheme; |
| 21 | |
| 22 | /// Minimum empty-water size that earns decorative ambient life when the |
| 23 | /// underwater theme is selected. Below this, content and controls own |
| 24 | /// every cell. Shared by the renderer and idle animation scheduler so redraws |
| 25 | /// are never scheduled for invisible life. |
| 26 | pub const AMBIENT_MIN_WIDTH: u16 = 40; |
| 27 | pub const AMBIENT_MIN_HEIGHT: u16 = 10; |
| 28 | pub const AMBIENT_IDLE_PRESENCE: f32 = 0.85; |
| 29 | pub const AMBIENT_LIFE_INTERVAL_MS: u64 = 250; |
| 30 | |
| 31 | /// Ambient-life ink pair, independent of the Deepsea ramp and shaped by what |
| 32 | /// the agent is doing so the marks themselves carry the state at a glance: |
| 33 | /// reasoning dims toward the deep, tool work brightens like a faster current, |
| 34 | /// and a sub-agent pod swims in seafoam — the hue reserved for orchestration. |
| 35 | #[must_use] |
| 36 | pub fn ambient_inks_for_activity( |
| 37 | theme: &UiTheme, |
| 38 | activity: crate::tui::ambient_life::AmbientActivity, |
| 39 | ) -> (Color, Color) { |
| 40 | use crate::tui::ambient_life::AmbientActivity; |
| 41 | let sky = match activity { |
| 42 | AmbientActivity::Subagents => rgb(theme.accent_secondary).unwrap_or((79, 209, 197)), |
| 43 | _ => rgb(theme.info).unwrap_or((106, 174, 242)), |
| 44 | }; |
| 45 | // `mix(sky, base, t)`: larger `t` sits closer to the background — dimmer. |
| 46 | let (toward_base_a, toward_base_b) = match activity { |
| 47 | AmbientActivity::Reasoning => (0.58, 0.44), |
| 48 | AmbientActivity::Reading => (0.50, 0.36), |
| 49 | AmbientActivity::Tools => (0.30, 0.18), |
| 50 | AmbientActivity::Subagents => (0.34, 0.22), |
| 51 | AmbientActivity::Verifying | AmbientActivity::Baseline => (0.42, 0.28), |
| 52 | }; |
| 53 | // Only the underwater theme owns a painted base column; everywhere else |
| 54 | // the terminal's own ground (Color::Reset) is the base and the inks fall |
| 55 | // back to the theme's info lane. |
| 56 | let mix_base = rgb(theme.surface_bg) |
| 57 | .or_else(|| OceanRamp::for_theme(theme).and_then(|ramp| rgb(ramp.middle))); |
| 58 | match mix_base { |
| 59 | Some(base) => ( |
| 60 | color(mix(sky, base, toward_base_a)), |
| 61 | color(mix(sky, base, toward_base_b)), |
| 62 | ), |
| 63 | None => (theme.info, theme.info), |
| 64 | } |
| 65 | } |
| 66 | |
| 67 | /// Length of the completion breath (the column's settle flourish), ms. |
| 68 | pub const COMPLETION_BREATH_MS: u128 = NativeRamp::COMPLETION_BREATH.as_millis(); |
| 69 | |
| 70 | /// Extra ms after the breath during which ambient life eases out of view. |
| 71 | pub const SETTLE_MS: u128 = 600; |
| 72 | pub(crate) const COMPLETION_SETTLE_MS: u128 = COMPLETION_BREATH_MS + SETTLE_MS; |
| 73 | |
| 74 | /// Ms over which animated life ramps in when a working phase begins. |
| 75 | pub const RAMP_MS: u128 = 450; |
| 76 | |
| 77 | /// Smoothstep easing: 0 at t=0, 1 at t=1, zero velocity at both ends. |
| 78 | #[must_use] |
| 79 | pub fn smoothstep(t: f32) -> f32 { |
| 80 | let t = t.clamp(0.0, 1.0); |
| 81 | t * t * (3.0 - 2.0 * t) |
| 82 | } |
| 83 | |
| 84 | /// Life presence (0..=1) as a pure function of the monotonic clocks. There is |
| 85 | /// deliberately NO per-frame mutable state here: the same inputs always yield |
| 86 | /// the same output, which keeps ambient-life renders deterministic. |
| 87 | /// |
| 88 | /// Rules: |
| 89 | /// - A turn just ended (`completion_elapsed_ms` within the breath) holds full |
| 90 | /// presence so ambient life keeps swimming through the settle flourish. |
| 91 | /// - After the breath, presence eases out over [`SETTLE_MS`] so the water |
| 92 | /// settles instead of snapping from animated to frozen. |
| 93 | /// - Browsing history or the pristine empty state is user-driven: full |
| 94 | /// presence immediately. |
| 95 | /// - A Working/Verifying phase ramps in from `turn_elapsed_ms` over |
| 96 | /// [`RAMP_MS`], giving bursty fast streams a calm, bounded onset. |
| 97 | /// - Everything else is fully static. |
| 98 | #[must_use] |
| 99 | pub fn life_presence( |
| 100 | completion_elapsed_ms: Option<u128>, |
| 101 | turn_elapsed_ms: Option<u128>, |
| 102 | animated: bool, |
| 103 | browsing_history: bool, |
| 104 | empty_state: bool, |
| 105 | ) -> f32 { |
| 106 | if let Some(elapsed) = completion_elapsed_ms { |
| 107 | if elapsed < COMPLETION_BREATH_MS { |
| 108 | return 1.0; |
| 109 | } |
| 110 | let t = (elapsed - COMPLETION_BREATH_MS) as f32 / SETTLE_MS as f32; |
| 111 | return 1.0 - smoothstep(t); |
| 112 | } |
| 113 | if !animated { |
| 114 | return 0.0; |
| 115 | } |
| 116 | if browsing_history || empty_state { |
| 117 | return 1.0; |
| 118 | } |
| 119 | match turn_elapsed_ms { |
| 120 | Some(elapsed) => smoothstep(elapsed as f32 / RAMP_MS as f32), |
| 121 | None => 1.0, |
| 122 | } |
| 123 | } |
| 124 | |
| 125 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 126 | pub struct OceanRamp { |
| 127 | pub surface: Color, |
| 128 | pub middle: Color, |
| 129 | pub deep: Color, |
| 130 | pub ambient: Color, |
| 131 | /// Tint for phases that are blocked on the user (Waiting / Approval). |
| 132 | /// The whole water field warms toward this so "needs you" is legible |
| 133 | /// from across the room, not only in the phase strip. |
| 134 | pub attention: Color, |
| 135 | /// Tint for the Failed outcome: a steady cast, not a pulse — it reports, |
| 136 | /// it does not ask. |
| 137 | pub failure: Color, |
| 138 | } |
| 139 | |
| 140 | /// One continuous water column shared by every shell band in a frame. |
| 141 | /// |
| 142 | /// Individual widgets still own their foreground and semantic surfaces, but |
| 143 | /// ordinary shell backgrounds sample this column with their absolute row. |
| 144 | /// That keeps the header, work strip, transcript, phase line, and composer |
| 145 | /// from each restarting the same miniature gradient. |
| 146 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 147 | pub struct OceanColumn { |
| 148 | ramp: OceanRamp, |
| 149 | viewport: Rect, |
| 150 | directional_light: bool, |
| 151 | session_depth: u8, |
| 152 | elapsed_ms: u128, |
| 153 | completion_elapsed_ms: Option<u128>, |
| 154 | phase: ShellPhase, |
| 155 | animated: bool, |
| 156 | /// Fixed-point (0..=1000) life presence; keeps `Eq` derivable. |
| 157 | presence: u16, |
| 158 | context_percent: u8, |
| 159 | paint_caps: Option<codewhale_ratatui::Caps>, |
| 160 | } |
| 161 | |
| 162 | #[derive(Debug, Clone, Copy, PartialEq, Eq)] |
| 163 | struct OceanRampCacheIdentity { |
| 164 | ramp: OceanRamp, |
| 165 | top: u16, |
| 166 | height: u16, |
| 167 | phase_tag: u8, |
| 168 | animated: bool, |
| 169 | completion_active: bool, |
| 170 | presence: u16, |
| 171 | context_percent: u8, |
| 172 | } |
| 173 | |
| 174 | impl OceanRampCacheIdentity { |
| 175 | fn fingerprint(self) -> u64 { |
| 176 | const OFFSET_BASIS: u64 = 0xcbf2_9ce4_8422_2325; |
| 177 | const PRIME: u64 = 0x0000_0100_0000_01b3; |
| 178 | |
| 179 | [ |
| 180 | color_cache_code(self.ramp.surface), |
| 181 | color_cache_code(self.ramp.middle), |
| 182 | color_cache_code(self.ramp.deep), |
| 183 | color_cache_code(self.ramp.ambient), |
| 184 | color_cache_code(self.ramp.attention), |
| 185 | color_cache_code(self.ramp.failure), |
| 186 | u32::from(self.top), |
| 187 | u32::from(self.height), |
| 188 | u32::from(self.phase_tag), |
| 189 | u32::from(self.animated), |
| 190 | u32::from(self.completion_active), |
| 191 | u32::from(self.presence), |
| 192 | u32::from(self.context_percent), |
| 193 | ] |
| 194 | .into_iter() |
| 195 | .flat_map(u32::to_le_bytes) |
| 196 | .fold(OFFSET_BASIS, |state, byte| { |
| 197 | (state ^ u64::from(byte)).wrapping_mul(PRIME) |
| 198 | }) |
| 199 | } |
| 200 | } |
| 201 | |
| 202 | fn color_cache_code(value: Color) -> u32 { |
| 203 | match value { |
| 204 | Color::Reset => 0, |
| 205 | Color::Black => 1, |
| 206 | Color::Red => 2, |
| 207 | Color::Green => 3, |
| 208 | Color::Yellow => 4, |
| 209 | Color::Blue => 5, |
| 210 | Color::Magenta => 6, |
| 211 | Color::Cyan => 7, |
| 212 | Color::Gray => 8, |
| 213 | Color::DarkGray => 9, |
| 214 | Color::LightRed => 10, |
| 215 | Color::LightGreen => 11, |
| 216 | Color::LightYellow => 12, |
| 217 | Color::LightBlue => 13, |
| 218 | Color::LightMagenta => 14, |
| 219 | Color::LightCyan => 15, |
| 220 | Color::White => 16, |
| 221 | Color::Indexed(index) => 0x0100_0000 | u32::from(index), |
| 222 | Color::Rgb(red, green, blue) => 0x0200_0000 | u32::from_be_bytes([0, red, green, blue]), |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | impl OceanColumn { |
| 227 | // Eight args mirroring the eight column fields; a params struct would |
| 228 | // only rename the call sites without removing a single decision. |
| 229 | #[allow(clippy::too_many_arguments)] |
| 230 | #[must_use] |
| 231 | pub fn new( |
| 232 | ramp: OceanRamp, |
| 233 | viewport: Rect, |
| 234 | elapsed_ms: u128, |
| 235 | completion_elapsed_ms: Option<u128>, |
| 236 | phase: ShellPhase, |
| 237 | animated: bool, |
| 238 | presence: u16, |
| 239 | context_percent: u8, |
| 240 | ) -> Self { |
| 241 | Self { |
| 242 | ramp, |
| 243 | viewport, |
| 244 | directional_light: false, |
| 245 | session_depth: 0, |
| 246 | elapsed_ms, |
| 247 | completion_elapsed_ms, |
| 248 | phase, |
| 249 | animated, |
| 250 | presence, |
| 251 | context_percent: context_percent.min(100), |
| 252 | paint_caps: None, |
| 253 | } |
| 254 | } |
| 255 | |
| 256 | pub(crate) fn still_room( |
| 257 | viewport: Rect, |
| 258 | ui: &UiTheme, |
| 259 | caps: Option<codewhale_ratatui::Caps>, |
| 260 | ) -> Option<Self> { |
| 261 | let ramp = OceanRamp::for_theme(ui)?; |
| 262 | Some( |
| 263 | Self::new(ramp, viewport, 0, None, ShellPhase::Idle, false, 0, 0) |
| 264 | .with_paint_caps(caps) |
| 265 | .with_directional_light(*ui == codewhale_palette::UNDERWATER_UI_THEME), |
| 266 | ) |
| 267 | } |
| 268 | |
| 269 | #[must_use] |
| 270 | pub fn color_at_y(self, y: u16) -> Color { |
| 271 | self.native() |
| 272 | .color_at_y_with_ramp(y, self.viewport, self.ramp.native()) |
| 273 | } |
| 274 | |
| 275 | fn native(self) -> NativeColumn { |
| 276 | // The host samples and gates these monotonic clocks once. The pure |
| 277 | // kit path owns the complete phase/context/presence/settle kernel; |
| 278 | // ColorCompatBackend remains the current terminal paint authority. |
| 279 | let motion = if self.animated || self.presence > 0 { |
| 280 | MotionMode::Full |
| 281 | } else { |
| 282 | MotionMode::Still |
| 283 | }; |
| 284 | let mut column = NativeColumn::new(phase_duration(self.elapsed_ms), motion) |
| 285 | .phase(native_phase(self.phase)) |
| 286 | .context_percent(self.context_percent) |
| 287 | .presence(self.presence) |
| 288 | .oklab_depth(true); |
| 289 | if let Some(elapsed) = self.completion_elapsed_ms { |
| 290 | column = column.completion_elapsed(completion_duration(elapsed)); |
| 291 | } |
| 292 | column |
| 293 | } |
| 294 | |
| 295 | fn completion_active(self) -> bool { |
| 296 | self.phase == ShellPhase::Done |
| 297 | && (self.animated || self.presence > 0) |
| 298 | && self |
| 299 | .completion_elapsed_ms |
| 300 | .is_some_and(|elapsed| elapsed < COMPLETION_BREATH_MS) |
| 301 | } |
| 302 | |
| 303 | /// Elapsed milliseconds of the completion breath, when active. Ambient |
| 304 | /// life uses this to time the rare whale cameo on successful turns. |
| 305 | #[must_use] |
| 306 | pub fn completion_elapsed_ms(self) -> Option<u128> { |
| 307 | self.completion_elapsed_ms |
| 308 | } |
| 309 | |
| 310 | /// Compact phase discriminator for [`crate::tui::ambient_life::OceanRampCache`]. |
| 311 | #[must_use] |
| 312 | pub fn phase_tag(self) -> u8 { |
| 313 | match self.phase { |
| 314 | ShellPhase::Idle => 0, |
| 315 | ShellPhase::Typing => 1, |
| 316 | ShellPhase::Working => 2, |
| 317 | ShellPhase::Verifying => 3, |
| 318 | ShellPhase::Waiting => 4, |
| 319 | ShellPhase::Approval => 5, |
| 320 | ShellPhase::Done => 6, |
| 321 | ShellPhase::Failed => 7, |
| 322 | } |
| 323 | } |
| 324 | |
| 325 | fn ramp_cache_identity(self) -> OceanRampCacheIdentity { |
| 326 | OceanRampCacheIdentity { |
| 327 | ramp: self.ramp, |
| 328 | top: self.viewport.y, |
| 329 | height: self.viewport.height.max(1), |
| 330 | phase_tag: self.phase_tag(), |
| 331 | animated: self.animated, |
| 332 | completion_active: self.completion_active(), |
| 333 | presence: self.presence, |
| 334 | context_percent: self.context_percent, |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | /// Deterministic fingerprint of every column input owned by the ramp cache. |
| 339 | /// Actual colors are encoded explicitly; this never depends on randomized |
| 340 | /// hashing or debug formatting. |
| 341 | #[must_use] |
| 342 | pub fn ramp_fingerprint(self) -> u64 { |
| 343 | self.ramp_cache_identity().fingerprint() |
| 344 | } |
| 345 | |
| 346 | #[must_use] |
| 347 | pub fn with_viewport(mut self, viewport: Rect) -> Self { |
| 348 | self.viewport = viewport; |
| 349 | self |
| 350 | } |
| 351 | |
| 352 | #[must_use] |
| 353 | pub(crate) fn with_directional_light(mut self, enabled: bool) -> Self { |
| 354 | self.directional_light = enabled; |
| 355 | self |
| 356 | } |
| 357 | |
| 358 | pub(crate) fn with_session_depth(mut self, depth: f32) -> Self { |
| 359 | self.session_depth = (smoothstep(depth) * 255.0).round() as u8; |
| 360 | self |
| 361 | } |
| 362 | |
| 363 | /// Borrowed backend facts for this render snapshot; no detector or store. |
| 364 | #[must_use] |
| 365 | pub fn with_paint_caps(mut self, caps: Option<codewhale_ratatui::Caps>) -> Self { |
| 366 | self.paint_caps = caps; |
| 367 | self |
| 368 | } |
| 369 | |
| 370 | pub(crate) fn paint_theme(self, ground: Color) -> Option<codewhale_ratatui::Theme> { |
| 371 | let mut caps = self.paint_caps?; |
| 372 | // Equality-matched opaque ground is actual pane evidence. A terminal |
| 373 | // Reset/named ink supplies no RGB evidence; an OS hint cannot fill it. |
| 374 | caps.appearance = codewhale_ratatui::detect::appearance_for_background(ground)?; |
| 375 | if caps.appearance != codewhale_ratatui::detect::Appearance::Dark { |
| 376 | return None; |
| 377 | } |
| 378 | Some( |
| 379 | codewhale_ratatui::Theme::new(caps) |
| 380 | .ground(codewhale_ratatui::Ground::Ocean) |
| 381 | .tui_palette(codewhale_ratatui::TuiPalette::Underwater), |
| 382 | ) |
| 383 | } |
| 384 | |
| 385 | pub fn paint_matching_native( |
| 386 | self, |
| 387 | area: Rect, |
| 388 | buf: &mut Buffer, |
| 389 | background: Color, |
| 390 | ui_theme: &UiTheme, |
| 391 | protected: &[Rect], |
| 392 | ) { |
| 393 | let facts = codewhale_ratatui::ocean::OceanPaintFacts { |
| 394 | protected, |
| 395 | ..codewhale_ratatui::ocean::OceanPaintFacts::new(background) |
| 396 | }; |
| 397 | self.paint_native(area, buf, ui_theme, &facts); |
| 398 | } |
| 399 | |
| 400 | pub(crate) fn paint_native( |
| 401 | self, |
| 402 | area: Rect, |
| 403 | buf: &mut Buffer, |
| 404 | ui_theme: &UiTheme, |
| 405 | facts: &codewhale_ratatui::ocean::OceanPaintFacts<'_>, |
| 406 | ) { |
| 407 | if ui_theme.name != codewhale_palette::UNDERWATER_UI_THEME.name { |
| 408 | return; |
| 409 | } |
| 410 | let Some(theme) = self.paint_theme(facts.ground) else { |
| 411 | return; |
| 412 | }; |
| 413 | if self.directional_light && theme.caps().paints_tokens() { |
| 414 | launch::paint(self, area, buf, theme.depth(), ui_theme, facts); |
| 415 | return; |
| 416 | } |
| 417 | let inks = codewhale_ratatui::ocean::OceanContrastInks { |
| 418 | border: Some(ui_theme.border), |
| 419 | border_strong: Some(ui_theme.text_hint), |
| 420 | dim: Some(ui_theme.text_dim), |
| 421 | }; |
| 422 | let native = self |
| 423 | .native() |
| 424 | .ramp(self.ramp.native()) |
| 425 | .viewport(self.viewport) |
| 426 | .contrast_inks(inks); |
| 427 | native.apply_native(area, buf, &theme, facts, |cell, water| { |
| 428 | crate::tui::color_compat::project_ocean_ink(cell, water, theme.depth(), ui_theme) |
| 429 | }); |
| 430 | if self.animated |
| 431 | && self.presence > 0 |
| 432 | && area.width >= AMBIENT_MIN_WIDTH |
| 433 | && area.height >= AMBIENT_MIN_HEIGHT |
| 434 | { |
| 435 | let caustics = codewhale_ratatui::ocean::OceanCausticFacts { |
| 436 | paint: *facts, |
| 437 | elapsed: Duration::from_millis((self.elapsed_ms % 1_000_000) as u64), |
| 438 | band_rows: (area.height / 2).clamp(4, 14), |
| 439 | }; |
| 440 | native.apply_volumetric_caustics(area, buf, &theme, &caustics); |
| 441 | } |
| 442 | } |
| 443 | |
| 444 | // Existing pure fixture facade explicitly supplies a synthetic backend. |
| 445 | // Runtime callers all use the live fact-bearing native facade above. |
| 446 | #[cfg(test)] |
| 447 | fn paint_matching(self, area: Rect, buf: &mut Buffer, background: Color) { |
| 448 | let caps = crate::tui::color_compat::ColorCompatBackend::new( |
| 449 | std::io::sink(), |
| 450 | codewhale_palette::ColorDepth::TrueColor, |
| 451 | codewhale_palette::PaletteMode::Dark, |
| 452 | ) |
| 453 | .native_ocean_caps(); |
| 454 | self.with_paint_caps(Some(caps)).paint_matching_native( |
| 455 | area, |
| 456 | buf, |
| 457 | background, |
| 458 | &codewhale_palette::UNDERWATER_UI_THEME, |
| 459 | &[], |
| 460 | ); |
| 461 | } |
| 462 | } |
| 463 | |
| 464 | impl OceanRamp { |
| 465 | #[must_use] |
| 466 | pub fn for_theme(theme: &UiTheme) -> Option<Self> { |
| 467 | // The painted field exists only under the underwater theme; every |
| 468 | // other theme leaves the terminal's ground alone. A user-supplied |
| 469 | // `background_color` rewrites the underwater surfaces through |
| 470 | // `with_background_color` and remains the source of truth there. |
| 471 | if theme.name != codewhale_palette::UNDERWATER_UI_THEME.name { |
| 472 | return None; |
| 473 | } |
| 474 | |
| 475 | let default = *theme == codewhale_palette::UNDERWATER_UI_THEME; |
| 476 | Some(Self { |
| 477 | // The authored Codewhale water column: unmistakably blue all the |
| 478 | // way to the floor. These restrained ocean shades sit between the |
| 479 | // shell's ink surfaces and its ambient blue, so the field gains |
| 480 | // depth without becoming a saturated blue panel. |
| 481 | surface: if default { |
| 482 | Color::Rgb(0x0b, 0x2c, 0x53) |
| 483 | } else { |
| 484 | NativeRamp::SURFACE |
| 485 | }, |
| 486 | middle: if default { |
| 487 | Color::Rgb(0x09, 0x22, 0x41) |
| 488 | } else { |
| 489 | NativeRamp::MIDDLE |
| 490 | }, |
| 491 | deep: NativeRamp::DEEP, |
| 492 | ambient: if default { |
| 493 | Color::Rgb(0x2a, 0x5e, 0x91) |
| 494 | } else { |
| 495 | NativeRamp::AMBIENT |
| 496 | }, |
| 497 | attention: theme.warning, |
| 498 | failure: theme.error_fg, |
| 499 | }) |
| 500 | } |
| 501 | |
| 502 | fn native(self) -> NativeRamp { |
| 503 | NativeRamp::new( |
| 504 | self.surface, |
| 505 | self.middle, |
| 506 | self.deep, |
| 507 | self.ambient, |
| 508 | self.attention, |
| 509 | self.failure, |
| 510 | ) |
| 511 | } |
| 512 | |
| 513 | #[cfg(test)] |
| 514 | #[must_use] |
| 515 | pub fn color_at_context(self, row: u16, height: u16, context_percent: u8) -> Color { |
| 516 | self.native() |
| 517 | .color_at_context_oklab(row, height, context_percent) |
| 518 | } |
| 519 | |
| 520 | #[cfg(test)] |
| 521 | #[must_use] |
| 522 | pub fn color_at_phase_context( |
| 523 | self, |
| 524 | row: u16, |
| 525 | height: u16, |
| 526 | elapsed_ms: u128, |
| 527 | phase: ShellPhase, |
| 528 | context_percent: u8, |
| 529 | ) -> Color { |
| 530 | self.native().color_at_phase_context_oklab( |
| 531 | row, |
| 532 | height, |
| 533 | phase_duration(elapsed_ms), |
| 534 | native_phase(phase), |
| 535 | context_percent, |
| 536 | ) |
| 537 | } |
| 538 | |
| 539 | #[cfg(test)] |
| 540 | #[must_use] |
| 541 | pub fn color_at_completion_context( |
| 542 | self, |
| 543 | row: u16, |
| 544 | height: u16, |
| 545 | elapsed_ms: u128, |
| 546 | context_percent: u8, |
| 547 | ) -> Color { |
| 548 | self.native().color_at_completion_context( |
| 549 | row, |
| 550 | height, |
| 551 | completion_duration(elapsed_ms), |
| 552 | context_percent, |
| 553 | ) |
| 554 | } |
| 555 | } |
| 556 | |
| 557 | fn native_phase(phase: ShellPhase) -> OceanPhase { |
| 558 | match phase { |
| 559 | ShellPhase::Idle => OceanPhase::Idle, |
| 560 | ShellPhase::Typing => OceanPhase::Typing, |
| 561 | ShellPhase::Working => OceanPhase::Working, |
| 562 | ShellPhase::Verifying => OceanPhase::Verifying, |
| 563 | ShellPhase::Waiting => OceanPhase::Waiting, |
| 564 | ShellPhase::Approval => OceanPhase::Approval, |
| 565 | ShellPhase::Done => OceanPhase::Done, |
| 566 | ShellPhase::Failed => OceanPhase::Failed, |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | fn phase_duration(elapsed_ms: u128) -> Duration { |
| 571 | Duration::from_millis((elapsed_ms % 90_000) as u64) |
| 572 | } |
| 573 | |
| 574 | fn completion_duration(elapsed_ms: u128) -> Duration { |
| 575 | Duration::from_millis(elapsed_ms.min(COMPLETION_BREATH_MS) as u64) |
| 576 | } |
| 577 | |
| 578 | #[must_use] |
| 579 | fn rgb(value: Color) -> Option<(u8, u8, u8)> { |
| 580 | match value { |
| 581 | Color::Rgb(r, g, b) => Some((r, g, b)), |
| 582 | _ => None, |
| 583 | } |
| 584 | } |
| 585 | |
| 586 | #[must_use] |
| 587 | fn color((r, g, b): (u8, u8, u8)) -> Color { |
| 588 | Color::Rgb(r, g, b) |
| 589 | } |
| 590 | |
| 591 | #[must_use] |
| 592 | pub fn mix_colors(from: Color, to: Color, amount: f32) -> Color { |
| 593 | match (rgb(from), rgb(to)) { |
| 594 | (Some(_), Some(_)) => codewhale_ratatui::color::mix_oklab(from, to, amount), |
| 595 | _ => from, |
| 596 | } |
| 597 | } |
| 598 | |
| 599 | #[must_use] |
| 600 | pub fn scale_color(value: Color, brightness: f32) -> Color { |
| 601 | let Some((r, g, b)) = rgb(value) else { |
| 602 | return value; |
| 603 | }; |
| 604 | color(( |
| 605 | (f32::from(r) * brightness).round().clamp(0.0, 255.0) as u8, |
| 606 | (f32::from(g) * brightness).round().clamp(0.0, 255.0) as u8, |
| 607 | (f32::from(b) * brightness).round().clamp(0.0, 255.0) as u8, |
| 608 | )) |
| 609 | } |
| 610 | |
| 611 | #[must_use] |
| 612 | fn mix(from: (u8, u8, u8), to: (u8, u8, u8), amount: f32) -> (u8, u8, u8) { |
| 613 | let amount = amount.clamp(0.0, 1.0); |
| 614 | let channel = |a: u8, b: u8| { |
| 615 | (f32::from(a) + (f32::from(b) - f32::from(a)) * amount) |
| 616 | .round() |
| 617 | .clamp(0.0, 255.0) as u8 |
| 618 | }; |
| 619 | ( |
| 620 | channel(from.0, to.0), |
| 621 | channel(from.1, to.1), |
| 622 | channel(from.2, to.2), |
| 623 | ) |
| 624 | } |
| 625 | |
| 626 | #[cfg(test)] |
| 627 | #[path = "ocean/tests.rs"] |
| 628 | mod tests; |
| 629 | |
| 630 | #[cfg(test)] |
| 631 | #[path = "ocean/guarded_tests.rs"] |
| 632 | mod guarded_tests; |
| 633 |