sync terminal, but fixes

This commit is contained in:
Penwing 2024-01-24 14:53:24 +01:00
parent ec1b855686
commit 89f71ddc06
4 changed files with 68 additions and 48 deletions

View file

@ -79,7 +79,7 @@ impl super::Calcifer {
ui.horizontal(|ui| { ui.horizontal(|ui| {
ui.style_mut().visuals.extreme_bg_color = bg_color; ui.style_mut().visuals.extreme_bg_color = bg_color;
let Self { command, .. } = self; let Self { command, .. } = self;
ui.colored_label(command_color.clone(), tools::format_path(&env::current_dir().expect("Could not find Shell Environnment"), 2)); ui.colored_label(command_color.clone(), tools::format_path(&env::current_dir().expect("Could not find Shell Environnment")));
let response = ui.add(egui::TextEdit::singleline(command).desired_width(f32::INFINITY).lock_focus(true)); let response = ui.add(egui::TextEdit::singleline(command).desired_width(f32::INFINITY).lock_focus(true));
if response.lost_focus() && ctx.input(|i| i.key_pressed(egui::Key::Enter)) { if response.lost_focus() && ctx.input(|i| i.key_pressed(egui::Key::Enter)) {
@ -95,7 +95,7 @@ impl super::Calcifer {
ui.horizontal_wrapped(|ui| { ui.horizontal_wrapped(|ui| {
ui.spacing_mut().item_spacing.y = 0.0; ui.spacing_mut().item_spacing.y = 0.0;
for entry in &self.command_history { for entry in &self.command_history {
ui.colored_label(command_color, format!("{}> {}", entry.env, entry.command)); ui.colored_label(command_color, format!("\n{} {}", entry.env, entry.command));
ui.end_row(); ui.end_row();
if entry.output != "" { if entry.output != "" {
ui.colored_label(entry_color, &entry.output); ui.colored_label(entry_color, &entry.output);

View file

@ -1 +1 @@
{"tabs":["/home/penwing/Documents/notes/victory2.txt","/home/penwing/Documents/projects/rust/calcifer/src/calcifer/code_editor/themes/sonokai.rs"],"theme":6} {"tabs":["/home/penwing/Documents/notes/victory2.txt","/home/penwing/Documents/projects/rust/calcifer/src/calcifer/code_editor/themes/sonokai.rs"],"theme":4}

View file

@ -14,6 +14,7 @@ const SAVE_PATH : &str = "calcifer_save.json";
const TIME_LABELS : [&str; 5] = ["settings", "tree", "terminal", "tabs", "content"]; const TIME_LABELS : [&str; 5] = ["settings", "tree", "terminal", "tabs", "content"];
const MAX_FPS : f32 = 30.0; const MAX_FPS : f32 = 30.0;
const PATH_ROOT : &str = "/home/penwing/Documents/"; const PATH_ROOT : &str = "/home/penwing/Documents/";
const DISPLAY_PATH_DEPTH : usize = 3;
fn main() -> Result<(), eframe::Error> { fn main() -> Result<(), eframe::Error> {

View file

@ -3,6 +3,7 @@ use crate::calcifer::code_editor::Syntax;
use eframe::egui; use eframe::egui;
use egui::text_edit::CCursorRange; use egui::text_edit::CCursorRange;
use serde::{Serialize, Deserialize}; use serde::{Serialize, Deserialize};
use crate::DISPLAY_PATH_DEPTH;
//pub mod themes; //pub mod themes;
pub mod search; pub mod search;
@ -101,27 +102,39 @@ impl Tab {
} }
} }
#[derive(Clone, PartialEq)]
pub struct CommandEntry { pub struct CommandEntry {
pub env : String, pub env: String,
pub command : String, pub command: String,
pub output : String, pub output: String,
pub error : String, pub error: String,
} }
impl CommandEntry {
impl Default for CommandEntry { pub fn new(command: String) -> Self {
fn default() -> Self { CommandEntry {
Self { env: format_path(&env::current_dir().expect("Could not find Shell Environnment")),
env: env::current_dir().expect("Could not find Shell Environnment").file_name().expect("Could not get Shell Environnment Name").to_string_lossy().to_string(), command,
command : "".into(), output: String::new(),
output : "".into(), error: String::new(),
error : "".into(),
} }
} }
pub fn run(&mut self) -> Self {
let output = Command::new("sh")
.arg("-c")
.arg(self.command.clone())
.output()
.expect("failed to execute process");
self.output = (&String::from_utf8_lossy(&output.stdout)).trim_end_matches('\n').to_string();
self.error = (&String::from_utf8_lossy(&output.stderr)).trim_end_matches('\n').to_string();
self.clone()
}
} }
#[derive(Serialize, Deserialize, Debug, PartialEq)] #[derive(Serialize, Deserialize, Debug, PartialEq)]
pub struct AppState { pub struct AppState {
pub tabs: Vec<PathBuf>, pub tabs: Vec<PathBuf>,
@ -177,31 +190,37 @@ pub fn to_syntax(language : &str) -> Syntax {
} }
pub fn run_command(cmd : String) -> CommandEntry { pub fn run_command(command: String) -> CommandEntry {
let mut entry = CommandEntry::default(); let mut entry = CommandEntry::new(command);
if &cmd[..2] == "cd" {
let path_append = cmd[3..].replace("~", "/home/penwing"); if entry.command.len() < 2 {
let path = Path::new(&path_append); return entry.run();
entry.command = cmd; }
if format!("{}", path.display()) != "/" { if &entry.command[..2] != "cd" {
if !env::set_current_dir(path).is_ok() { return entry.run()
entry.error = format!("Could not find path : {}", path.display());
}
}
} else {
let output = Command::new("sh")
.arg("-c")
.arg(cmd.clone())
.output()
.expect("failed to execute process");
entry.command = cmd;
entry.output = (&String::from_utf8_lossy(&output.stdout)).to_string();
entry.error = (&String::from_utf8_lossy(&output.stderr)).to_string();
} }
entry if entry.command.len() < 4 {
entry.error = "Invalid cd, should provide path".to_string();
return entry
}
let path_append = entry.command[3..].replace("~", "/home/penwing");
let path = Path::new(&path_append);
if format!("{}", path.display()) == "/" {
entry.error = "Root access denied".to_string();
return entry
}
if env::set_current_dir(path).is_ok() {
entry.output = format!("Moved to : {}", path.display());
} else {
entry.error = format!("Could not find path : {}", path.display());
}
return entry
} }
@ -221,18 +240,18 @@ pub fn sort_directories_first(a: &std::fs::DirEntry, b: &std::fs::DirEntry) -> O
} }
pub fn format_path(path: &Path, n_parents: usize) -> String { pub fn format_path(path: &Path) -> String {
let components: Vec<&OsStr> = path let components: Vec<&OsStr> = path
.components() .components()
.rev() .rev()
.take(n_parents + 1) // Take up to three components (root, parent, file/folder) .take(DISPLAY_PATH_DEPTH)
.filter_map(|component| match component { .filter_map(|component| match component {
Component::RootDir | Component::CurDir => None, Component::RootDir | Component::CurDir => None,
_ => Some(component.as_os_str()), _ => Some(component.as_os_str()),
}) })
.collect(); .collect();
format!("{}>", components.iter().rev().map(|&c| c.to_string_lossy()).collect::<Vec<_>>().join("/")) format!("{}>", components.iter().rev().map(|&c| c.to_string_lossy()).collect::<Vec<_>>().join("/"))
} }