2025-12-26 17:45:37 +05:00
|
|
|
|
package fields
|
|
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
|
"bubble/setup/ui"
|
2026-01-20 17:40:21 +05:00
|
|
|
|
"bubble/setup/utils"
|
2025-12-26 17:45:37 +05:00
|
|
|
|
"fmt"
|
|
|
|
|
|
"github.com/charmbracelet/bubbles/textinput"
|
|
|
|
|
|
tea "github.com/charmbracelet/bubbletea"
|
|
|
|
|
|
"github.com/sirupsen/logrus"
|
|
|
|
|
|
"os"
|
|
|
|
|
|
"path/filepath"
|
|
|
|
|
|
"strings"
|
|
|
|
|
|
)
|
|
|
|
|
|
|
2026-01-20 17:40:21 +05:00
|
|
|
|
// DirSelectModel структура описывающая модель поля выбора директории
|
2025-12-26 17:45:37 +05:00
|
|
|
|
type DirSelectModel struct {
|
2026-01-20 17:40:21 +05:00
|
|
|
|
input textinput.Model //Готовый компонент input для ввода данных
|
|
|
|
|
|
label string //Имя поля
|
|
|
|
|
|
description string //Описание поля
|
|
|
|
|
|
focus int //Фокус для управления опциями
|
|
|
|
|
|
open bool //Открыт ли список
|
|
|
|
|
|
dir string //Имя выбранной директории
|
|
|
|
|
|
currentDirectory string //Директория текущего файла
|
|
|
|
|
|
options []os.FileInfo //Элементы выбора файлов
|
|
|
|
|
|
optionsCount int //Количество элементов к показу
|
2025-12-26 17:45:37 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:40:21 +05:00
|
|
|
|
// NewDirSelectModel создает новую модель поля выбора директории
|
|
|
|
|
|
func NewDirSelectModel(label, dir, description string, validator textinput.ValidateFunc) *DirSelectModel {
|
2025-12-26 17:45:37 +05:00
|
|
|
|
inputModel := textinput.New()
|
2026-01-20 17:40:21 +05:00
|
|
|
|
inputModel.Validate = validator
|
2025-12-26 17:45:37 +05:00
|
|
|
|
inputModel.SetValue(dir)
|
|
|
|
|
|
return &DirSelectModel{
|
|
|
|
|
|
input: inputModel,
|
|
|
|
|
|
label: label,
|
2026-01-19 17:44:49 +05:00
|
|
|
|
description: description,
|
2025-12-26 17:45:37 +05:00
|
|
|
|
currentDirectory: filepath.Dir(dir),
|
|
|
|
|
|
open: false,
|
|
|
|
|
|
dir: dir,
|
2026-01-19 17:44:49 +05:00
|
|
|
|
optionsCount: 10,
|
2025-12-26 17:45:37 +05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *DirSelectModel) Init() tea.Cmd {
|
|
|
|
|
|
return textinput.Blink
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:40:21 +05:00
|
|
|
|
// Update отслеживает действия с полями
|
2025-12-26 17:45:37 +05:00
|
|
|
|
func (m *DirSelectModel) Update(msg tea.Msg) (FieldModel, tea.Cmd) {
|
|
|
|
|
|
var cmds []tea.Cmd
|
|
|
|
|
|
|
|
|
|
|
|
switch msg := msg.(type) {
|
|
|
|
|
|
case tea.KeyMsg:
|
2026-01-20 17:40:21 +05:00
|
|
|
|
//Отслеживает нажатия клавиш.
|
|
|
|
|
|
//Если список открыт, то отслеживаем клавиши отдельно
|
2025-12-26 17:45:37 +05:00
|
|
|
|
if m.open {
|
|
|
|
|
|
switch msg.String() {
|
|
|
|
|
|
case "esc":
|
2026-01-20 17:40:21 +05:00
|
|
|
|
//Закрываем список
|
2025-12-26 17:45:37 +05:00
|
|
|
|
m.open = false
|
|
|
|
|
|
case "up":
|
2026-01-20 17:40:21 +05:00
|
|
|
|
//Уменьшаем фокус, поднимаем его
|
2025-12-26 17:45:37 +05:00
|
|
|
|
m.focus--
|
|
|
|
|
|
if m.focus < 0 {
|
|
|
|
|
|
m.focus = len(m.options) - 1
|
|
|
|
|
|
}
|
|
|
|
|
|
case "down":
|
2026-01-20 17:40:21 +05:00
|
|
|
|
//Увеличиваем фокус, опуская его
|
2025-12-26 17:45:37 +05:00
|
|
|
|
m.focus++
|
|
|
|
|
|
if m.focus == len(m.options) {
|
|
|
|
|
|
m.focus = 0
|
|
|
|
|
|
}
|
|
|
|
|
|
case "enter":
|
2026-01-20 17:40:21 +05:00
|
|
|
|
//Выбираем директорию из списка. Если список пуст ничего не делаем.
|
|
|
|
|
|
//Получаем имя файла.
|
|
|
|
|
|
//Если имя файла ".." возвращаемся в родительскую папку.
|
|
|
|
|
|
//Если файл является директорией, то записываем его и закрываем список.
|
|
|
|
|
|
if len(m.options) == 0 || m.focus >= len(m.options) {
|
2025-12-26 17:45:37 +05:00
|
|
|
|
return m, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
file := m.options[m.focus]
|
|
|
|
|
|
fileName := file.Name()
|
|
|
|
|
|
if fileName == ".." {
|
|
|
|
|
|
m.currentDirectory = filepath.Dir(m.currentDirectory)
|
|
|
|
|
|
m.focus = 0
|
|
|
|
|
|
m.RenderOptions()
|
|
|
|
|
|
newValue := m.currentDirectory + string(filepath.Separator)
|
|
|
|
|
|
m.input.SetValue(newValue)
|
|
|
|
|
|
m.dir = newValue
|
|
|
|
|
|
m.input.CursorEnd()
|
|
|
|
|
|
return m, nil
|
|
|
|
|
|
}
|
|
|
|
|
|
if file.IsDir() {
|
|
|
|
|
|
m.currentDirectory = filepath.Join(m.currentDirectory, fileName)
|
|
|
|
|
|
newValue := m.currentDirectory + string(filepath.Separator)
|
|
|
|
|
|
m.input.SetValue(newValue)
|
|
|
|
|
|
m.dir = newValue
|
|
|
|
|
|
m.input.CursorEnd()
|
|
|
|
|
|
m.open = false
|
|
|
|
|
|
m.focus = 0
|
2026-01-16 17:44:03 +05:00
|
|
|
|
return m, nil
|
2025-12-26 17:45:37 +05:00
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
} else {
|
2026-01-20 17:40:21 +05:00
|
|
|
|
//Если список не открыт и был нажат enter, открываем его и рисуем опции списка.
|
|
|
|
|
|
//Иначе просто передаем сообщения нажатия клавиш в input.
|
2025-12-26 17:45:37 +05:00
|
|
|
|
switch msg.String() {
|
|
|
|
|
|
case "enter":
|
|
|
|
|
|
m.open = true
|
|
|
|
|
|
newDir := strings.TrimSpace(m.input.Value())
|
|
|
|
|
|
if len(newDir) > 1 && strings.HasSuffix(newDir, string(filepath.Separator)) {
|
|
|
|
|
|
newDir = strings.TrimSuffix(newDir, string(filepath.Separator))
|
|
|
|
|
|
}
|
|
|
|
|
|
if info, err := os.Stat(newDir); err == nil && info.IsDir() {
|
|
|
|
|
|
m.currentDirectory = newDir
|
|
|
|
|
|
m.dir = newDir + string(filepath.Separator)
|
|
|
|
|
|
m.input.SetValue(m.dir)
|
|
|
|
|
|
m.input.CursorEnd()
|
|
|
|
|
|
} else {
|
|
|
|
|
|
m.currentDirectory = filepath.Dir(newDir)
|
|
|
|
|
|
}
|
|
|
|
|
|
m.RenderOptions()
|
|
|
|
|
|
return m, nil
|
|
|
|
|
|
default:
|
|
|
|
|
|
var cmd tea.Cmd
|
|
|
|
|
|
m.input, cmd = m.input.Update(msg)
|
|
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
|
|
if m.input.Value() != m.dir {
|
|
|
|
|
|
m.dir = m.input.Value()
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
default:
|
2026-01-20 17:40:21 +05:00
|
|
|
|
//Все прочие виды сообщений передаем в input если список закрыт
|
2025-12-26 17:45:37 +05:00
|
|
|
|
if !m.open {
|
|
|
|
|
|
var cmd tea.Cmd
|
|
|
|
|
|
m.input, cmd = m.input.Update(msg)
|
|
|
|
|
|
cmds = append(cmds, cmd)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-23 17:52:47 +05:00
|
|
|
|
return m, tea.Sequence(cmds...)
|
2025-12-26 17:45:37 +05:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *DirSelectModel) View() string {
|
|
|
|
|
|
res := fmt.Sprintf("%s: %s", m.label, m.input.View())
|
2026-01-20 17:40:21 +05:00
|
|
|
|
if m.input.Err != nil && !m.open {
|
|
|
|
|
|
res += fmt.Sprintf("\nОшибка валидации: %s", m.input.Err)
|
|
|
|
|
|
} else if m.description != "" {
|
2026-01-19 17:44:49 +05:00
|
|
|
|
res += fmt.Sprintf("\n%s", m.description)
|
|
|
|
|
|
}
|
2025-12-26 17:45:37 +05:00
|
|
|
|
if m.open && len(m.options) > 0 {
|
2026-01-20 17:40:21 +05:00
|
|
|
|
start, end := utils.CalculateSelectOptionsIndex(m.focus, m.optionsCount, len(m.options))
|
2025-12-26 17:45:37 +05:00
|
|
|
|
for i := start; i < end; i++ {
|
|
|
|
|
|
file := m.options[i]
|
|
|
|
|
|
name := file.Name()
|
|
|
|
|
|
prefix := "\n\t"
|
|
|
|
|
|
if m.focus == i {
|
|
|
|
|
|
prefix += "-> "
|
|
|
|
|
|
} else {
|
|
|
|
|
|
prefix += " "
|
|
|
|
|
|
}
|
|
|
|
|
|
if name == ".." {
|
|
|
|
|
|
res += prefix + ui.SelectFileDirParentStyle.Render(name)
|
|
|
|
|
|
} else if file.IsDir() {
|
|
|
|
|
|
res += prefix + ui.SelectDirStyle.Render(name)
|
|
|
|
|
|
} else {
|
|
|
|
|
|
res += prefix + ui.SelectFileDirDeniedStyle.Render(name)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
return res
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *DirSelectModel) Focus() {
|
|
|
|
|
|
m.input.Focus()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *DirSelectModel) Blur() {
|
|
|
|
|
|
m.input.Blur()
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *DirSelectModel) CaptureKeys() bool {
|
|
|
|
|
|
return m.open
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *DirSelectModel) GetLabel() string {
|
|
|
|
|
|
return m.label
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
func (m *DirSelectModel) GetValue() interface{} {
|
|
|
|
|
|
return m.dir
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2026-01-20 17:40:21 +05:00
|
|
|
|
// RenderOptions заполняем список опций на основе директорий
|
2025-12-26 17:45:37 +05:00
|
|
|
|
func (m *DirSelectModel) RenderOptions() {
|
2026-01-20 17:40:21 +05:00
|
|
|
|
//Читаем файлы к текущей директории
|
2025-12-26 17:45:37 +05:00
|
|
|
|
filesInDir, err := os.ReadDir(m.currentDirectory)
|
|
|
|
|
|
if err != nil {
|
|
|
|
|
|
logrus.Errorf("Ошибка чтения директории: %v", err)
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
m.options = nil
|
2026-01-20 17:40:21 +05:00
|
|
|
|
//Если это не корень файловой системы, добавляем переход на родительскую директорию
|
2025-12-26 17:45:37 +05:00
|
|
|
|
var allOptions []os.FileInfo
|
|
|
|
|
|
if m.currentDirectory != "/" && filepath.Clean(m.currentDirectory) != "/" {
|
|
|
|
|
|
allOptions = append(allOptions, virtualDirInfo{name: ".."})
|
|
|
|
|
|
}
|
2026-01-20 17:40:21 +05:00
|
|
|
|
//Заполняем прочие опции
|
2025-12-26 17:45:37 +05:00
|
|
|
|
for _, entry := range filesInDir {
|
|
|
|
|
|
info, _ := entry.Info()
|
|
|
|
|
|
allOptions = append(allOptions, info)
|
|
|
|
|
|
}
|
2026-01-20 17:40:21 +05:00
|
|
|
|
//Заполняем фильтр для опций на основе значения поля ввода
|
2025-12-26 17:45:37 +05:00
|
|
|
|
filter := ""
|
|
|
|
|
|
if m.open {
|
|
|
|
|
|
inputValue := strings.TrimSpace(m.input.Value())
|
|
|
|
|
|
if inputValue != "" {
|
|
|
|
|
|
lastPart := inputValue[strings.LastIndex(inputValue, "/")+1:]
|
|
|
|
|
|
if lastPart != "" {
|
|
|
|
|
|
filter = strings.ToLower(lastPart)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
2026-01-20 17:40:21 +05:00
|
|
|
|
//Чистим опции и заполняем их правильными значениями
|
2025-12-26 17:45:37 +05:00
|
|
|
|
m.options = m.options[:0]
|
|
|
|
|
|
for _, opt := range allOptions {
|
|
|
|
|
|
name := opt.Name()
|
|
|
|
|
|
if filter == "" || strings.HasPrefix(strings.ToLower(name), filter) || name == ".." {
|
|
|
|
|
|
m.options = append(m.options, opt)
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if len(m.options) == 0 {
|
|
|
|
|
|
m.focus = 0
|
|
|
|
|
|
return
|
|
|
|
|
|
}
|
|
|
|
|
|
if m.focus >= len(m.options) {
|
|
|
|
|
|
m.focus = len(m.options) - 1
|
|
|
|
|
|
}
|
|
|
|
|
|
if m.focus < 0 {
|
|
|
|
|
|
m.focus = 0
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|