zsh/my_zsh_func.zsh
2025-12-23 12:46:07 +03:00

142 lines
5.4 KiB
Bash
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Start the tmux session if not alraedy in the tmux session
# if [[ ! -n $TMUX ]]; then
# # Get the session IDs
# session_ids="$(tmux list-sessions)"
# # Create new session if no sessions exist
# if [[ -z "$session_ids" ]]; then
# tmux new-session
# fi
# # Select from following choices
# # - Attach existing session
# # - Create new session
# # - Start without tmux
# create_new_session="Create new session"
# start_without_tmux="Start without tmux"
# choices="$session_ids\n${create_new_session}:\n${start_without_tmux}:"
# choice="$(echo $choices | fzf | cut -d: -f1)"
# selected_name=$(basename "$choice" | tr . _)
# if expr "$choice" : "[0-9a-zA-Z]*$" >&/dev/null; then
# # if expr "$choice" : "[0-9]*$" >&/dev/null; then
# # Attach existing session
# tmux attach-session -t "$choice"
# elif [[ "$choice" = "${create_new_session}" ]]; then
# # Create new session
# tmux new-session
# elif [[ "$choice" = "${start_without_tmux}" ]]; then
# # Start without tmux
# :
# fi
# fi
switch_or_create_tmux_session() {
# Если передан один аргумент, используем его
if [[ $# -eq 1 ]]; then
selected=$1
else
# Используем fzf для выбора директории
selected=$(find ~/.config ~/projects -mindepth 1 -maxdepth 1 -type d | fzf)
fi
# Если ничего не выбрано, выходим
if [[ -z $selected ]]; then
return 0
fi
# Получаем имя для сессии tmux, заменяя точки на подчеркивания
selected_name=$(basename "$selected" | tr . _)
# Проверяем, запущен ли tmux
tmux_running=$(pgrep tmux)
# Если tmux не запущен и мы не в сессии tmux, создаем новую сессию
if [[ -z $TMUX ]] && [[ -z $tmux_running ]]; then
tmux new-session -s $selected_name -c $selected
return 0
fi
# Создаем новую сессию, если такой еще нет
if ! tmux has-session -t=$selected_name 2> /dev/null; then
tmux new-session -ds $selected_name -c $selected
fi
# Переключаемся на существующую сессию, если она есть, или подключаемся к ней
if ! tmux switch-client -t $selected_name 2> /dev/null; then
tmux attach-session -t $selected_name
fi
}
zle -N switch_or_create_tmux_session_widget switch_or_create_tmux_session
# Function for cd and showing including files.
cd() {
# Переход в указанный каталог
builtin cd "$1"
# Печать полного пути к текущему каталогу
# Цвет текста:
BLACK='\033[0;30m' # ${BLACK} # чёрный цвет знаков
RED='\033[0;31m' # ${RED} # красный цвет знаков
GREEN='\033[0;32m' # ${GREEN} # зелёный цвет знаков
YELLOW='\033[0;33m' # ${YELLOW} # желтый цвет знаков
BLUE='\033[0;34m' # ${BLUE} # синий цвет знаков
MAGENTA='\033[0;35m' # ${MAGENTA} # фиолетовый цвет знаков
CYAN='\033[0;36m' # ${CYAN} # цвет морской волны знаков
GRAY='\033[0;37m' # ${GRAY} # серый цвет знаков
LRED='\033[1;31m'
NC='\033[0m'
echo "${MAGENTA}Current directory${NC}: ${YELLOW}$(pwd)${NC}"
# # Вывод содержимого каталога
# gls -aCAGF --color=always --group-directories-first
# eza -aAGF --color=always --group-directories-first
ls -aAGF --color=always
}
# Function for creating a new note in Obsidian.
cnn() {
# Основная директория с заметками для Obsidian
local notes_base_dir="$HOME/Library/Mobile Documents/iCloud~md~obsidian/Documents/Main"
# Проверяем, существует ли основная директория
if [ ! -d "$notes_base_dir" ]; then
echo "Папка с заметками не найдена: $notes_base_dir"
return 1
fi
# Добавляем пункт для создания новой папки в меню выбора
local selected_dir=$(echo -e "Создать новую папку\n$(find "$notes_base_dir" -mindepth 1 -maxdepth 1 -type d)" | fzf --prompt="Выберите папку")
# Если выбран пункт "Создать новую папку"
if [[ "$selected_dir" == "Создать новую папку" ]]; then
echo "Введите название новой папки:"
read new_dir_name
selected_dir="$notes_base_dir/$new_dir_name"
mkdir -p "$selected_dir"
echo "Создана новая папка: $selected_dir"
fi
# Если не выбрана папка и не создана новая
if [ -z "$selected_dir" ]; then
echo "Папка не выбрана. Отмена создания заметки."
return 1
fi
# Если аргумент передан, используем его как имя файла, иначе используем текущую дату и время
local file_name
if [ -n "$1" ]; then
file_name="$1"
else
file_name=$(date "+%Y-%m-%d-%H%M%S")
fi
local note_file="$selected_dir/$file_name.md"
# Создаем новый Markdown файл
touch "$note_file"
# Открываем файл в nvim с obsidian.nvim
nvim "$note_file"
}