Skip to content

Commit

Permalink
fix: Update the title on file changes (#274)
Browse files Browse the repository at this point in the history
  • Loading branch information
CosmicHorrorDev authored Mar 16, 2024
1 parent 073a160 commit f2c7d83
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 49 deletions.
60 changes: 11 additions & 49 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,39 +143,6 @@ pub struct Inlyne {
watcher: Watcher,
}

/// Gets a relative path extending from the repo root falling back to the full path
fn root_filepath_to_vcs_dir(path: &Path) -> Option<PathBuf> {
let mut full_path = path.canonicalize().ok()?;
let mut parts = vec![full_path.file_name()?.to_owned()];

full_path.pop();
loop {
full_path.push(".git");
let is_git = full_path.exists();
full_path.pop();
full_path.push(".hg");
let is_mercurial = full_path.exists();
full_path.pop();

let is_vcs_dir = is_git || is_mercurial;

match full_path.file_name() {
Some(name) => parts.push(name.to_owned()),
// We've searched the full path and didn't find a vcs dir
None => return Some(path.to_owned()),
}
if is_vcs_dir {
let mut rooted = PathBuf::new();
for part in parts.into_iter().rev() {
rooted.push(part);
}
return Some(rooted);
}

full_path.pop();
}
}

impl Inlyne {
pub fn new(opts: Opts) -> anyhow::Result<Self> {
let keycombos = KeyCombos::new(opts.keybindings.clone())?;
Expand All @@ -184,10 +151,7 @@ impl Inlyne {

let event_loop = EventLoopBuilder::<InlyneEvent>::with_user_event().build();
let window = Arc::new(Window::new(&event_loop).unwrap());
match root_filepath_to_vcs_dir(&file_path) {
Some(path) => window.set_title(&format!("Inlyne - {}", path.to_string_lossy())),
None => window.set_title("Inlyne"),
}
window.set_title(&utils::format_title(&file_path));
let renderer = pollster::block_on(Renderer::new(
&window,
opts.theme.clone(),
Expand Down Expand Up @@ -281,6 +245,12 @@ impl Inlyne {
self.interpreter_sender.send(contents).unwrap();
}

fn update_file(&mut self, path: &Path, contents: String) {
self.window.set_title(&utils::format_title(path));
self.watcher.update_file(path, contents);
self.renderer.set_scroll_y(0.0);
}

pub fn run(mut self) {
let mut pending_resize = None;
let mut scrollbar_held = None;
Expand Down Expand Up @@ -518,12 +488,8 @@ impl Inlyne {
} else {
match read_to_string(&path) {
Ok(contents) => {
self.watcher.update_file(
&path,
contents,
);
self.update_file(&path, contents);
self.opts.history.make_next(path);
self.renderer.set_scroll_y(0.);
}
Err(err) => {
tracing::warn!(
Expand Down Expand Up @@ -632,17 +598,13 @@ impl Inlyne {
let changed_path = match hist_dir {
HistDirection::Next => self.opts.history.next(),
HistDirection::Prev => self.opts.history.previous(),
};
}.map(ToOwned::to_owned);
let Some(file_path) = changed_path else {
return;
};
match read_to_string(file_path) {
match read_to_string(&file_path) {
Ok(contents) => {
self.watcher.update_file(
file_path,
contents,
);
self.renderer.set_scroll_y(0.0);
self.update_file(&file_path, contents);
}
Err(err) => {
tracing::warn!(
Expand Down
41 changes: 41 additions & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::collections::HashMap;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::{Arc, Mutex, OnceLock};

use crate::image::ImageData;
Expand All @@ -14,6 +15,46 @@ use syntect::highlighting::{Theme as SyntectTheme, ThemeSet as SyntectThemeSet};
use syntect::parsing::SyntaxSet;
use winit::window::CursorIcon;

pub fn format_title(file_path: &Path) -> String {
match root_filepath_to_vcs_dir(file_path) {
Some(path) => format!("Inlyne - {}", path.to_string_lossy()),
None => "Inlyne".to_owned(),
}
}

/// Gets a relative path extending from the repo root falling back to the full path
fn root_filepath_to_vcs_dir(path: &Path) -> Option<PathBuf> {
let mut full_path = path.canonicalize().ok()?;
let mut parts = vec![full_path.file_name()?.to_owned()];

full_path.pop();
loop {
full_path.push(".git");
let is_git = full_path.exists();
full_path.pop();
full_path.push(".hg");
let is_mercurial = full_path.exists();
full_path.pop();

let is_vcs_dir = is_git || is_mercurial;

match full_path.file_name() {
Some(name) => parts.push(name.to_owned()),
// We've searched the full path and didn't find a vcs dir
None => return Some(path.to_owned()),
}
if is_vcs_dir {
let mut rooted = PathBuf::new();
for part in parts.into_iter().rev() {
rooted.push(part);
}
return Some(rooted);
}

full_path.pop();
}
}

pub fn client() -> Client {
const USER_AGENT: &str = concat!(
"inlyne ",
Expand Down

0 comments on commit f2c7d83

Please sign in to comment.