forked from 1jehuang/jcode
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.rs
More file actions
78 lines (72 loc) · 2.18 KB
/
utils.rs
File metadata and controls
78 lines (72 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use crate::session::GitState;
use std::path::Path;
use std::process::Command;
use super::Agent;
pub(super) fn trace_enabled() -> bool {
match std::env::var("JCODE_TRACE") {
Ok(value) => {
let value = value.trim();
!value.is_empty() && value != "0" && value.to_lowercase() != "false"
}
Err(_) => false,
}
}
pub(super) fn git_state_for_dir(dir: &Path) -> Option<GitState> {
let root = git_output(dir, &["rev-parse", "--show-toplevel"])?;
let head = git_output(dir, &["rev-parse", "HEAD"]);
let branch = git_output(dir, &["rev-parse", "--abbrev-ref", "HEAD"]);
let dirty = git_output(dir, &["status", "--porcelain"]).map(|out| !out.is_empty());
Some(GitState {
root,
head,
branch,
dirty,
})
}
fn git_output(dir: &Path, args: &[&str]) -> Option<String> {
let output = Command::new("git")
.args(args)
.current_dir(dir)
.output()
.ok()?;
if !output.status.success() {
return None;
}
Some(String::from_utf8_lossy(&output.stdout).trim().to_string())
}
impl Agent {
pub(super) fn update_generated_image_side_panel(
&self,
id: &str,
path: &str,
metadata_path: Option<&str>,
output_format: &str,
revised_prompt: Option<&str>,
) -> Option<crate::side_panel::SidePanelSnapshot> {
match crate::tui::write_generated_image_side_panel_page(
&self.session.id,
id,
path,
metadata_path,
output_format,
revised_prompt,
) {
Ok(snapshot) => {
crate::bus::Bus::global().publish(crate::bus::BusEvent::SidePanelUpdated(
crate::bus::SidePanelUpdated {
session_id: self.session.id.clone(),
snapshot: snapshot.clone(),
},
));
Some(snapshot)
}
Err(err) => {
crate::logging::warn(&format!(
"Failed to write generated image side panel page: {}",
err
));
None
}
}
}
}