Skip to content

Commit 68ab024

Browse files
Madman10Kalehander92
authored andcommitted
refactor(tui): use paths.rs in tui
1 parent 142e67c commit 68ab024

File tree

7 files changed

+62
-103
lines changed

7 files changed

+62
-103
lines changed

src/tui/Cargo.lock

Lines changed: 4 additions & 85 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/tui/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ serde_repr = "0.1"
1919
notify = "6.1.1"
2020
futures = "0.3.30"
2121
futures-timer = "3.0.3"
22-
tempfile = "3.20.0"
2322
ratatui = "0.29.0"
2423
log = "0.4.27"
2524
env_logger = "0.11.8"

src/tui/src/core.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,7 @@ use crate::task::{gen_task_id, to_event_kind, to_task_kind_text, EventId, TaskId
1313
use serde::Serialize;
1414
use tokio;
1515
use tokio::sync::mpsc;
16-
17-
pub const CODETRACER_TMP_PATH: &str = "/tmp/codetracer";
16+
use crate::paths::CODETRACER_PATHS;
1817

1918
#[derive(Debug, Default)]
2019
pub struct Core {
@@ -44,7 +43,8 @@ impl Core {
4443
}
4544

4645
fn run_dir(&self) -> PathBuf {
47-
PathBuf::from(CODETRACER_TMP_PATH).join(format!("run-{}", self.caller_process_pid))
46+
let tmp_path = CODETRACER_PATHS.lock().unwrap().tmp_path.clone();
47+
tmp_path.join(format!("run-{}", self.caller_process_pid))
4848
}
4949

5050
fn ensure_arg_path_for(&self, task_id: TaskId) -> Result<PathBuf, Box<dyn Error>> {

src/tui/src/dap_client.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@ use std::os::unix::net::UnixStream;
66
use std::process::{Child, Command};
77
use tokio::sync::mpsc;
88

9+
//mod paths;
10+
use crate::paths::CODETRACER_PATHS;
11+
912
pub struct DapClient {
1013
child: Child,
1114
reader: BufReader<UnixStream>,
@@ -16,7 +19,12 @@ pub struct DapClient {
1619
impl DapClient {
1720
pub fn start(server_bin: &str) -> Result<Self, Box<dyn Error>> {
1821
let pid = std::process::id();
19-
let socket_path = format!("/tmp/ct_dap_socket_{pid}");
22+
let tmp_path =
23+
{
24+
CODETRACER_PATHS.lock()?.tmp_path.clone()
25+
};
26+
let tmp_path_str = tmp_path.display();
27+
let socket_path = format!("{tmp_path_str}/ct_dap_socket_{pid}");
2028
let _ = std::fs::remove_file(&socket_path);
2129

2230
let child = if server_bin.ends_with("dlv") {

src/tui/src/main.rs

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -35,18 +35,19 @@ mod status_component;
3535
mod task;
3636
mod value;
3737
mod window;
38+
mod paths;
3839

3940
use crate::dap_client::DapClient;
4041
use component::Component;
41-
use core::{caller_process_pid, Core, CODETRACER_TMP_PATH};
42+
use core::{caller_process_pid, Core};
4243
use editor_component::EditorComponent;
4344
use event::{CtEvent, Event};
4445
use lang::Lang;
4546
use panel::{coord, panel, size};
46-
use serde_json::Value;
4747
use state_component::StateComponent;
4848
use status_component::StatusComponent;
4949
use task::{Action, EventId, EventKind, FlowUpdate, Location, MoveState, StepArg, TaskKind};
50+
use paths::CODETRACER_PATHS;
5051

5152
#[derive(Debug, Default)]
5253
pub struct Trace {
@@ -99,9 +100,6 @@ impl Default for App {
99100
}
100101
}
101102

102-
const CT_SOCKET_PATH: &str = "/tmp/ct_socket";
103-
const CT_CLIENT_SOCKET_PATH: &str = "/tmp/ct_client_socket";
104-
105103
const EDITOR_COMPONENT: usize = 0;
106104

107105
impl App {
@@ -274,8 +272,18 @@ impl App {
274272
fn process_incoming_messages(&mut self) {}
275273

276274
fn start_core(&mut self) -> Result<(), Box<dyn Error>> {
277-
let last_start_pid_path = format!("{CODETRACER_TMP_PATH}/last-start-pid");
278-
let _ = std::fs::create_dir(CODETRACER_TMP_PATH);
275+
let tmp_path: PathBuf;
276+
let socket_path_tmp: PathBuf;
277+
{
278+
let paths = CODETRACER_PATHS.lock()?;
279+
tmp_path = paths.tmp_path.clone();
280+
socket_path_tmp = paths.socket_path.clone();
281+
}
282+
283+
let tmp_socket_path_str = socket_path_tmp.display();
284+
285+
let last_start_pid_path = format!("{tmp_socket_path_str}/last-start-pid");
286+
let _ = std::fs::create_dir(tmp_path);
279287
std::fs::write(
280288
last_start_pid_path,
281289
format!("{}\n", self.caller_process_pid),
@@ -286,13 +294,9 @@ impl App {
286294
env::set_var("CODETRACER_DISPATCHER_READ_CLIENT", "STDIN");
287295
env::set_var("CODETRACER_DISPATCHER_SEND_CLIENT", "FILE");
288296

289-
// https://stackoverflow.com/a/73224567/438099
290-
// let file = std::fs::File::create("/tmp/codetracer/out.txt").unwrap();
291-
// let file_out_stdio = Stdio::from(file);
292-
293297
let caller_pid = self.caller_process_pid;
294298

295-
let socket_path = format!("{CT_SOCKET_PATH}_{caller_pid}");
299+
let socket_path = format!("{tmp_socket_path_str}_{caller_pid}");
296300
eprintln!("{socket_path:?}");
297301
let socket = UnixStream::connect(&socket_path)?;
298302
eprintln!("{socket:?}");
@@ -648,7 +652,11 @@ mod tests {
648652

649653
#[test]
650654
fn register_trace_in_db() {
651-
let base = env::temp_dir().join("ct_tui_test_db");
655+
let tmp_path =
656+
{
657+
CODETRACER_PATHS.lock()?.tmp_path.clone()
658+
};
659+
let base = tmp_path.join("ct_tui_test_db");
652660
let _ = fs::remove_dir_all(&base);
653661
fs::create_dir_all(&base).unwrap();
654662
let temp_home = base.join("home");

src/tui/src/paths.rs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
use std::env;
2+
use std::path::PathBuf;
3+
use std::sync::{LazyLock, Mutex};
4+
5+
pub struct Paths {
6+
pub tmp_path: PathBuf,
7+
pub socket_path: PathBuf,
8+
}
9+
10+
impl Default for Paths {
11+
fn default() -> Self {
12+
let tmpdir: PathBuf = if cfg!(target_os = "macos") {
13+
PathBuf::from(env::var("HOME").unwrap_or("/".to_string())).join("Library/Caches/com.codetracer.CodeTracer/")
14+
} else {
15+
PathBuf::from(env::temp_dir()).join("codetracer/")
16+
};
17+
Self {
18+
tmp_path: PathBuf::from(&tmpdir),
19+
socket_path: PathBuf::from(&tmpdir).join("ct_socket"),
20+
}
21+
}
22+
}
23+
24+
pub static CODETRACER_PATHS: LazyLock<Mutex<Paths>> = LazyLock::new(|| Mutex::new(Paths::default()));

src/tui/src/simple_main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ fn with_line_numbers(lines: &[String]) -> Vec<String> {
2828
}
2929

3030
mod dap_client;
31+
mod paths;
3132
use dap_client::DapClient;
3233

3334
#[derive(Debug)]

0 commit comments

Comments
 (0)