Skip to content

Commit

Permalink
Add config for terminal_columns
Browse files Browse the repository at this point in the history
  • Loading branch information
erichlf committed Apr 23, 2024
1 parent 71917e1 commit 7d26395
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 10 deletions.
2 changes: 2 additions & 0 deletions lua/devcontainer_cli/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ local default_config = {
dotfiles_targetPath = "~/dotfiles",
-- command that's executed for installed the dependencies from the setup_environment_repo
dotfiles_installCommand = "install.sh",
-- The number of columns to wrap text at
terminal_columns = 80,
}

local options
Expand Down
5 changes: 1 addition & 4 deletions lua/devcontainer_cli/devcontainer_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@ local folder_utils = require("devcontainer_cli.folder_utils")

local M = {}

local terminal_columns = 80 -- number of columns for displaying text

-- window management variables
local prev_win = -1
local win = -1
Expand Down Expand Up @@ -146,8 +144,7 @@ function M.bringup(cwd)
if config.interactive then
vim.ui.input(
{prompt=windows_utils.wrap_text(
"Spawning devcontainer with command: " .. command,
terminal_columns
"Spawning devcontainer with command: " .. command
) .. "\n\n" .. "Press q to cancel or any other key to continue\n"
},
function(input)
Expand Down
18 changes: 12 additions & 6 deletions lua/devcontainer_cli/windows_utils.lua
Original file line number Diff line number Diff line change
@@ -1,15 +1,19 @@
local config = require("devcontainer_cli.config")

local M = {}

-- wrao the given text at max_width
-- number of columns for displaying text
local terminal_columns = config.terminal_columns

-- wrap the given text at max_width
-- @param text the text to wrap
-- @param max_width the width at which to wrap text
-- @return the text wrapped
function M.wrap_text(text, max_width)
function M.wrap_text(text)
local wrapped_lines = {}
for line in text:gmatch("[^\n]+") do
local current_line = ""
for word in line:gmatch("%S+") do
if #current_line + #word <= max_width then
if #current_line + #word <= terminal_columns then
current_line = current_line .. word .. " "
else
table.insert(wrapped_lines, current_line)
Expand All @@ -31,7 +35,9 @@ function M.open_floating_window(on_detach)
vim.api.nvim_buf_set_keymap(buf, 'n', 'q', '<CMD>close<CR>', {})
vim.api.nvim_buf_set_keymap(buf, 'n', '<esc>', '<CMD>close<CR>', {})

local width = math.ceil(math.min(vim.o.columns, math.max(80, vim.o.columns - 20)))
local width = math.ceil(
math.min(vim.o.columns, math.max(terminal_columns, vim.o.columns - 20))
)
local height = math.ceil(math.min(vim.o.lines, math.max(20, vim.o.lines - 10)))

local row = math.ceil(vim.o.lines - height) * 0.5 - 1
Expand Down Expand Up @@ -61,7 +67,7 @@ end
-- @param text the text to send
-- @param buffer the buffer to send text to
function M.send_text(text, buffer)
local text = vim.split(wrap_text(text, 80), "\n")
local text = vim.split(wrap_text(text), "\n")

-- Set the content of the buffer
vim.api.nvim_buf_set_lines(buffer, 0, -1, false, text)
Expand Down

0 comments on commit 7d26395

Please sign in to comment.