Skip to content

Commit 73cbaf2

Browse files
committed
ref(daemon): improve nvim.logger interface
1 parent ec9a362 commit 73cbaf2

File tree

6 files changed

+131
-131
lines changed

6 files changed

+131
-131
lines changed

src/daemon/requests/build.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use crate::{nvim::BufferDirection, types::BuildConfiguration};
33
use std::fmt::Debug;
44

55
#[cfg(feature = "daemon")]
6-
use crate::{constants::DAEMON_STATE, xcode::stream_build};
6+
use crate::constants::DAEMON_STATE;
77

88
/// Build a project.
99
#[derive(Debug, Serialize, Deserialize)]
@@ -20,7 +20,7 @@ impl Handler for Build {
2020
let Self { client, config, .. } = &self;
2121
let Client { pid, root } = client;
2222

23-
tracing::debug!("Handling build request {:#?}", self.config);
23+
tracing::debug!("Handling build request {:#?}", config);
2424

2525
let state = DAEMON_STATE.clone().lock_owned().await;
2626
let nvim = state
@@ -30,10 +30,16 @@ impl Handler for Build {
3030

3131
let direction = self.direction.clone();
3232

33-
nvim.new_logger("build", config, &direction)
34-
.log_stream(stream_build(&root, &config).await?, true, true)
33+
let (success, _) = nvim
34+
.new_logger("build", &config.target, &direction)
35+
.log_build_stream(&root, &config.as_args(), true, true)
3536
.await?;
3637

38+
if !success {
39+
nvim.echo_err(&format!("Failed: {} ", config.to_string()))
40+
.await?;
41+
};
42+
3743
Ok(())
3844
}
3945
}

src/nvim.rs

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ use anyhow::Result;
1717

1818
#[cfg(feature = "daemon")]
1919
type NvimConnection = Compat<tokio::io::WriteHalf<parity_tokio_ipc::Connection>>;
20+
#[cfg(feature = "daemon")]
21+
pub type NvimWindow = nvim_rs::Window<NvimConnection>;
2022

2123
#[derive(Deserialize, Serialize)]
2224
pub struct NvimClient {
@@ -40,13 +42,14 @@ impl NvimClient {
4042
let (nvim, _) = connect(address, Dummy::new()).await?;
4143
let buf = nvim.create_buf(false, true).await?;
4244
let log_bufnr = buf.get_number().await?;
45+
let script = format!("let g:xbase_log_bufnr={log_bufnr}");
4346

44-
buf.set_name("[xbase Logs]").await?;
45-
buf.set_option("filetype", "xcodebuildlog".into()).await?;
46-
47-
// NOTE: store log bufnr somewhere in vim state
48-
nvim.exec(&format!("let g:xbase_log_bufnr={log_bufnr}"), false)
49-
.await?;
47+
let (a, b, c) = tokio::join!(
48+
buf.set_name("[xbase Logs]"),
49+
buf.set_option("filetype", "xcodebuildlog".into()),
50+
nvim.exec(&script, false)
51+
);
52+
_ = (a?, b?, c?);
5053

5154
Ok(NvimClient {
5255
pid: *pid,
@@ -63,11 +66,16 @@ impl NvimClient {
6366

6467
pub fn new_logger<'a>(
6568
&'a self,
66-
title: &'a str,
67-
config: &'a crate::types::BuildConfiguration,
69+
ops: &str,
70+
target: &str,
6871
direction: &'a Option<BufferDirection>,
6972
) -> Logger<'a> {
70-
Logger::new(self, title, &config, direction.clone())
73+
use crate::util::string_as_section;
74+
Logger::new(
75+
self,
76+
string_as_section(format!("[{ops}: {target}]")),
77+
direction.clone(),
78+
)
7179
}
7280
}
7381

@@ -84,11 +92,9 @@ impl NvimClient {
8492

8593
Ok(())
8694
}
87-
8895
fn inner(&self) -> &nvim_rs::Neovim<NvimConnection> {
8996
self.conn.as_ref().unwrap()
9097
}
91-
9298
pub async fn log_info(&self, scope: &str, msg: impl ToString) -> Result<()> {
9399
self.log("info", scope, msg).await
94100
}
@@ -108,9 +114,8 @@ impl NvimClient {
108114
self.exec(msg, false).await?;
109115
Ok(())
110116
}
111-
112117
pub async fn echo_err(&self, msg: &str) -> Result<()> {
113-
Ok(self.err_write(msg).await?)
118+
Ok(self.err_writeln(msg).await?)
114119
}
115120
}
116121

src/nvim/logger.rs

Lines changed: 96 additions & 89 deletions
Original file line numberDiff line numberDiff line change
@@ -1,63 +1,92 @@
1-
use super::{NvimClient, NvimConnection};
1+
use std::path::Path;
2+
3+
use super::{NvimClient, NvimConnection, NvimWindow};
24
use crate::nvim::BufferDirection;
3-
use crate::types::BuildConfiguration;
45
use anyhow::Result;
56
use nvim_rs::{Buffer, Window};
6-
use tokio_stream::{Stream, StreamExt};
7+
use tokio_stream::StreamExt;
78

89
pub struct Logger<'a> {
910
pub nvim: &'a NvimClient,
10-
pub title: &'a str,
11-
pub request: &'a BuildConfiguration,
11+
pub title: String,
1212
pub buf: Buffer<NvimConnection>,
1313
open_cmd: Option<String>,
1414
current_line_count: Option<i64>,
1515
}
1616

1717
impl<'a> Logger<'a> {
18-
pub fn new(
19-
nvim: &'a NvimClient,
20-
title: &'a str,
21-
request: &'a BuildConfiguration,
22-
direction: Option<BufferDirection>,
23-
) -> Self {
18+
pub fn new(nvim: &'a NvimClient, title: String, direction: Option<BufferDirection>) -> Self {
2419
let buf = Buffer::new(nvim.log_bufnr.into(), nvim.inner().clone());
2520
let open_cmd = direction.map(|v| v.to_nvim_command(nvim.log_bufnr));
2621

2722
Self {
2823
nvim,
2924
title,
30-
request,
3125
buf,
3226
open_cmd,
3327
current_line_count: None,
3428
}
3529
}
3630

37-
pub async fn log(&mut self, msg: String) -> Result<()> {
38-
let c = self.line_count().await?;
31+
async fn clear(&self) -> Result<()> {
32+
self.buf.set_lines(0, -1, false, vec![]).await?;
33+
Ok(())
34+
}
3935

40-
self.buf.set_lines(c, c + 1, false, vec![msg]).await?;
41-
self.current_line_count = Some(c + 1);
36+
async fn get_line_count(&'a self) -> Result<i64> {
37+
Ok(if let Some(count) = self.current_line_count {
38+
count
39+
} else {
40+
match self.buf.line_count().await? {
41+
1 => 0,
42+
count => count,
43+
}
44+
})
45+
}
4246

43-
Ok(())
47+
async fn get_open_cmd(&self, direction: Option<BufferDirection>) -> String {
48+
let direction =
49+
BufferDirection::get_window_direction(self.nvim, direction, self.nvim.log_bufnr);
50+
51+
match direction.await {
52+
Ok(open_command) => open_command,
53+
Err(e) => {
54+
tracing::error!("Unable to convert value to string {e}");
55+
BufferDirection::Horizontal.to_nvim_command(self.nvim.log_bufnr)
56+
}
57+
}
4458
}
4559

46-
async fn clear(&self) -> Result<()> {
47-
self.buf.set_lines(0, -1, false, vec![]).await?;
60+
pub async fn log(&mut self, msg: String, win: &Option<NvimWindow>) -> Result<()> {
61+
let mut c = self.get_line_count().await?;
62+
let lines = msg
63+
.split("\n")
64+
.map(ToString::to_string)
65+
.collect::<Vec<String>>();
66+
let lines_len = lines.len() as i64;
67+
68+
self.buf
69+
.set_lines(c, c + lines_len as i64, false, lines)
70+
.await?;
71+
c += lines_len;
72+
73+
self.current_line_count = Some(c);
74+
75+
if let Some(win) = win {
76+
win.set_cursor((c, 0)).await?;
77+
}
78+
4879
Ok(())
4980
}
5081

51-
pub async fn log_stream<S>(&mut self, mut stream: S, clear: bool, open: bool) -> Result<()>
52-
where
53-
S: Stream<Item = String> + Unpin,
54-
{
55-
self.set_running().await?;
56-
57-
let title = format!(
58-
"[{}] ------------------------------------------------------",
59-
self.title
60-
);
82+
pub async fn log_build_stream<P: AsRef<Path>>(
83+
&mut self,
84+
root: P,
85+
args: &Vec<String>,
86+
clear: bool,
87+
open: bool,
88+
) -> Result<(bool, Option<Window<NvimConnection>>)> {
89+
let mut stream = crate::xcode::stream_build(root, args).await?;
6190

6291
// TODO(nvim): close log buffer if it is open for new direction
6392
//
@@ -74,22 +103,50 @@ impl<'a> Logger<'a> {
74103
None
75104
};
76105

77-
self.log(title).await?;
78-
79106
let mut success = false;
107+
108+
self.set_running().await?;
109+
80110
while let Some(line) = stream.next().await {
81111
line.contains("Succeed").then(|| success = true);
82-
self.log(line).await?;
83-
if open {
84-
win.as_ref()
85-
.unwrap()
86-
.set_cursor((self.line_count().await?, 0))
87-
.await?;
88-
}
112+
113+
self.log(line, &win).await?;
89114
}
90115

91116
self.set_status_end(success, open).await?;
92117

118+
Ok((success, win))
119+
}
120+
121+
async fn log_title(&mut self) -> Result<()> {
122+
self.log(self.title.clone(), &None).await?;
123+
Ok(())
124+
}
125+
126+
async fn open_cmd(&self) -> String {
127+
let open_cmd = match self.open_cmd.as_ref() {
128+
Some(s) => s.clone(),
129+
None => self.get_open_cmd(None).await,
130+
};
131+
open_cmd
132+
}
133+
134+
pub async fn open_win(&self) -> Result<Window<NvimConnection>> {
135+
let open_cmd = self.open_cmd().await;
136+
137+
self.nvim.exec(&open_cmd, false).await?;
138+
let win = self.nvim.get_current_win().await?;
139+
self.nvim.exec("setl nu nornu so=9", false).await?;
140+
self.nvim.exec("wincmd w", false).await?;
141+
142+
Ok(win)
143+
}
144+
145+
pub async fn set_running(&mut self) -> Result<()> {
146+
self.log_title().await?;
147+
self.nvim
148+
.exec("let g:xbase_watch_build_status='running'", false)
149+
.await?;
93150
Ok(())
94151
}
95152

@@ -107,61 +164,11 @@ impl<'a> Logger<'a> {
107164
self.nvim
108165
.get_current_win()
109166
.await?
110-
.set_cursor((self.line_count().await?, 0))
167+
.set_cursor((self.get_line_count().await?, 0))
111168
.await?;
112169
self.nvim.exec("call feedkeys('zt')", false).await?;
113170
}
114171
}
115172
Ok(())
116173
}
117-
118-
pub async fn set_running(&mut self) -> Result<()> {
119-
self.nvim
120-
.exec("let g:xbase_watch_build_status='running'", false)
121-
.await?;
122-
Ok(())
123-
}
124-
125-
pub async fn line_count(&'a self) -> Result<i64> {
126-
Ok(if let Some(count) = self.current_line_count {
127-
count
128-
} else {
129-
match self.buf.line_count().await? {
130-
1 => 0,
131-
count => count,
132-
}
133-
})
134-
}
135-
136-
async fn get_open_cmd(&self, direction: Option<BufferDirection>) -> String {
137-
let direction =
138-
BufferDirection::get_window_direction(self.nvim, direction, self.nvim.log_bufnr);
139-
140-
match direction.await {
141-
Ok(open_command) => open_command,
142-
Err(e) => {
143-
tracing::error!("Unable to convert value to string {e}");
144-
BufferDirection::Horizontal.to_nvim_command(self.nvim.log_bufnr)
145-
}
146-
}
147-
}
148-
149-
pub async fn open_win(&self) -> Result<Window<NvimConnection>> {
150-
let open_cmd = self.open_cmd().await;
151-
152-
self.nvim.exec(&open_cmd, false).await?;
153-
let win = self.nvim.get_current_win().await?;
154-
self.nvim.exec("setl nu nornu so=9", false).await?;
155-
self.nvim.exec("wincmd w", false).await?;
156-
157-
Ok(win)
158-
}
159-
160-
async fn open_cmd(&self) -> String {
161-
let open_cmd = match self.open_cmd.as_ref() {
162-
Some(s) => s.clone(),
163-
None => self.get_open_cmd(None).await,
164-
};
165-
open_cmd
166-
}
167174
}

src/store/clients.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl ClientStore {
5757
pub async fn echo_err(&self, root: &PathBuf, scope: &str, msg: &str) {
5858
let msg = format!("{scope}: {msg}");
5959
for client in self.get_clients_by_root(&root).await {
60-
client.echo_msg(&msg).await.ok();
60+
client.echo_err(&msg).await.ok();
6161
}
6262
}
6363

0 commit comments

Comments
 (0)