Services

Services Overview

Background services for application functionality.

Overview

Services provide background functionality for TUI applications. They handle tasks like file watching, repository monitoring, and global hotkey management.

Available Services

FileClocker

GitWatcher

RepoWatcher

HotkeyService

Service Architecture

Services in ratkit are designed to be:

  • Non-blocking - Run asynchronously without blocking the UI
  • Event-driven - Emit events when state changes
  • Composable - Combine multiple services together
  • Resource-efficient - Minimal overhead when idle

Usage Pattern

Services typically follow this pattern:

use ratkit::services::file_watcher::FileClocker;

// Create the service
let watcher = FileClocker::new("./src");

// Set up event handling
watcher.on_change(|event| {
    println!("File changed: {:?}", event);
});

// Start watching
watcher.start()?;

// Later, stop the service
watcher.stop()?;

Integration with TUI

Services work seamlessly with ratatui applications:

use ratatui::crossterm::event::{self, Event, KeyCode};

loop {
    // Poll for events with timeout
    if event::poll(Duration::from_millis(100))? {
        match event::read()? {
            Event::Key(key) => handle_key(key),
            _ => {}
        }
    }
    
    // Service updates happen automatically
    // Redraw UI
    terminal.draw(|frame| render(frame))?;
}