5 Commits

Author SHA1 Message Date
Uber Veng
65e189c913 Fixed auto update 2026-05-22 00:24:19 +07:00
Uber Veng
cb85dfa039 v1.0.3 vim motions 2026-05-22 00:10:31 +07:00
Uber Veng
9600e49385 added vim motions 2026-05-22 00:06:30 +07:00
Uber Veng
149246b000 gitignore update 2026-05-21 23:14:07 +07:00
Uber Veng
f2349c9f15 added auto update & changed config path 2026-05-21 23:02:14 +07:00
8 changed files with 512 additions and 12 deletions

1
.gitignore vendored
View File

@@ -1,3 +1,4 @@
/target /target
*txt *txt
.DS_Store .DS_Store
/dist

2
Cargo.lock generated
View File

@@ -1123,7 +1123,7 @@ dependencies = [
[[package]] [[package]]
name = "ostiary" name = "ostiary"
version = "1.0.0" version = "1.0.4"
dependencies = [ dependencies = [
"anyhow", "anyhow",
"crossterm", "crossterm",

View File

@@ -1,6 +1,6 @@
[package] [package]
name = "ostiary" name = "ostiary"
version = "1.0.0" version = "1.0.4"
edition = "2024" edition = "2024"
[dependencies] [dependencies]

View File

@@ -4,6 +4,7 @@ use crate::error::Result;
use crate::executor; use crate::executor;
use crate::menu::{Action, InteractionMode, MenuItem, MenuItemKind, MenuRoot}; use crate::menu::{Action, InteractionMode, MenuItem, MenuItemKind, MenuRoot};
use crate::network; use crate::network;
use crate::updater;
use crossterm::event::{Event as CrosstermEvent, KeyCode, KeyEventKind}; use crossterm::event::{Event as CrosstermEvent, KeyCode, KeyEventKind};
use tokio::sync::mpsc::UnboundedSender; use tokio::sync::mpsc::UnboundedSender;
@@ -16,6 +17,11 @@ pub enum Event {
StructuredCommand(executor::StructuredCommand), StructuredCommand(executor::StructuredCommand),
StructuredOutput(String), StructuredOutput(String),
StructuredFinished(i32), StructuredFinished(i32),
UpdateAvailable(updater::UpdateInfo),
UpdateProgress(u64),
/// Carries the exe path captured before the binary was replaced.
UpdateDone(std::path::PathBuf),
UpdateError(String),
} }
/// A pending request to hand the terminal to an external interactive process. /// A pending request to hand the terminal to an external interactive process.
@@ -35,6 +41,8 @@ pub struct App {
pub event_tx: UnboundedSender<Event>, pub event_tx: UnboundedSender<Event>,
/// When set, main loop suspends ratatui, runs the command, then restores. /// When set, main loop suspends ratatui, runs the command, then restores.
pub pending_exec: Option<PendingExec>, pub pending_exec: Option<PendingExec>,
/// Exe path to exec after update; captured before the binary was replaced.
pub pending_restart: Option<std::path::PathBuf>,
} }
pub enum Popup { pub enum Popup {
@@ -62,6 +70,22 @@ pub enum Popup {
}, },
Downloading { progress: f32, message: String }, Downloading { progress: f32, message: String },
Message { text: String, level: MessageLevel }, Message { text: String, level: MessageLevel },
UpdateConfirm {
info: updater::UpdateInfo,
},
Updating {
info: updater::UpdateInfo,
downloaded: u64,
status: UpdatingStatus,
},
}
#[derive(Debug, PartialEq)]
pub enum UpdatingStatus {
Downloading,
Applying,
Done,
Failed(String),
} }
#[derive(Debug, Clone, Copy)] #[derive(Debug, Clone, Copy)]
@@ -82,9 +106,25 @@ impl App {
popup: None, popup: None,
event_tx, event_tx,
pending_exec: None, pending_exec: None,
pending_restart: None,
} }
} }
pub fn check_update(&mut self) {
let api_base = match &self.config.update_api {
Some(url) => url.clone(),
None => return,
};
let timeout = self.config.timeout_sec;
let tx = self.event_tx.clone();
tokio::spawn(async move {
if let Ok(Some(info)) = updater::check(&api_base, timeout).await {
let _ = tx.send(Event::UpdateAvailable(info));
}
});
}
pub async fn load_menu(&mut self) { pub async fn load_menu(&mut self) {
let server_url = self.config.server_url.clone(); let server_url = self.config.server_url.clone();
let timeout = self.config.timeout_sec; let timeout = self.config.timeout_sec;
@@ -190,6 +230,32 @@ impl App {
// script), we silently discard the exit event. // script), we silently discard the exit event.
Ok(false) Ok(false)
} }
Event::UpdateAvailable(info) => {
// Only show if no popup is currently open (don't interrupt running scripts)
if self.popup.is_none() {
self.popup = Some(Popup::UpdateConfirm { info });
}
Ok(false)
}
Event::UpdateProgress(bytes) => {
if let Some(Popup::Updating { downloaded, .. }) = &mut self.popup {
*downloaded = bytes;
}
Ok(false)
}
Event::UpdateDone(exe_path) => {
if let Some(Popup::Updating { status, .. }) = &mut self.popup {
*status = UpdatingStatus::Done;
}
self.pending_restart = Some(exe_path);
Ok(false)
}
Event::UpdateError(msg) => {
if let Some(Popup::Updating { status, .. }) = &mut self.popup {
*status = UpdatingStatus::Failed(msg);
}
Ok(false)
}
} }
} }
@@ -233,6 +299,11 @@ impl App {
Some(executor::StructuredCommand::Confirm { .. }) Some(executor::StructuredCommand::Confirm { .. })
); );
let has_command = current_command.is_some(); let has_command = current_command.is_some();
// Vim motions are disabled only when free text input is active.
let is_text_input = matches!(
current_command,
Some(executor::StructuredCommand::Input { .. })
);
match key.code { match key.code {
// ── Log scrolling (PageUp / PageDown always work) ─ // ── Log scrolling (PageUp / PageDown always work) ─
@@ -330,7 +401,66 @@ impl App {
let _ = current_command.take(); let _ = current_command.take();
let _ = reply_tx.send("n".to_string()); let _ = reply_tx.send("n".to_string());
} else { } else {
// Kill the script if still running. if let Some(kx) = kill_tx.take() {
let _ = kx.send(());
}
self.popup = None;
}
}
// ── Vim motions (off during text input) ───────────
KeyCode::Char('k') if !is_text_input => {
if is_menu {
if *menu_selected_index > 0 {
*menu_selected_index -= 1;
}
} else if !has_command {
*log_scroll_pos = log_scroll_pos.saturating_sub(1);
*log_follow_bottom = false;
}
}
KeyCode::Char('j') if !is_text_input => {
if is_menu {
if let Some(executor::StructuredCommand::Menu {
options, ..
}) = current_command
{
if *menu_selected_index + 1 < options.len() {
*menu_selected_index += 1;
}
}
} else if !has_command {
*log_scroll_pos = log_scroll_pos.saturating_add(1);
*log_follow_bottom =
*log_scroll_pos + 1 >= log_buffer.len();
}
}
KeyCode::Char('l') if !is_text_input => {
// Forward / confirm — same logic as Enter.
if let Some(cmd) = current_command.take() {
let response = match &cmd {
executor::StructuredCommand::Confirm { .. } => {
"y".to_string()
}
executor::StructuredCommand::Menu {
options, ..
} => options
.get(*menu_selected_index)
.map(|o| o.id.clone())
.unwrap_or_default(),
_ => String::new(),
};
let _ = reply_tx.send(response);
input_buffer.clear();
*menu_selected_index = 0;
}
}
KeyCode::Char('h') if !is_text_input => {
// Back / cancel — same logic as Esc.
if is_confirm {
let _ = current_command.take();
let _ = reply_tx.send("n".to_string());
} else {
if let Some(kx) = kill_tx.take() { if let Some(kx) = kill_tx.take() {
let _ = kx.send(()); let _ = kx.send(());
} }
@@ -347,6 +477,67 @@ impl App {
self.popup = None; self.popup = None;
return Ok(false); return Ok(false);
} }
Popup::UpdateConfirm { info } => {
match key.code {
KeyCode::Char('y') | KeyCode::Char('Y') | KeyCode::Char('l') => {
let info = info.clone();
self.popup = Some(Popup::Updating {
info: info.clone(),
downloaded: 0,
status: UpdatingStatus::Downloading,
});
let (progress_tx, mut progress_rx) =
tokio::sync::mpsc::unbounded_channel::<u64>();
let tx = self.event_tx.clone();
let info_clone = info.clone();
// Forward progress events
tokio::spawn(async move {
while let Some(bytes) = progress_rx.recv().await {
let _ = tx.send(Event::UpdateProgress(bytes));
}
});
// Download and apply
let tx2 = self.event_tx.clone();
tokio::spawn(async move {
match updater::download_and_apply(
&info_clone,
progress_tx,
)
.await
{
Ok(exe_path) => {
let _ = tx2.send(Event::UpdateDone(exe_path));
}
Err(e) => {
let _ =
tx2.send(Event::UpdateError(e.to_string()));
}
}
});
}
KeyCode::Char('n') | KeyCode::Char('N')
| KeyCode::Esc | KeyCode::Char('h') => {
self.popup = None;
}
_ => {}
}
return Ok(false);
}
Popup::Updating { status, .. } => {
if key.code == KeyCode::Esc {
if matches!(status, UpdatingStatus::Failed(_)) {
self.popup = None;
}
// Ignore Esc while downloading/applying
}
return Ok(false);
}
_ => {} _ => {}
} }
} }
@@ -354,24 +545,24 @@ impl App {
// ── Main menu navigation ───────────────────────────────────────── // ── Main menu navigation ─────────────────────────────────────────
match key.code { match key.code {
KeyCode::Char('q') => return Ok(true), KeyCode::Char('q') => return Ok(true),
KeyCode::Up => { KeyCode::Up | KeyCode::Char('k') => {
let len = self.current_items().len(); let len = self.current_items().len();
if len > 0 { if len > 0 {
self.selected_index = (self.selected_index + len - 1) % len; self.selected_index = (self.selected_index + len - 1) % len;
} }
} }
KeyCode::Down => { KeyCode::Down | KeyCode::Char('j') => {
let len = self.current_items().len(); let len = self.current_items().len();
if len > 0 { if len > 0 {
self.selected_index = (self.selected_index + 1) % len; self.selected_index = (self.selected_index + 1) % len;
} }
} }
KeyCode::Enter => { KeyCode::Enter | KeyCode::Char('l') => {
if let Some(item) = self.selected_item().cloned() { if let Some(item) = self.selected_item().cloned() {
self.activate_item(item).await?; self.activate_item(item).await?;
} }
} }
KeyCode::Esc => { KeyCode::Esc | KeyCode::Char('h') => {
if !self.breadcrumbs.is_empty() { if !self.breadcrumbs.is_empty() {
self.breadcrumbs.pop(); self.breadcrumbs.pop();
self.selected_index = 0; self.selected_index = 0;

View File

@@ -9,6 +9,8 @@ pub struct Config {
pub timeout_sec: u64, pub timeout_sec: u64,
#[serde(default = "default_theme")] #[serde(default = "default_theme")]
pub theme: Theme, pub theme: Theme,
#[serde(default)]
pub update_api: Option<String>,
} }
#[derive(Debug, Deserialize, Serialize, Clone)] #[derive(Debug, Deserialize, Serialize, Clone)]
@@ -29,9 +31,16 @@ fn default_theme() -> Theme {
impl Config { impl Config {
pub fn load() -> Result<Self> { pub fn load() -> Result<Self> {
let config_path = dirs::config_dir() // Respect XDG_CONFIG_HOME; fall back to ~/.config on all platforms.
.unwrap_or_else(|| PathBuf::from(".")) let config_base = std::env::var("XDG_CONFIG_HOME")
.join("tui-client") .map(PathBuf::from)
.unwrap_or_else(|_| {
dirs::home_dir()
.unwrap_or_else(|| PathBuf::from("."))
.join(".config")
});
let config_path = config_base
.join(env!("CARGO_PKG_NAME"))
.join("config.toml"); .join("config.toml");
if !config_path.exists() { if !config_path.exists() {
@@ -39,6 +48,7 @@ impl Config {
server_url: "http://localhost:8080/api/menu".to_string(), server_url: "http://localhost:8080/api/menu".to_string(),
timeout_sec: 10, timeout_sec: 10,
theme: default_theme(), theme: default_theme(),
update_api: None,
}; };
fs::create_dir_all(config_path.parent().unwrap())?; fs::create_dir_all(config_path.parent().unwrap())?;
fs::write(config_path, toml::to_string_pretty(&default)?)?; fs::write(config_path, toml::to_string_pretty(&default)?)?;

View File

@@ -6,6 +6,7 @@ mod executor;
mod menu; mod menu;
mod network; mod network;
mod ui; mod ui;
mod updater;
use std::io; use std::io;
use anyhow::Result; use anyhow::Result;
@@ -30,6 +31,7 @@ async fn main() -> Result<()> {
let (event_tx, mut event_rx) = mpsc::unbounded_channel(); let (event_tx, mut event_rx) = mpsc::unbounded_channel();
let mut app = app::App::new(config, event_tx.clone()); let mut app = app::App::new(config, event_tx.clone());
app.load_menu().await; app.load_menu().await;
app.check_update();
let res = run_app(&mut terminal, &mut app, &mut event_rx).await; let res = run_app(&mut terminal, &mut app, &mut event_rx).await;
@@ -71,6 +73,21 @@ async fn run_app(
break; break;
} }
// Check for pending restart (after update applied) — before pending_exec.
if let Some(exe_path) = app.pending_restart.take() {
disable_raw_mode()?;
execute!(
terminal.backend_mut(),
LeaveAlternateScreen,
DisableMouseCapture
)?;
terminal.show_cursor()?;
#[cfg(unix)]
updater::exec_updated(&exe_path);
// fallback for non-unix or if exec failed
break;
}
// After handling events, check if a script requested an exec. // After handling events, check if a script requested an exec.
if let Some(exec) = app.pending_exec.take() { if let Some(exec) = app.pending_exec.take() {
run_exec(terminal, exec).await?; run_exec(terminal, exec).await?;

113
src/ui.rs
View File

@@ -1,6 +1,6 @@
use crate::ansi; use crate::ansi;
use crate::executor; use crate::executor;
use crate::app::{App, MessageLevel, Popup}; use crate::app::{App, MessageLevel, Popup, UpdatingStatus};
use ratatui::{ use ratatui::{
layout::{Alignment, Constraint, Direction, Layout, Rect}, layout::{Alignment, Constraint, Direction, Layout, Rect},
style::{Color, Modifier, Style}, style::{Color, Modifier, Style},
@@ -69,7 +69,12 @@ fn render_description(f: &mut Frame, app: &App, area: Rect) {
fn render_popup(f: &mut Frame, popup: &mut Popup) { fn render_popup(f: &mut Frame, popup: &mut Popup) {
let area = f.area(); let area = f.area();
let popup_area = centered_rect(80, 90, area);
// Use a smaller area for update-related popups
let popup_area = match popup {
Popup::UpdateConfirm { .. } | Popup::Updating { .. } => centered_rect(50, 40, area),
_ => centered_rect(80, 90, area),
};
f.render_widget(Clear, popup_area); f.render_widget(Clear, popup_area);
@@ -235,6 +240,110 @@ fn render_popup(f: &mut Frame, popup: &mut Popup) {
f.render_widget(paragraph, popup_area); f.render_widget(paragraph, popup_area);
} }
Popup::UpdateConfirm { info } => {
let mut lines = vec![
Line::from(""),
Line::from(Span::raw(format!(
" {}{}",
info.current_version, info.new_version
))),
];
if info.size > 0 {
let mb = info.size as f64 / 1_048_576.0;
lines.push(Line::from(Span::raw(format!(" Размер: {:.1} МБ", mb))));
}
lines.push(Line::from(""));
lines.push(Line::from(Span::styled(
" [Y] Обновить [N / Esc] Пропустить",
Style::default().fg(Color::Yellow),
)));
let block = Block::default()
.borders(Borders::ALL)
.title(" Доступно обновление ")
.border_style(Style::default().fg(Color::Cyan));
let paragraph = Paragraph::new(lines)
.block(block)
.wrap(Wrap { trim: true });
f.render_widget(paragraph, popup_area);
}
Popup::Updating {
info,
downloaded,
status,
} => {
let (title, border_color) = match status {
UpdatingStatus::Downloading => (" Загрузка обновления… ", Color::Cyan),
UpdatingStatus::Applying => (" Применение обновления… ", Color::Yellow),
UpdatingStatus::Done => (" Обновление завершено ", Color::Green),
UpdatingStatus::Failed(_) => (" Ошибка обновления ", Color::Red),
};
let bar_width = popup_area.width.saturating_sub(4) as usize;
let progress_line = if info.size > 0 {
let percent = ((*downloaded as f64 / info.size as f64) * 100.0)
.min(100.0) as usize;
let filled = (percent * bar_width) / 100;
format!(
"[{}{}] {}%",
"".repeat(filled),
"".repeat(bar_width.saturating_sub(filled)),
percent
)
} else {
// Indeterminate: animate based on downloaded bytes
let pos = ((*downloaded / 4096) as usize) % bar_width.max(1);
let thumb = 4.min(bar_width);
let mut bar = vec!['░'; bar_width];
for i in pos..((pos + thumb).min(bar_width)) {
bar[i] = '█';
}
format!("[{}]", bar.iter().collect::<String>())
};
let status_text = match status {
UpdatingStatus::Downloading => {
if info.size > 0 {
let mb_done = *downloaded as f64 / 1_048_576.0;
let mb_total = info.size as f64 / 1_048_576.0;
format!(" {:.1} / {:.1} МБ", mb_done, mb_total)
} else {
let kb = *downloaded / 1024;
format!(" {} КБ загружено", kb)
}
}
UpdatingStatus::Applying => " Применяется…".to_string(),
UpdatingStatus::Done => " Обновление успешно установлено. Перезапуск…".to_string(),
UpdatingStatus::Failed(msg) => format!(" Ошибка: {}", msg),
};
let mut lines = vec![
Line::from(""),
Line::from(Span::raw(progress_line)),
Line::from(""),
Line::from(Span::raw(status_text)),
];
if matches!(status, UpdatingStatus::Failed(_)) {
lines.push(Line::from(""));
lines.push(Line::from(Span::styled(
" [Esc] Закрыть",
Style::default().fg(Color::Yellow),
)));
}
let block = Block::default()
.borders(Borders::ALL)
.title(title)
.border_style(Style::default().fg(border_color));
let paragraph = Paragraph::new(lines)
.block(block)
.wrap(Wrap { trim: true });
f.render_widget(paragraph, popup_area);
}
_ => {} _ => {}
} }
} }

172
src/updater.rs Normal file
View File

@@ -0,0 +1,172 @@
// src/updater.rs
use anyhow::{Context, Result};
use futures::StreamExt;
use serde::Deserialize;
use tokio::io::AsyncWriteExt;
use tokio::sync::mpsc::UnboundedSender;
#[derive(Debug, Clone)]
pub struct UpdateInfo {
pub current_version: String,
pub new_version: String,
pub download_url: String,
pub size: u64,
}
#[derive(Deserialize)]
struct Release {
tag_name: String,
prerelease: bool,
assets: Vec<ReleaseAsset>,
}
#[derive(Deserialize)]
struct ReleaseAsset {
name: String,
browser_download_url: String,
size: u64,
}
/// Checks Gitea releases API for latest stable (non-prerelease) release.
/// Returns Ok(None) if already up to date or no matching asset found.
pub async fn check(api_base: &str, timeout_sec: u64) -> Result<Option<UpdateInfo>> {
let url = format!("{}/releases?limit=10&page=1", api_base);
let client = reqwest::Client::builder()
.timeout(std::time::Duration::from_secs(timeout_sec))
.build()
.context("failed to build HTTP client")?;
let releases: Vec<Release> = client
.get(&url)
.send()
.await
.context("failed to fetch releases")?
.json()
.await
.context("failed to parse releases JSON")?;
let current_version = env!("CARGO_PKG_VERSION").to_string();
// Find the latest non-prerelease release
let latest = releases.into_iter().find(|r| !r.prerelease);
let release = match latest {
Some(r) => r,
None => return Ok(None),
};
// Strip "v" prefix and anything after "@" (e.g. "v1.0.0@master" → "1.0.0")
let tag = release.tag_name.as_str();
let tag = tag.strip_prefix('v').unwrap_or(tag);
let tag = tag.split('@').next().unwrap_or(tag).trim();
let new_version = tag.to_string();
// Already up to date
if new_version == current_version {
return Ok(None);
}
// Find a matching asset
let asset_prefix = format!(
"{}-{}-{}",
env!("CARGO_PKG_NAME"),
std::env::consts::OS,
std::env::consts::ARCH
);
let asset = release
.assets
.into_iter()
.find(|a| a.name.starts_with(&asset_prefix));
let asset = match asset {
Some(a) => a,
None => return Ok(None),
};
Ok(Some(UpdateInfo {
current_version,
new_version,
download_url: asset.browser_download_url,
size: asset.size,
}))
}
/// Downloads binary to temp file with streaming progress, atomically replaces
/// current exe, sets chmod 755.
/// Returns the path of the replaced executable (captured before rename so it
/// remains valid even after the old inode is marked "(deleted)" by the kernel).
pub async fn download_and_apply(
info: &UpdateInfo,
progress_tx: UnboundedSender<u64>,
) -> Result<std::path::PathBuf> {
let client = reqwest::Client::new();
let response = client
.get(&info.download_url)
.send()
.await
.context("failed to start download")?
.error_for_status()
.context("download request failed with error status")?;
// Write to a temp file next to the current exe so rename is atomic
let current_exe = std::env::current_exe().context("failed to get current exe path")?;
let exe_dir = current_exe
.parent()
.context("current exe has no parent directory")?;
let tmp_path = exe_dir.join(format!(".{}.tmp", env!("CARGO_PKG_NAME")));
let mut file = tokio::fs::File::create(&tmp_path)
.await
.context("failed to create temp file")?;
let mut stream = response.bytes_stream();
let mut downloaded: u64 = 0;
while let Some(chunk) = stream.next().await {
let chunk = chunk.context("error reading download chunk")?;
downloaded += chunk.len() as u64;
file.write_all(&chunk)
.await
.context("failed to write to temp file")?;
let _ = progress_tx.send(downloaded);
}
// Flush and close
drop(file);
// chmod 755
#[cfg(unix)]
{
use std::os::unix::fs::PermissionsExt;
let perms = std::fs::Permissions::from_mode(0o755);
std::fs::set_permissions(&tmp_path, perms).context("failed to set permissions")?;
}
// Atomically replace current exe.
// current_exe is captured BEFORE this rename — after rename, /proc/self/exe
// on Linux returns the path with " (deleted)" appended, but the PathBuf we
// hold still refers to the correct filesystem path of the new binary.
std::fs::rename(&tmp_path, &current_exe).context("failed to replace current exe")?;
Ok(current_exe)
}
/// Replaces current process via execv (Unix). Never returns on success.
/// `exe_path` must be the path captured *before* the binary was replaced —
/// do NOT call std::env::current_exe() here, it returns "(deleted)" on Linux.
#[cfg(unix)]
pub fn exec_updated(exe_path: &std::path::Path) -> ! {
use std::os::unix::process::CommandExt;
let args: Vec<String> = std::env::args().collect();
let err = std::process::Command::new(exe_path)
.args(&args[1..])
.exec();
eprintln!("Failed to exec updated binary: {}", err);
std::process::exit(1);
}