Download kernel/src/apps/editor.rs from Jaswanth-2008/AeroOS: direct link, hf CLI and curl.
- Browser
- Download file 3.1 kB
-
https://huggingface.co/Jaswanth-2008/AeroOS/resolve/main/kernel/src/apps/editor.rs
- Command line
-
hf download hf://Jaswanth-2008/AeroOS/kernel/src/apps/editor.rs
-
curl -L -o editor.rs https://huggingface.co/Jaswanth-2008/AeroOS/resolve/main/kernel/src/apps/editor.rs
3.1 kB
| extern crate alloc; | |
| use alloc::string::String; | |
| use alloc::vec::Vec; | |
| use pc_keyboard::DecodedKey; | |
| use crate::graphics::color::Color; | |
| use crate::graphics::surface::Surface; | |
| pub struct EditorApp { | |
| pub content: Vec<String>, | |
| pub filename: String, | |
| } | |
| impl EditorApp { | |
| pub fn new() -> Self { | |
| let mut ed = Self { | |
| content: Vec::new(), | |
| filename: String::from("/user/welcome.txt"), | |
| }; | |
| ed.content.push(String::from("AeroOS Notepad - Native Text Editor")); | |
| ed.content.push(String::from("-----------------------------------")); | |
| ed.content.push(String::from("AeroOS is built completely in safe Rust.")); | |
| ed.content.push(String::from("Direct UEFI GOP 32-bit linear framebuffer.")); | |
| ed.content.push(String::from("Full VirtIO-Net smoltcp networking active!")); | |
| ed.content.push(String::from("")); | |
| ed.content.push(String::from("Type anywhere to edit this document...")); | |
| ed | |
| } | |
| pub fn handle_key(&mut self, key: DecodedKey) { | |
| match key { | |
| DecodedKey::Unicode('\n') => { | |
| self.content.push(String::new()); | |
| } | |
| DecodedKey::Unicode('\u{08}') => { | |
| if let Some(last_line) = self.content.last_mut() { | |
| if !last_line.is_empty() { | |
| last_line.pop(); | |
| } else if self.content.len() > 1 { | |
| self.content.pop(); | |
| } | |
| } | |
| } | |
| DecodedKey::Unicode(c) => { | |
| if c >= ' ' && c <= '~' { | |
| if let Some(last_line) = self.content.last_mut() { | |
| if last_line.len() < 50 { | |
| last_line.push(c); | |
| } | |
| } else { | |
| let mut s = String::new(); | |
| s.push(c); | |
| self.content.push(s); | |
| } | |
| } | |
| } | |
| _ => {} | |
| } | |
| } | |
| pub fn render(&self, surface: &mut Surface, x: i32, y: i32, w: usize, h: usize) { | |
| surface.draw_rect(x, y, w, h, Color::rgb(30, 32, 42)); | |
| // Subheader file info | |
| surface.draw_rect(x, y, w, 24, Color::rgb(22, 24, 32)); | |
| surface.draw_text(&self.filename, x + 10, y + 4, Color::TEXT_SECONDARY); | |
| // Lines | |
| let mut cur_y = y + 32; | |
| for (i, line) in self.content.iter().enumerate() { | |
| if cur_y + 16 > y + h as i32 - 10 { | |
| break; | |
| } | |
| // Line numbers | |
| let line_num = alloc::format!("{:2} ", i + 1); | |
| surface.draw_text(&line_num, x + 8, cur_y, Color::TEXT_MUTED); | |
| surface.draw_text(line, x + 36, cur_y, Color::WHITE); | |
| cur_y += 18; | |
| } | |
| // Active blinking cursor | |
| if let Some(last) = self.content.last() { | |
| let cx = x + 36 + (last.len() as i32 * 8); | |
| let cy = (cur_y - 18).max(y + 32); | |
| surface.draw_rect(cx, cy, 2, 14, Color::ACCENT_CYAN); | |
| } | |
| } | |
| } | |