Compare commits

...

6 Commits

Author SHA1 Message Date
narf_poit 5c532197c8 Merge pull request 'master' (#1) from master into main
Reviewed-on: http://192.168.1.66:30008/narf_poit/Klipper-tui/pulls/1
2026-03-21 15:11:54 -07:00
narf_poit 5e649474d9 done for today 2026-03-21 15:11:54 -07:00
narf_poit 8e32cc7277 getting there 2026-03-21 15:11:54 -07:00
narf_poit 6314b16e57 another commit 2026-03-21 15:11:54 -07:00
narf_poit 1d1a4af630 yaaasssss 2026-03-21 15:11:54 -07:00
narf_poit 31d23e078b 2nd commit 2026-03-21 15:11:54 -07:00
7 changed files with 2093 additions and 0 deletions
+1
View File
@@ -0,0 +1 @@
/target
Generated
+1839
View File
File diff suppressed because it is too large Load Diff
+10
View File
@@ -0,0 +1,10 @@
[package]
name = "klipper-tui"
version = "0.1.0"
edition = "2024"
[dependencies]
ratatui = "0.30.0"
crossterm = "0.29.0"
clap = { version = "4.6.0", features = ["derive"] }
serde_json = "1.0.149"
+100
View File
@@ -0,0 +1,100 @@
pub struct TabsState<'a> {
pub titles: Vec<&'a str>,
pub index: usize,
}
impl<'a> TabsState<'a> {
pub const fn new(titles: Vec<&'a str>) -> Self {
Self { titles, index: 0 }
}
pub fn next(&mut self) {
self.index = (self.index + 1) % self.titles.len();
}
pub fn previous(&mut self) {
if self.index > 0 {
self.index -= 1;
} else {
self.index = self.titles.len() - 1;
}
}
}
pub struct Servers<'a> {
pub servers: Vec<Server<'a>>,
pub index: usize,
}
impl<'a> Servers<'a> {
pub const fn new( servers: Vec<Server<'a>>) -> Self {
Self { servers, index: 0 }
}
pub fn next(&mut self) {
self.index = (self.index + 1) % self.servers.len();
}
pub fn previous(&mut self) {
if self.index > 0 {
self.index -= 1;
} else {
self.index = self.servers.len() - 1;
}
}
}
pub struct Server<'a> {
pub name: &'a str,
pub location: &'a str,
pub ip: &'a str,
pub connected: bool,
}
pub struct App<'a> {
pub title: &'a str,
pub should_quit: bool,
pub tabs: TabsState<'a>,
pub servers: Vec<Server<'a>>,
pub enhanced_graphics: bool,
}
impl<'a> App<'a> {
pub fn new(title: &'a str, servers: Vec<Server<'a>>, enhanced_graphics: bool) -> Self {
App {
title,
should_quit: false,
tabs: TabsState::new(vec!["Dashboard","Machine info","Config"]),
servers,
enhanced_graphics,
}
}
pub fn on_up(&mut self) {
}
pub fn on_down(&mut self) {
}
pub fn on_right(&mut self) {
}
pub fn on_left(&mut self) {
}
pub fn on_key(&mut self, c: char) {
match c {
'q' => {
self.should_quit = true;
}
_ => {}
}
}
pub fn on_tick(&mut self) {
}
}
+95
View File
@@ -0,0 +1,95 @@
use std::error::Error;
use std::io;
use std::time::{Duration, Instant};
use crossterm::event::{self, DisableMouseCapture, EnableMouseCapture, KeyCode};
use crossterm::execute;
use crossterm::terminal::{
disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen,
};
use ratatui::backend::{Backend, CrosstermBackend};
use ratatui::Terminal;
use crate::app::{App, Server};
use crate::ui;
pub fn run(tick_rate: Duration, enhanced_graphics: bool) -> Result<(), Box<dyn Error>> {
// setup terminal
enable_raw_mode()?;
let mut stdout = io::stdout();
execute!(stdout, EnterAlternateScreen, EnableMouseCapture)?;
let backend = CrosstermBackend::new(stdout);
let mut terminal = Terminal::new(backend)?;
// create app and run it
let app = App::new("Klipper-Tui", get_servers(), enhanced_graphics);
let app_result = run_app(&mut terminal, app, tick_rate);
// restore terminal
disable_raw_mode()?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
terminal.show_cursor()?;
if let Err(err) = app_result {
println!("{err:?}");
}
Ok(())
}
fn run_app<B: Backend>(
terminal: &mut Terminal<B>,
mut app: App,
tick_rate: Duration,
) -> Result<(), Box<dyn Error>>
where
B::Error: 'static,{
let mut last_tick = Instant::now();
loop {
terminal.draw(|frame| ui::render(frame, &mut app))?;
let timeout = tick_rate.saturating_sub(last_tick.elapsed());
if !event::poll(timeout)? {
app.on_tick();
last_tick = Instant::now();
continue;
}
if let Some(key) = event::read()?.as_key_press_event() {
match key.code {
KeyCode::Char('h') | KeyCode::Left => app.on_left(),
KeyCode::Char('j') | KeyCode::Down => app.on_down(),
KeyCode::Char('k') | KeyCode::Up => app.on_up(),
KeyCode::Char('l') | KeyCode::Right => app.on_right(),
KeyCode::Char(c) => app.on_key(c),
_ => {}
}
}
if app.should_quit {
return Ok(());
}
}
}
pub fn get_servers<'a>() -> Vec<Server<'a>> {
//todo: add printer ip addrs
let printers: Vec<Server<'a>> = vec![
crate::app::Server{
name: "Micron+",
location:"Office",
ip:"192.168.1.247",
connected:false,
},
crate::app::Server{
name: "LDO V0.2",
location: "Office",
ip: "192.168.1.217",
connected: false,
}
];
return crate::app::Servers::new(printers).servers;
}
+30
View File
@@ -0,0 +1,30 @@
use std::io;
use std::error::Error;
use std::time::Duration;
use clap::Parser;
mod app;
mod ui;
mod crossterm;
use crate::{
app::App,
ui::ui,
};
#[derive(Parser)]
struct Cli {
/// time in ms between two ticks.
#[arg(short, long, default_value_t = 200)]
tick_rate: u64,
/// whether unicode symbols are used to improve the overall look of the app
#[arg(short, long, default_value_t = true)]
unicode: bool,
}
fn main() -> Result<(), Box<dyn Error>> {
let cli = Cli::parse();
let tick_rate = Duration::from_millis(cli.tick_rate);
crate::crossterm::run(tick_rate, cli.unicode)?;
Ok(())
}
+18
View File
@@ -0,0 +1,18 @@
use ratatui::{
layout::{Constraint, Direction, Layout, Rect},
style::{Color, Style},
text::{Line, Span, Text},
widgets::{Block, Borders, Clear, List, ListItem, Paragraph, Wrap},
Frame,
};
use crate::app::App;
// ANCHOR: method_sig
pub fn ui(frame: &mut Frame, app: &App) {
}
pub fn render(frame: &mut Frame, app: &mut App) {
}