aboutsummaryrefslogtreecommitdiff
path: root/rprt-engine/src/buffer.rs
diff options
context:
space:
mode:
authortslil <tslil@posteo.de>2025-10-17 20:35:53 +0100
committertslil <tslil@posteo.de>2025-10-17 23:48:42 +0100
commita775b8f56e5422e47c40927328baf3045c500a32 (patch)
treefc003854fb4bc7e3b0cb815034540536a5fd286e /rprt-engine/src/buffer.rs
parent1376a23bda5e6fe94148cb57ca1447907c6bb21e (diff)
Starting on RIIR, first steps have been whelming
Diffstat (limited to 'rprt-engine/src/buffer.rs')
-rw-r--r--rprt-engine/src/buffer.rs51
1 files changed, 51 insertions, 0 deletions
diff --git a/rprt-engine/src/buffer.rs b/rprt-engine/src/buffer.rs
new file mode 100644
index 0000000..f536054
--- /dev/null
+++ b/rprt-engine/src/buffer.rs
@@ -0,0 +1,51 @@
+use std::collections::hash_map::DefaultHasher;
+use std::hash::{Hash, Hasher};
+use std::process::Command;
+use std::time::{SystemTime, UNIX_EPOCH};
+
+pub type BufferID = usize;
+pub struct Buffer {
+ pub content: String,
+ pub name: String,
+}
+
+impl Buffer {
+ pub fn new(name: String, content: String) -> Self {
+ Self {
+ name,
+ content,
+ }
+ }
+
+ fn generate_shell_name(command: &str) -> String {
+ let mut hasher = DefaultHasher::new();
+ command.hash(&mut hasher);
+ let command_hash = hasher.finish();
+
+ let timestamp = match SystemTime::now().duration_since(UNIX_EPOCH) {
+ Ok(x) => x.as_secs(),
+ Err(_) => 0,
+ };
+
+ format!("shell_{:x}_{}", command_hash, timestamp)
+ }
+
+ pub fn from_shell_command(command: &str) -> Self {
+ let name = Self::generate_shell_name(command);
+
+ let output = Command::new("sh").arg("-c").arg(command).output();
+
+ let content = match output {
+ Ok(output) => match String::from_utf8(output.stdout) {
+ Ok(s) => s,
+ Err(e) => format!("UTF-8 decode error: {}", e),
+ },
+ Err(e) => format!("Command failed: {}", e),
+ };
+
+ Self {
+ name,
+ content,
+ }
+ }
+}