Skip to content

Commit 34665ad

Browse files
committed
Document the code
1 parent 9c9221d commit 34665ad

File tree

4 files changed

+70
-14
lines changed

4 files changed

+70
-14
lines changed

lua/tmux-switch/commands.lua

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ local tmux = require("tmux-switch.tmux")
22

33
local M = {}
44

5+
-- Registers custom user commands for tmux operations
6+
--
7+
-- This function creates three custom commands in Neovim:
8+
-- - `TmuxSwitch`: Opens the tmux session switcher.
9+
-- - `TmuxCreateSession`: Creates a new tmux session with a specified name.
10+
-- - `TmuxRenameSession`: Renames the current tmux session.
11+
--
12+
-- @return nil
513
function M.register()
614
vim.api.nvim_create_user_command("TmuxSwitch", function()
715
tmux.switch()

lua/tmux-switch/tmux.lua

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,36 @@ local util = require("tmux-switch.util")
33

44
local M = {}
55

6+
-- Switches to a different tmux session
7+
--
8+
-- This function retrieves the available tmux sessions and displays a picker
9+
-- for the user to select a session to switch to.
10+
--
11+
-- @return nil
612
function M.switch()
713
local tmux_sessions = util.get_tmux_sessions()
814
ui.show_tmux_session_picker(tmux_sessions)
915
end
1016

17+
-- Creates a new tmux session
18+
--
19+
-- This function prompts the user to input a session name, creates a new tmux
20+
-- session with the given name, and switches to it.
21+
--
22+
-- @return nil
1123
function M.create_session()
1224
ui.create_input_prompt("[ TMUX switch ]", "", function(session_name)
1325
util.create_new_session(session_name)
1426
util.switch_to_session(session_name)
1527
end)
1628
end
1729

30+
-- Renames the current tmux session
31+
--
32+
-- This function prompts the user to rename the current tmux session. The prompt
33+
-- is pre-filled with the current session name for easy editing.
34+
--
35+
-- @return nil
1836
function M.rename_session()
1937
local current_session = util.get_current_tmux_session()
2038
ui.create_input_prompt("[ TMUX switch ]", current_session, function(new_session_name)

lua/tmux-switch/ui.lua

Lines changed: 12 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,10 @@
1-
local util = require("tmux-switch.util")
2-
3-
local actions = require("telescope.actions")
4-
local action_state = require("telescope.actions.state")
5-
local finders = require("telescope.finders")
6-
local pickers = require("telescope.pickers")
7-
local sorters = require("telescope.sorters")
8-
local themes = require("telescope.themes")
9-
10-
local nui_input = require("nui.input")
11-
local event = require("nui.utils.autocmd").event
12-
13-
local M = {}
14-
1+
--- Creates an input prompt with customizable text and a default value
2+
--
3+
-- @param prompt_text The text to display at the top of the input prompt
4+
-- @param default_value The default value for the input (optional)
5+
-- @param on_submit The function to call when the user submits the input
6+
--
7+
-- @return nil
158
function M.create_input_prompt(prompt_text, default_value, on_submit)
169
local input = nui_input({
1710
position = "50%",
@@ -45,6 +38,11 @@ function M.create_input_prompt(prompt_text, default_value, on_submit)
4538
end)
4639
end
4740

41+
--- Displays a tmux session picker using Telescope
42+
--
43+
-- @param tmux_sessions A list of tmux sessions to display in the picker
44+
--
45+
-- @return nil
4846
function M.show_tmux_session_picker(tmux_sessions)
4947
local opts = themes.get_dropdown({
5048
layout_config = { width = 50 },

lua/tmux-switch/util.lua

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
local M = {}
22

3+
-- Retrieves the list of available tmux sessions
4+
--
5+
-- This function runs a system command to list all tmux sessions and returns them
6+
-- as a table of session names.
7+
--
8+
-- @return A table containing the names of tmux sessions, or an empty table if an error occurs
39
function M.get_tmux_sessions()
410
local list_tmux_sessions_cmd = "tmux ls | awk -F ':' '{print $1}'"
511
local handle = io.popen(list_tmux_sessions_cmd)
@@ -20,20 +26,39 @@ function M.get_tmux_sessions()
2026
return tmux_sessions
2127
end
2228

29+
-- Switches to the specified tmux session
30+
--
31+
-- This function switches to the tmux session specified by the session name.
32+
--
33+
-- @param session_name The name of the tmux session to switch to
34+
--
35+
-- @return nil
2336
function M.switch_to_session(session_name)
2437
if session_name then
2538
local switch_to_session_cmd = string.format("tmux switch -t %s", session_name)
2639
vim.fn.system(switch_to_session_cmd)
2740
end
2841
end
2942

43+
-- Creates a new tmux session
44+
--
45+
-- This function creates a new tmux session with the specified session name.
46+
--
47+
-- @param session_name The name of the new tmux session to create
48+
--
49+
-- @return nil
3050
function M.create_new_session(session_name)
3151
if session_name and session_name ~= "" then
3252
local create_new_session_cmd = string.format("tmux new-session -d -s %s", session_name)
3353
vim.fn.system(create_new_session_cmd)
3454
end
3555
end
3656

57+
-- Retrieves the name of the current tmux session
58+
--
59+
-- This function runs a system command to get the name of the current tmux session.
60+
--
61+
-- @return The name of the current tmux session, or `nil` if an error occurs
3762
function M.get_current_tmux_session()
3863
local display_current_tmux_session_cmd = "tmux display-message -p '#S'"
3964
local handle = io.popen(display_current_tmux_session_cmd)
@@ -49,6 +74,13 @@ function M.get_current_tmux_session()
4974
return result:match("^%s*(.-)%s*$")
5075
end
5176

77+
-- Renames the current tmux session
78+
--
79+
-- This function renames the current tmux session to the new specified name.
80+
--
81+
-- @param new_session_name The new name for the current tmux session
82+
--
83+
-- @return nil
5284
function M.rename_current_session(new_session_name)
5385
local rename_current_session_cmd = string.format("tmux rename-session %s", new_session_name)
5486
vim.fn.system(rename_current_session_cmd)

0 commit comments

Comments
 (0)