Skip to content

Commit

Permalink
feat: add responsive capabilities for horizontal direction (#618)
Browse files Browse the repository at this point in the history
* add responsiveness options

* update readme

* fix code style

* address PR comments

* address PR comments
  • Loading branch information
searleser97 authored Nov 1, 2024
1 parent 137d06f commit db7607c
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,12 @@ require("toggleterm").setup{
return term.name
end
},
responsiveness = {
-- breakpoint in terms of `vim.o.columns` at which terminals will start to stack on top of each other
-- instead of next to each other
-- default = 0 which means the feature is turned off
horizontal_breakpoint = 135,
}
}
```

Expand Down
7 changes: 7 additions & 0 deletions lua/toggleterm/config.lua
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ local function shade(color, factor) return colors.shade_color(color, factor) end
---@field name_formatter fun(term: Terminal):string
---@field enabled boolean

--- @class Responsiveness
--- @field horizontal_breakpoint number

--- @class ToggleTermConfig
--- @field size number
--- @field shade_filetypes string[]
Expand All @@ -37,6 +40,7 @@ local function shade(color, factor) return colors.shade_color(color, factor) end
--- @field winbar WinbarOpts
--- @field autochdir boolean
--- @field title_pos '"left"' | '"center"' | '"right"'
--- @field responsiveness Responsiveness

---@type ToggleTermConfig
local config = {
Expand Down Expand Up @@ -65,6 +69,9 @@ local config = {
winblend = 0,
title_pos = "left",
},
responsiveness = {
horizontal_breakpoint = 0,
},
}

---Derive the highlights for a toggleterm and merge these with the user's preferences
Expand Down
9 changes: 8 additions & 1 deletion lua/toggleterm/ui.lua
Original file line number Diff line number Diff line change
Expand Up @@ -224,6 +224,7 @@ end
local split_commands = {
horizontal = {
existing = "rightbelow vsplit",
existing_stacked = "rightbelow split",
new = "botright split",
resize = "resize",
},
Expand Down Expand Up @@ -316,7 +317,13 @@ function M.open_split(size, term)
local split_win = windows[#windows]
if config.persist_size then M.save_window_size(term.direction, split_win.window) end
api.nvim_set_current_win(split_win.window)
vim.cmd(commands.existing)
local window_width = vim.o.columns
local horizontal_breakpoint = config.responsiveness.horizontal_breakpoint
if term.direction == "horizontal" and window_width < horizontal_breakpoint then
vim.cmd(commands.existing_stacked)
else
vim.cmd(commands.existing)
end
else
vim.cmd(commands.new)
end
Expand Down
2 changes: 1 addition & 1 deletion stylua.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ column_width = 100
indent_type = "Spaces"
quote_style = "AutoPreferDouble"
indent_width = 2
collapse_simple_statement = 'Always'
collapse_simple_statement = "Always"

0 comments on commit db7607c

Please sign in to comment.