Skip to content

Commit 0cc1e09

Browse files
committed
ref(logger): don't require win to be passed around
1 parent e0235a7 commit 0cc1e09

File tree

8 files changed

+93
-119
lines changed

8 files changed

+93
-119
lines changed

src/compile.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -253,9 +253,9 @@ pub async fn ensure_server_support<'a>(
253253
if proc_exists(pid, || {}) {
254254
let mut logger = client.new_logger("Compile Error", name, &None);
255255
logger.set_running().await.ok();
256-
let ref win = Some(logger.open_win().await?);
257-
logger.log(err.to_string(), win).await.ok();
258-
logger.set_status_end(false, true).await.ok();
256+
logger.open_win().await.ok();
257+
logger.log(err.to_string()).await.ok();
258+
logger.set_status_end(false, false).await.ok();
259259
}
260260
}
261261

src/daemon/requests/build.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ impl Handler for Build {
2727
let direction = self.direction.clone();
2828

2929
let args = append_build_root(&root, config.as_args())?;
30-
let (success, _) = nvim
30+
let success = nvim
3131
.new_logger("Build", &config.target, &direction)
3232
.log_build_stream(&root, &args, true, true)
3333
.await?;

src/daemon/requests/drop.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -23,25 +23,21 @@ impl Handler for Drop {
2323

2424
if state.clients.contains_key(&client.pid) {
2525
tracing::info!("Drop({}: {})", client.pid, client.abbrev_root());
26-
2726
// NOTE: Should only be Some if no more client depend on it
2827
if let Some(project) = state.projects.remove(&client).await? {
2928
// NOTE: Remove project watchers
3029
state.watcher.remove_project_watcher(&client).await;
31-
3230
// NOTE: Remove target watchers associsated with root
3331
state
3432
.watcher
3533
.remove_target_watcher_for_root(&project.root)
3634
.await;
3735
}
38-
3936
// NOTE: Try removing client with given pid
4037
if remove_client {
4138
tracing::debug!("RemoveClient({})", client.pid);
4239
state.clients.remove(&client.pid);
4340
}
44-
4541
// NOTE: Sink state to all client vim.g.xbase.state
4642
state.sync_client_state().await?;
4743
}

src/daemon/requests/run.rs

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@ use super::*;
22
use crate::{
33
nvim::BufferDirection,
44
types::{BuildConfiguration, Platform},
5-
util::string_as_section,
65
};
76

87
#[cfg(feature = "daemon")]
98
use {
10-
crate::{constants::DAEMON_STATE, types::SimDevice, xcode::append_build_root, Error},
9+
crate::{
10+
constants::DAEMON_STATE, types::SimDevice, util::string_as_section,
11+
xcode::append_build_root, Error,
12+
},
1113
std::str::FromStr,
1214
tokio_stream::StreamExt,
1315
xcodebuild::runner,
@@ -63,7 +65,7 @@ impl Handler for Run {
6365
let settings = runner::build_settings(&root, &args).await?;
6466
let platform = platform.unwrap_or(Platform::from_str(&settings.platform_display_name)?);
6567

66-
let (success, ref win) = nvim
68+
let success = nvim
6769
.new_logger("Build", &config.target, &direction)
6870
.log_build_stream(&root, &args, true, true)
6971
.await?;
@@ -81,6 +83,8 @@ impl Handler for Run {
8183
tracing::debug!("Running binary {program:?}");
8284

8385
logger.log_title().await?;
86+
logger.open_win().await?;
87+
8488
tokio::spawn(async move {
8589
let mut stream = runner::run(program).await?;
8690

@@ -89,18 +93,16 @@ impl Handler for Run {
8993
while let Some(update) = stream.next().await {
9094
let state = DAEMON_STATE.clone();
9195
let state = state.lock().await;
92-
9396
let nvim = state.clients.get(&pid)?;
9497
let mut logger = nvim.new_logger("Run", &config.target, &direction);
95-
let ref win = Some(logger.open_win().await?);
9698

9799
// NOTE: NSLog get directed to error by default which is odd
98100
match update {
99101
Stdout(msg) => {
100-
logger.log(msg, win).await?;
102+
logger.log(msg).await?;
101103
}
102104
Error(msg) | Stderr(msg) => {
103-
logger.log(format!("[Error] {msg}"), win).await?;
105+
logger.log(format!("[Error] {msg}")).await?;
104106
}
105107
Exit(ref code) => {
106108
let success = code == "0";
@@ -110,8 +112,8 @@ impl Handler for Run {
110112
format!("Panic {code}")
111113
});
112114

113-
logger.log(msg, win).await?;
114-
logger.set_status_end(success, win.is_none()).await?;
115+
logger.log(msg).await?;
116+
logger.set_status_end(success, true).await?;
115117
}
116118
}
117119
}
@@ -125,23 +127,22 @@ impl Handler for Run {
125127
tracing::debug!("{app_id}: {:?}", path_to_app);
126128

127129
logger.log_title().await?;
130+
128131
tokio::spawn(async move {
129132
// NOTE: This is required so when neovim exist this should also exit
130133
let state = DAEMON_STATE.clone().lock_owned().await;
131134
let nvim = state.clients.get(&pid)?;
132135
let ref mut logger = nvim.new_logger("Run", &config.target, &direction);
133-
let ref win = Some(logger.open_win().await?);
134136

137+
logger.open_win().await?;
135138
logger.set_running().await?;
136139

137-
device.try_boot(logger, win).await?;
138-
device
139-
.try_install(&path_to_app, &app_id, logger, win)
140-
.await?;
141-
device.try_launch(&app_id, logger, win).await?;
140+
device.try_boot(logger).await?;
141+
device.try_install(&path_to_app, &app_id, logger).await?;
142+
device.try_launch(&app_id, logger).await?;
142143

143144
// TODO: Remove and repalce with app logs
144-
logger.set_status_end(true, win.is_none()).await?;
145+
logger.set_status_end(true, false).await?;
145146

146147
let mut state = DAEMON_STATE.clone().lock_owned().await;
147148
state.devices.insert(device);
@@ -151,7 +152,7 @@ impl Handler for Run {
151152
}
152153

153154
let msg = format!("Unable to run `{}` under `{platform}`", config.target);
154-
logger.log(msg.clone(), win).await?;
155+
logger.log(msg.clone()).await?;
155156
Err(Error::Run(msg))
156157
}
157158
}

src/nvim/buffer.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[cfg(feature = "daemon")]
22
use super::NvimClient;
33
#[cfg(feature = "daemon")]
4-
use anyhow::{Context, Result};
4+
use crate::Result;
55
use serde::{Deserialize, Serialize};
66

77
#[derive(Clone, Debug, strum::EnumString, Serialize, Deserialize)]
@@ -37,14 +37,21 @@ impl BufferDirection {
3737
return Ok(direction.to_nvim_command(bufnr));
3838
};
3939

40-
"return require'xbase.config'.values.default_log_buffer_direction"
40+
match "return require'xbase.config'.values.default_log_buffer_direction"
4141
.pipe(|str| nvim.exec_lua(str, vec![]))
4242
.await?
4343
.as_str()
4444
.ok_or_else(|| anyhow::anyhow!("Unable to covnert value to string"))?
45-
.pipe(BufferDirection::from_str)
45+
.pipe(Self::from_str)
4646
.map(|d| d.to_nvim_command(bufnr))
47-
.context("Convert to string to direction")
47+
{
48+
Ok(open_command) => open_command,
49+
Err(e) => {
50+
tracing::error!("Unable to convert value to string {e}");
51+
Self::Horizontal.to_nvim_command(bufnr)
52+
}
53+
}
54+
.pipe(Ok)
4855
}
4956
}
5057

src/nvim/logger.rs

Lines changed: 49 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ use crate::nvim::BufferDirection;
33
use crate::Result;
44
use nvim_rs::{Buffer, Window};
55
use std::path::Path;
6-
use tap::Pipe;
76
use tokio_stream::StreamExt;
87

98
pub struct Logger<'a> {
@@ -28,7 +27,7 @@ impl<'a> Logger<'a> {
2827
}
2928
}
3029

31-
async fn clear(&self) -> Result<()> {
30+
async fn clear_content(&self) -> Result<()> {
3231
self.buf.set_lines(0, -1, false, vec![]).await?;
3332
Ok(())
3433
}
@@ -44,20 +43,7 @@ impl<'a> Logger<'a> {
4443
})
4544
}
4645

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-
}
58-
}
59-
60-
pub async fn log(&mut self, msg: String, win: &Option<NvimWindow>) -> Result<()> {
46+
pub async fn log(&mut self, msg: String) -> Result<()> {
6147
tracing::debug!("{msg}");
6248

6349
let mut c = self.get_line_count().await?;
@@ -74,7 +60,7 @@ impl<'a> Logger<'a> {
7460

7561
self.current_line_count = Some(c);
7662

77-
if let Some(win) = win {
63+
if let Some(win) = self.win().await {
7864
win.set_cursor((c, 0)).await?;
7965
}
8066

@@ -87,23 +73,21 @@ impl<'a> Logger<'a> {
8773
args: &Vec<String>,
8874
clear: bool,
8975
open: bool,
90-
) -> Result<(bool, Option<Window<NvimConnection>>)> {
76+
) -> Result<bool> {
9177
let mut stream = crate::xcode::stream_build(root, args).await?;
9278

9379
// TODO(nvim): close log buffer if it is open for new direction
9480
//
9581
// Currently the buffer direction will be ignored if the buffer is opened already
9682

9783
if clear {
98-
self.clear().await?;
84+
self.clear_content().await?;
9985
}
10086

10187
// TODO(nvim): build log correct height
102-
let win = if open {
103-
Some(self.open_win().await?)
104-
} else {
105-
None
106-
};
88+
if open {
89+
self.open_win().await?;
90+
}
10791

10892
let mut success = false;
10993

@@ -113,51 +97,54 @@ impl<'a> Logger<'a> {
11397
while let Some(line) = stream.next().await {
11498
line.contains("Succeed").then(|| success = true);
11599

116-
self.log(line, &win).await?;
100+
self.log(line).await?;
117101
}
118102

119-
self.set_status_end(success, open).await?;
103+
self.set_status_end(success, true).await?;
120104

121-
Ok((success, win))
105+
Ok(success)
122106
}
123107

124108
pub async fn log_title(&mut self) -> Result<()> {
125-
self.log(self.title.clone(), &None).await?;
109+
self.log(self.title.clone()).await?;
126110
Ok(())
127111
}
128112

129-
async fn open_cmd(&self) -> String {
130-
let open_cmd = match self.open_cmd.as_ref() {
131-
Some(s) => s.clone(),
132-
None => self.get_open_cmd(None).await,
133-
};
134-
open_cmd
135-
}
136-
137-
pub async fn open_win(&self) -> Result<Window<NvimConnection>> {
138-
let (mut window, windows) = (None, self.nvim.list_wins().await?);
139-
113+
/// Get window if it's available
114+
pub async fn win(&self) -> Option<NvimWindow> {
115+
let windows = self.nvim.list_wins().await.ok()?;
140116
for win in windows.into_iter() {
141-
let buf = win.get_buf().await?;
142-
if buf.get_number().await? == self.nvim.log_bufnr {
143-
window = win.into();
117+
let buf = win.get_buf().await.ok()?;
118+
if buf.get_number().await.ok()? == self.nvim.log_bufnr {
119+
return Some(win);
144120
}
145121
}
122+
None
123+
}
146124

147-
if let Some(win) = window {
148-
win
149-
} else {
150-
let open_cmd = self.open_cmd().await;
151-
self.nvim.exec(&open_cmd, false).await?;
152-
let win = self.nvim.get_current_win().await?;
153-
// NOTE: This doesn't work
154-
win.set_option("number", false.into()).await?;
155-
win.set_option("relativenumber", false.into()).await?;
156-
// self.nvim.exec("setl nu nornu so=9", false).await?;
157-
self.nvim.exec("wincmd w", false).await?;
158-
win
125+
/// Open Window
126+
pub async fn open_win(&self) -> Result<Window<NvimConnection>> {
127+
if let Some(win) = self.win().await {
128+
return Ok(win);
159129
}
160-
.pipe(Ok)
130+
131+
tracing::info!("Openning a new window");
132+
133+
let open_cmd = match self.open_cmd.as_ref() {
134+
Some(s) => s.clone(),
135+
None => {
136+
BufferDirection::get_window_direction(self.nvim, None, self.nvim.log_bufnr).await?
137+
}
138+
};
139+
self.nvim.exec(&open_cmd, false).await?;
140+
let win = self.nvim.get_current_win().await?;
141+
// NOTE: This doesn't work
142+
win.set_option("number", false.into()).await?;
143+
win.set_option("relativenumber", false.into()).await?;
144+
// self.nvim.exec("setl nu nornu so=9", false).await?;
145+
self.nvim.exec("wincmd w", false).await?;
146+
147+
Ok(win)
161148
}
162149

163150
pub async fn set_running(&mut self) -> Result<()> {
@@ -168,6 +155,7 @@ impl<'a> Logger<'a> {
168155
}
169156

170157
pub async fn set_status_end(&mut self, success: bool, open: bool) -> Result<()> {
158+
let win = self.win().await;
171159
if success {
172160
self.nvim
173161
.exec("let g:xbase_watch_build_status='success'", false)
@@ -176,16 +164,13 @@ impl<'a> Logger<'a> {
176164
self.nvim
177165
.exec("let g:xbase_watch_build_status='failure'", false)
178166
.await?;
179-
if !open {
180-
self.nvim.exec(&self.open_cmd().await, false).await?;
181-
self.nvim
182-
.get_current_win()
183-
.await?
184-
.set_cursor((self.get_line_count().await?, 0))
185-
.await?;
186-
self.nvim.exec("call feedkeys('zt')", false).await?;
187-
}
188167
}
168+
169+
if open && win.is_none() {
170+
self.open_win().await?;
171+
self.nvim.exec("call feedkeys('zt')", false).await?;
172+
}
173+
189174
Ok(())
190175
}
191176
}

src/types/client.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ impl Client {
2020

2121
#[cfg(feature = "lua")]
2222
use {crate::util::mlua::LuaExtension, mlua::prelude::*, tap::Pipe};
23+
2324
#[cfg(feature = "lua")]
2425
impl Client {
2526
/// Derive client from lua value

0 commit comments

Comments
 (0)