mod config;
mod terminal;
use config::Config;
use gpui::{
App, Application, Bounds, ClipboardItem, Context, ElementInputHandler, EntityInputHandler,
FocusHandle, FontStyle, FontWeight, KeyDownEvent, Keystroke, MouseButton, MouseDownEvent,
MouseMoveEvent, Pixels, Point, ResizeEdge, ScrollWheelEvent, TextRun, TitlebarOptions,
UTF16Selection, UnderlineStyle, Window, WindowBounds, WindowDecorations, WindowOptions, canvas,
div, fill, font, point, prelude::*, px, rgb, size,
};
use std::{ops::Range, sync::atomic::Ordering, time::Duration};
use terminal::{Terminal, ansi_color};
struct Ducky {
config: Config,
tabs: Vec<Terminal>,
active: usize,
focus: FocusHandle,
message: Option<String>,
bounds: Bounds<Pixels>,
cell_width: f32,
selection: Option<((u16, u16), (u16, u16))>,
dragging: bool,
composition: String,
}
impl Ducky {
fn new(config: Config, cx: &mut Context<Self>) -> anyhow::Result<Self> {
let terminal = Terminal::new(&config)?;
cx.spawn(async |this, cx| {
loop {
cx.background_executor()
.timer(Duration::from_millis(16))
.await;
if this
.update(cx, |this, cx| {
let changed = this
.tabs
.iter()
.any(|t| t.dirty.swap(false, Ordering::AcqRel));
if changed {
cx.notify();
}
})
.is_err()
{
break;
}
}
})
.detach();
Ok(Self {
config,
tabs: vec![terminal],
active: 0,
focus: cx.focus_handle(),
message: None,
bounds: Bounds::default(),
cell_width: 9.,
selection: None,
dragging: false,
composition: String::new(),
})
}
fn send(&mut self, bytes: &[u8], cx: &mut Context<Self>) {
self.selection = None;
if let Err(e) = self.tabs[self.active].write(bytes) {
self.message = Some(e.to_string());
}
cx.notify();
}
fn new_tab(&mut self, cx: &mut Context<Self>) {
match Terminal::new(&self.config) {
Ok(t) => {
self.tabs.push(t);
self.active = self.tabs.len() - 1;
self.selection = None;
}
Err(e) => self.message = Some(e.to_string()),
}
cx.notify();
}
fn close_tab(&mut self, cx: &mut Context<Self>) {
self.tabs.remove(self.active);
if self.tabs.is_empty() {
cx.quit();
return;
}
self.active = self.active.min(self.tabs.len() - 1);
self.selection = None;
cx.notify();
}
fn reload(&mut self, cx: &mut Context<Self>) {
match Config::load() {
Ok(c) => {
self.config = c;
self.message = Some("Configuration reloaded".into());
}
Err(e) => self.message = Some(format!("{e:#}")),
}
cx.notify();
}
fn copy(&mut self, cx: &mut Context<Self>) {
let parser = self.tabs[self.active].parser.lock().unwrap();
let s = parser.screen();
let text = if let Some((a, b)) = self.selection {
let (a, b) = if a <= b { (a, b) } else { (b, a) };
let mut text = String::new();
for row in a.0..=b.0 {
let mut line = String::new();