Skip to content

Commit 4345ae4

Browse files
committed
fix(logger): force scroll
1 parent c545b73 commit 4345ae4

File tree

4 files changed

+39
-17
lines changed

4 files changed

+39
-17
lines changed

CHANGELOG.md

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,11 @@
22
## [unreleased]
33
### <!-- 1 -->Bug Fixes
44
- `(Logger)` <a href="https://github.com/tami5/xbase/commit/25a4e56"> Incorrect title for build once requests</a>
5+
- `(Logger)` <a href="https://github.com/tami5/xbase/commit/db147a9"> Force scroll</a>
56
### <!-- 2 -->Refactor
6-
- `(Logger)` <a href="https://github.com/tami5/xbase/commit/32e9555"> Append logger title to all msgs</a>
7+
- `(Logger)` <a href="https://github.com/tami5/xbase/commit/f32fd6b"> Append logger title to all msgs</a>
8+
### <!-- 3 -->Enhancement
9+
- `(Logger)` <a href="https://github.com/tami5/xbase/commit/c545b73"> Output readability and traceability</a>
710

811
## [0.1.0] - 2022-05-26
912
### <!-- 0 -->Features

lua/xbase/init.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
local M = {}
2-
local pid = vim.fn.getpid()
32

43
vim.g.xbase = {
54
---@type Project[]
@@ -59,8 +58,9 @@ local function try_map(key, fun)
5958
end
6059

6160
M.toggle_log_buffer = function(vsplit)
62-
local bufnr = vim.g.xbase.clients[string.format("%s", pid)].log_bufnr
61+
local bufnr = vim.g.xbase_log_bufnr
6362
local win = vim.fn.win_findbuf(bufnr)[1]
63+
6464
if win then
6565
vim.api.nvim_win_close(win, false)
6666
end

src/nvim/logger.rs

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,10 @@ impl<'a> Logger<'a> {
4141
})
4242
}
4343

44+
// TODO(logger): always show current new logs in middle of the window
4445
pub async fn append(&mut self, msg: String) -> Result<()> {
4546
tracing::debug!("{msg}");
47+
let win_info = self.win().await;
4648

4749
let mut c = self.get_line_count().await?;
4850
let lines = msg
@@ -58,28 +60,41 @@ impl<'a> Logger<'a> {
5860

5961
self.current_line_count = Some(c);
6062

61-
if let Some(win) = self.win().await {
62-
win.set_cursor((c, 0)).await?;
63+
if let Some((focused, win)) = win_info {
64+
if !focused {
65+
// self.nvim.exec("call feedkeys('zt')", false).await?;
66+
win.set_cursor((c, 0)).await?;
67+
} else {
68+
let (current, _) = win.get_cursor().await?;
69+
let diff = c - current;
70+
if diff == 1 || diff == 2 {
71+
// self.nvim.exec("call feedkeys('zt')", false).await?;
72+
win.set_cursor((c, 0)).await?;
73+
}
74+
}
6375
}
6476

6577
Ok(())
6678
}
6779

68-
/// Get window if it's available
69-
pub async fn win(&self) -> Option<NvimWindow> {
80+
/// Get logger window if it's available and whether is currently focused.
81+
pub async fn win(&self) -> Option<(bool, NvimWindow)> {
7082
let windows = self.nvim.list_wins().await.ok()?;
7183
for win in windows.into_iter() {
7284
let buf = win.get_buf().await.ok()?;
85+
7386
if buf.get_number().await.ok()? == self.nvim.log_bufnr {
74-
return Some(win);
87+
let curr = self.nvim.get_current_win().await.ok()?;
88+
let is_focused = curr.get_number().await.ok()? == win.get_number().await.ok()?;
89+
return Some((is_focused, win));
7590
}
7691
}
7792
None
7893
}
7994

8095
/// Open Window
8196
pub async fn open_win(&mut self) -> Result<Window<NvimConnection>> {
82-
if let Some(win) = self.win().await {
97+
if let Some((_, win)) = self.win().await {
8398
return Ok(win);
8499
}
85100

@@ -90,14 +105,18 @@ impl<'a> Logger<'a> {
90105
self.open_cmd = Some(v);
91106
};
92107

93-
let open_cmd = self.open_cmd.as_ref().unwrap();
94-
95-
self.nvim.exec(open_cmd, false).await?;
108+
// TODO(nvim): setup autocmd for buffer type
109+
let setup_script = format!(
110+
r#"
111+
{}
112+
setlocal nonumber norelativenumber
113+
setlocal scrolloff=3
114+
"#,
115+
self.open_cmd.as_ref().unwrap()
116+
);
117+
118+
self.nvim.exec(&setup_script, false).await?;
96119
let win = self.nvim.get_current_win().await?;
97-
// NOTE: This doesn't work
98-
win.set_option("number", false.into()).await?;
99-
win.set_option("relativenumber", false.into()).await?;
100-
// self.nvim.exec("setl nu nornu so=9", false).await?;
101120
self.nvim.exec("wincmd w", false).await?;
102121

103122
Ok(win)

src/run/handler.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#![allow(dead_code)]
2-
use crate::{client::Client, constants::DAEMON_STATE, util::fmt, Result};
2+
use crate::{client::Client, constants::DAEMON_STATE, Result};
33
use process_stream::{Process, StreamExt};
44
use tokio::task::JoinHandle;
55

0 commit comments

Comments
 (0)