Skip to content

Commit

Permalink
Resize Broot when Vim is resized (#14)
Browse files Browse the repository at this point in the history
Fixes #13
  • Loading branch information
9999years authored Oct 11, 2023
1 parent 770dc79 commit 1385e63
Show file tree
Hide file tree
Showing 9 changed files with 187 additions and 92 deletions.
9 changes: 8 additions & 1 deletion flake.nix
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,14 @@

nativeBuildInputs = [
pkgs.neovim
pkgs.broot
(pkgs.broot.overrideAttrs {
patches = [
(pkgs.fetchpatch {
url = "https://github.com/Canop/broot/pull/758.diff";
hash = "sha256-TwZ6rOR0TVwCD/8hnrWZw4eFj2mybaK+OXzVHE8Gyho=";
})
];
})
pkgs.git
];

Expand Down
34 changes: 28 additions & 6 deletions lua/broot/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,17 @@ function M._mktemp()
return path
end

function M._window_size()
local height = vim.o.lines - vim.o.cmdheight
if vim.o.laststatus ~= 0 then
height = height - 1
end
return {
height = height,
width = vim.o.columns,
}
end

function M.broot(opts)
if opts == nil then
opts = {}
Expand All @@ -59,17 +70,14 @@ function M.broot(opts)
-- Don't warn when exiting the Broot buffer.
vim.api.nvim_buf_set_option(buffer_id, "modified", false)

local height = vim.o.lines - vim.o.cmdheight - 1
if vim.o.laststatus ~= 0 then
height = height - 1
end
local window_size = M._window_size()

local window_id = vim.api.nvim_open_win(buffer_id, true, {
relative = "editor",
row = 0,
col = 0,
width = vim.o.columns,
height = height,
width = window_size.width,
height = window_size.height,
style = "minimal",
})
if window_id == 0 then
Expand Down Expand Up @@ -108,6 +116,20 @@ function M.broot(opts)
error("Broot command is not executable: " .. M.broot_binary)
end
vim.cmd(":startinsert")

vim.api.nvim_create_augroup("Broot", {})
vim.api.nvim_create_autocmd("VimResized", {
buffer = buffer_id,
group = "Broot",
nested = true,
callback = function()
local window_resize = M._window_size()
vim.api.nvim_win_set_width(window_id, window_resize.width)
vim.api.nvim_win_set_height(window_id, window_resize.height)
-- Moving the cursor back to {1, 0} puts the window where we expect.
vim.api.nvim_win_set_cursor(window_id, { 1, 0 })
end,
})
end

function M._on_broot_exit(exit_code, window_id, buffer_id, cmd_path, out_path)
Expand Down
4 changes: 1 addition & 3 deletions scripts/mini_test_init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,7 @@ vim.opt.runtimepath:append("," .. vim.fn.getcwd() .. "," .. mini_nvim)
-- Set up 'mini.test'
require("mini.test").setup()

-- Set up 'broot.nvim'.
local broot = require("broot")
broot.setup {
require("broot").setup {
config_files = {
vim.fn.fnamemodify("tests/data/conf.toml", ":p"),
vim.fn.fnamemodify("tests/data/nvim.toml", ":p"),
Expand Down
6 changes: 6 additions & 0 deletions scripts/pre_case.lua
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
require("broot").setup {
config_files = {
vim.fn.fnamemodify("tests/data/conf.toml", ":p"),
vim.fn.fnamemodify("tests/data/nvim.toml", ":p"),
},
}
3 changes: 3 additions & 0 deletions tests/data/conf.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,5 @@
# No Git info.
default_flags = "G"
# Print the root as a relative path so it doesn't matter what directory you run
# the tests in.
root_relative_path = true
51 changes: 51 additions & 0 deletions tests/screenshots/tests-test_broot.lua---broot---can_resize
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
--|---------|---------|---------|---------|
01|.
02|├──conf.toml
03|├──doggy
04|├──flavors
05|│ ├──goldie
06|│ ├──samoyed
07|│ └──weenie
08|├──nvim.toml
09|└──puppy
10|
11|
12|
13|
14|
15|
16|
17|
18|
19|
20|
21| Hit e…r to go up, ? for h…, …l…s to s…h
22| h:n gi:y
23|[No Name]
24|-- TERMINAL -- 1,1 Top

--|---------|---------|---------|---------|
01|0111111111111111111111111111111111111111
02|2223333333333333333333333333333333333333
03|2223333333333333333333333333333333333333
04|2224444444333333333333333333333333333333
05|2222223333333333333333333333333333333333
06|2222223333333333333333333333333333333333
07|2222223333333333333333333333333333333333
08|2223333333333333333333333333333333333333
09|2223333333333333333333333333333333333333
10|3333333333333333333333333333333333333333
11|3333333333333333333333333333333333333333
12|3333333333333333333333333333333333333333
13|3333333333333333333333333333333333333333
14|3333333333333333333333333333333333333333
15|3333333333333333333333333333333333333333
16|3333333333333333333333333333333333333333
17|3333333333333333333333333333333333333333
18|3333333333333333333333333333333333333333
19|3333333333333333333333333333333333333333
20|3333333333333333333333333333333333333333
21|5555567655555555555655555575575755555575
22|8999999999999999999999999999:::;:::::;:9
23|<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
24|==============>>>>>>>>>>>>>>>>>>>>>>>>>>
96 changes: 48 additions & 48 deletions tests/screenshots/tests-test_broot.lua---broot---can_use_directory
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@
--|---------|---------|---------|---------|---------|---------|---------|---------|
01|goldie
02|~
03|~
04|~
05|~
06|~
07|~
08|~
09|~
10|~
11|~
12|~
13|~
14|~
15|~
16|~
17|~
18|~
19|~
20|~
21|~
22|~
23|goldie
24|
01|.
02|├──goldie
03|├──samoyed
04|└──weenie
05|
06|
07|
08|
09|
10|
11|
12|
13|
14|
15|
16|
17|
18|
19|
20|
21| Hit enter to go up, ? for help, or a few letters to search
22| h:n gi:y
23|[No Name]
24|-- TERMINAL -- 1,0-1 All

--|---------|---------|---------|---------|---------|---------|---------|---------|
01|00000000000000000000000000000000000000000000000000000000000000000000000000000000
02|11111111111111111111111111111111111111111111111111111111111111111111111111111111
03|11111111111111111111111111111111111111111111111111111111111111111111111111111111
04|11111111111111111111111111111111111111111111111111111111111111111111111111111111
05|11111111111111111111111111111111111111111111111111111111111111111111111111111111
06|11111111111111111111111111111111111111111111111111111111111111111111111111111111
07|11111111111111111111111111111111111111111111111111111111111111111111111111111111
08|11111111111111111111111111111111111111111111111111111111111111111111111111111111
09|11111111111111111111111111111111111111111111111111111111111111111111111111111111
10|11111111111111111111111111111111111111111111111111111111111111111111111111111111
11|11111111111111111111111111111111111111111111111111111111111111111111111111111111
12|11111111111111111111111111111111111111111111111111111111111111111111111111111111
13|11111111111111111111111111111111111111111111111111111111111111111111111111111111
14|11111111111111111111111111111111111111111111111111111111111111111111111111111111
15|11111111111111111111111111111111111111111111111111111111111111111111111111111111
16|11111111111111111111111111111111111111111111111111111111111111111111111111111111
17|11111111111111111111111111111111111111111111111111111111111111111111111111111111
18|11111111111111111111111111111111111111111111111111111111111111111111111111111111
19|11111111111111111111111111111111111111111111111111111111111111111111111111111111
20|11111111111111111111111111111111111111111111111111111111111111111111111111111111
21|11111111111111111111111111111111111111111111111111111111111111111111111111111111
22|11111111111111111111111111111111111111111111111111111111111111111111111111111111
23|22222222222222222222222222222222222222222222222222222222222222222222222222222222
24|33333333333333333333333333333333333333333333333333333333333333333333333333333333
01|01111111111111111111111111111111111111111111111111111111111111111111111111111111
02|22233333333333333333333333333333333333333333333333333333333333333333333333333333
03|22233333333333333333333333333333333333333333333333333333333333333333333333333333
04|22233333333333333333333333333333333333333333333333333333333333333333333333333333
05|33333333333333333333333333333333333333333333333333333333333333333333333333333333
06|33333333333333333333333333333333333333333333333333333333333333333333333333333333
07|33333333333333333333333333333333333333333333333333333333333333333333333333333333
08|33333333333333333333333333333333333333333333333333333333333333333333333333333333
09|33333333333333333333333333333333333333333333333333333333333333333333333333333333
10|33333333333333333333333333333333333333333333333333333333333333333333333333333333
11|33333333333333333333333333333333333333333333333333333333333333333333333333333333
12|33333333333333333333333333333333333333333333333333333333333333333333333333333333
13|33333333333333333333333333333333333333333333333333333333333333333333333333333333
14|33333333333333333333333333333333333333333333333333333333333333333333333333333333
15|33333333333333333333333333333333333333333333333333333333333333333333333333333333
16|33333333333333333333333333333333333333333333333333333333333333333333333333333333
17|33333333333333333333333333333333333333333333333333333333333333333333333333333333
18|33333333333333333333333333333333333333333333333333333333333333333333333333333333
19|33333333333333333333333333333333333333333333333333333333333333333333333333333333
20|33333333333333333333333333333333333333333333333333333333333333333333333333333333
21|44444555554444444444454444444444444444444444444444444444444444444444444444444444
22|67777777777777777777777777777777777777777777777777777777777777777777888988888983
23|::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::
24|;;;;;;;;;;;;;;<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
62 changes: 31 additions & 31 deletions tests/screenshots/tests-test_broot.lua---broot---can_use_extra_args
Original file line number Diff line number Diff line change
@@ -1,33 +1,33 @@
--|---------|---------|---------|---------|---------|---------|---------|---------|
01|sammy
02|~
03|~
04|~
05|~
06|~
07|~
08|~
09|~
10|~
11|~
12|~
13|~
14|~
15|~
16|~
17|~
18|~
19|~
20|~
21|~
22|~
23|samoyed
24|
01|.
02|└──flavors
03| └──samoyed sammy
04|
05|
06|
07|
08|
09|
10|
11|
12|
13|
14|
15|
16|
17|
18|
19|
20|
21| Hit Right to open, alt-Enter to open and quit, esc to clear the filter
22|c/sam h:n gi:y
23|[No Name]
24|-- TERMINAL -- 1,0-1 All

--|---------|---------|---------|---------|---------|---------|---------|---------|
01|00000000000000000000000000000000000000000000000000000000000000000000000000000000
02|11111111111111111111111111111111111111111111111111111111111111111111111111111111
03|11111111111111111111111111111111111111111111111111111111111111111111111111111111
01|01111111111111111111111111111111111111111111111111111111111111111111111111111111
02|22200000001111111111111111111111111111111111111111111111111111111111111111111111
03|33333344444444556665544444444444444444444444444444444444444444444444444444444444
04|11111111111111111111111111111111111111111111111111111111111111111111111111111111
05|11111111111111111111111111111111111111111111111111111111111111111111111111111111
06|11111111111111111111111111111111111111111111111111111111111111111111111111111111
Expand All @@ -45,7 +45,7 @@
18|11111111111111111111111111111111111111111111111111111111111111111111111111111111
19|11111111111111111111111111111111111111111111111111111111111111111111111111111111
20|11111111111111111111111111111111111111111111111111111111111111111111111111111111
21|11111111111111111111111111111111111111111111111111111111111111111111111111111111
22|11111111111111111111111111111111111111111111111111111111111111111111111111111111
23|22222222222222222222222222222222222222222222222222222222222222222222222222222222
24|33333333333333333333333333333333333333333333333333333333333333333333333333333333
21|77777888887777777777888888888777777777777777777788877777777777777777777777777777
22|99999:99999999999999999999999999999999999999999999999999999999999999;;;<;;;;;<;1
23|================================================================================
24|>>>>>>>>>>>>>>??????????????????????????????????????????????????????????????????
14 changes: 11 additions & 3 deletions tests/test_broot.lua
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,6 @@ end
broot_test["can_use_directory"] = function()
eq(child.lua_get([[M.broot{directory = "flavors"}]]), vim.NIL)
vim.loop.sleep(250)
child.type_keys(100, "gol", "<CR>")
vim.loop.sleep(250)
expect.reference_screenshot(child.get_screenshot())
end

Expand All @@ -52,7 +50,17 @@ end
broot_test["can_use_extra_args"] = function()
eq(child.lua_get([[M.broot{extra_args = {"--cmd", "c/sam"}}]]), vim.NIL)
vim.loop.sleep(250)
child.type_keys("<CR>")
expect.reference_screenshot(child.get_screenshot())
end

broot_test["can_resize"] = function()
child.o.lines = 35
child.o.columns = 100
eq(child.lua_get([[M.broot()]]), vim.NIL)
vim.loop.sleep(250)

child.o.lines = 24
child.o.columns = 40
vim.loop.sleep(250)
expect.reference_screenshot(child.get_screenshot())
end
Expand Down

0 comments on commit 1385e63

Please sign in to comment.