first commit

This commit is contained in:
XeRo 2025-12-26 17:45:37 +05:00
commit 5203032b83
25 changed files with 2825 additions and 0 deletions

64
setup/fields/checkbox.go Normal file
View file

@ -0,0 +1,64 @@
package fields
import (
tea "github.com/charmbracelet/bubbletea"
)
type CheckboxModel struct {
Label string
Checked bool
}
func NewCheckboxModel(label string, checked bool) *CheckboxModel {
return &CheckboxModel{
Label: label,
Checked: checked,
}
}
func (m *CheckboxModel) Init() tea.Cmd {
return nil
}
func (m *CheckboxModel) Update(msg tea.Msg) (FieldModel, tea.Cmd) {
switch msg := msg.(type) {
case tea.KeyMsg:
switch msg.String() {
case " ", "enter":
m.Checked = !m.Checked
}
}
return m, nil
}
func (m *CheckboxModel) View() string {
res := ""
res += m.Label + ": "
if m.Checked {
res += "[x]"
} else {
res += "[ ]"
}
return res
}
func (m *CheckboxModel) Focus() {
}
func (m *CheckboxModel) Blur() {
}
func (m *CheckboxModel) CaptureKeys() bool {
return false
}
func (m *CheckboxModel) GetLabel() string {
return m.Label
}
func (m *CheckboxModel) GetValue() interface{} {
return m.Checked
}

48
setup/fields/compute.go Normal file
View file

@ -0,0 +1,48 @@
package fields
import (
"fmt"
tea "github.com/charmbracelet/bubbletea"
)
type ComputeModel struct {
Label string
value string
}
func NewComputeModel(label, value string) *ComputeModel {
return &ComputeModel{
Label: label,
value: value,
}
}
func (m *ComputeModel) Init() tea.Cmd {
return nil
}
func (m *ComputeModel) Update(msg tea.Msg) (FieldModel, tea.Cmd) {
return m, nil
}
func (m *ComputeModel) View() string {
return fmt.Sprintf("%s: %s", m.Label, m.value)
}
func (m *ComputeModel) Focus() {}
func (m *ComputeModel) Blur() {
}
func (m *ComputeModel) CaptureKeys() bool {
return false
}
func (m *ComputeModel) GetLabel() string {
return m.Label
}
func (m *ComputeModel) GetValue() interface{} {
return m.value
}

236
setup/fields/dirSelect.go Normal file
View file

@ -0,0 +1,236 @@
package fields
import (
"bubble/setup/ui"
"bubble/types"
"fmt"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/sirupsen/logrus"
"os"
"path/filepath"
"strings"
)
type DirSelectModel struct {
input textinput.Model
label string
focus int
open bool
dir string
currentDirectory string
options []os.FileInfo
optionsCount int
}
func NewDirSelectModel(label, dir string) *DirSelectModel {
inputModel := textinput.New()
inputModel.SetValue(dir)
return &DirSelectModel{
input: inputModel,
label: label,
currentDirectory: filepath.Dir(dir),
open: false,
dir: dir,
optionsCount: 3,
}
}
func (m *DirSelectModel) Init() tea.Cmd {
return textinput.Blink
}
func (m *DirSelectModel) Update(msg tea.Msg) (FieldModel, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
if m.open {
switch msg.String() {
case "esc":
m.open = false
case "up":
m.focus--
if m.focus < 0 {
m.focus = len(m.options) - 1
}
case "down":
m.focus++
if m.focus == len(m.options) {
m.focus = 0
}
case "enter":
if len(m.options) == 0 || m.focus == len(m.options) {
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
selected := types.SelectedDirMsg{
DirectoryName: fileName,
Index: m.focus,
}
m.focus = 0
return m, func() tea.Msg {
return selected
}
}
}
} else {
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:
if !m.open {
var cmd tea.Cmd
m.input, cmd = m.input.Update(msg)
cmds = append(cmds, cmd)
}
}
return m, tea.Batch(cmds...)
}
func (m *DirSelectModel) View() string {
res := fmt.Sprintf("%s: %s", m.label, m.input.View())
if m.open && len(m.options) > 0 {
visibleCount := m.optionsCount
if visibleCount > len(m.options) {
visibleCount = len(m.options)
}
half := visibleCount / 2
start := m.focus - half
if start < 0 {
start = 0
}
end := start + visibleCount
if end > len(m.options) {
end = len(m.options)
start = end - visibleCount
if start < 0 {
start = 0
}
}
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
}
func (m *DirSelectModel) RenderOptions() {
filesInDir, err := os.ReadDir(m.currentDirectory)
if err != nil {
logrus.Errorf("Ошибка чтения директории: %v", err)
return
}
m.options = nil
var allOptions []os.FileInfo
if m.currentDirectory != "/" && filepath.Clean(m.currentDirectory) != "/" {
allOptions = append(allOptions, virtualDirInfo{name: ".."})
}
for _, entry := range filesInDir {
info, _ := entry.Info()
allOptions = append(allOptions, info)
}
filter := ""
if m.open {
inputValue := strings.TrimSpace(m.input.Value())
if inputValue != "" {
lastPart := inputValue[strings.LastIndex(inputValue, "/")+1:]
if lastPart != "" {
filter = strings.ToLower(lastPart)
}
}
}
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
}
}

View file

@ -0,0 +1,14 @@
package fields
import tea "github.com/charmbracelet/bubbletea"
type FieldModel interface {
Init() tea.Cmd
Update(msg tea.Msg) (FieldModel, tea.Cmd)
View() string
Focus()
Blur()
CaptureKeys() bool
GetLabel() string
GetValue() interface{}
}

257
setup/fields/fileSelect.go Normal file
View file

@ -0,0 +1,257 @@
package fields
import (
"bubble/setup/ui"
"bubble/types"
"fmt"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
"github.com/sirupsen/logrus"
"os"
"path/filepath"
"slices"
"strings"
"time"
)
type FileSelectModel struct {
input textinput.Model
label string
focus int
open bool
file string
currentDirectory string
options []os.FileInfo
extensions []string
optionsCount int
}
func NewFileSelectModel(label, file string, extensions []string) *FileSelectModel {
inputModel := textinput.NewModel()
inputModel.SetValue(file)
return &FileSelectModel{
input: inputModel,
label: label,
extensions: extensions,
currentDirectory: filepath.Dir(file),
open: false,
file: file,
optionsCount: 3,
}
}
func (m *FileSelectModel) Init() tea.Cmd {
return textinput.Blink
}
func (m *FileSelectModel) Update(msg tea.Msg) (FieldModel, tea.Cmd) {
var cmds []tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
if m.open {
switch msg.String() {
case "esc":
m.open = false
case "up":
m.focus--
if m.focus < 0 {
m.focus = len(m.options) - 1
}
case "down":
m.focus++
if m.focus == len(m.options) {
m.focus = 0
}
case "enter":
if len(m.options) == 0 || m.focus == len(m.options) {
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.file = newValue
m.input.CursorEnd()
return m, nil
}
if file.IsDir() {
m.currentDirectory = filepath.Join(m.currentDirectory, fileName)
m.focus = 0
newValue := m.currentDirectory + string(filepath.Separator)
m.input.SetValue(newValue)
m.file = newValue
m.input.CursorEnd()
m.RenderOptions()
} else if slices.Contains(m.extensions, filepath.Ext(fileName)) {
fullPath := filepath.Join(m.currentDirectory, fileName)
m.file = fullPath
m.input.SetValue(fullPath)
m.open = false
selected := types.SelectedFileMsg{
Filename: fileName,
Index: m.focus,
}
m.input.CursorEnd()
m.focus = 0
return m, func() tea.Msg {
return selected
}
}
}
} else {
switch msg.String() {
case "enter":
m.open = true
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.file {
m.file = m.input.Value()
m.currentDirectory = filepath.Dir(m.file)
}
}
}
default:
if !m.open {
var cmd tea.Cmd
m.input, cmd = m.input.Update(msg)
cmds = append(cmds, cmd)
}
}
return m, tea.Batch(cmds...)
}
func (m *FileSelectModel) View() string {
res := fmt.Sprintf("%s: %s", m.label, m.input.View())
if m.open && len(m.options) > 0 {
visibleCount := m.optionsCount //количество отображаемых
if visibleCount > len(m.options) { //если количество отображаемых больше, чем есть опций на самом деле
visibleCount = len(m.options) //то меняем количество видимых на количество опций
}
half := visibleCount / 2 //количество объектов сверху и снизу
start := m.focus - half //стартовая позиция
if start < 0 { //если стартовая позиция меньше 0, то меняем на 0
start = 0
}
end := start + visibleCount //последний элемент
if end > len(m.options) { //если последний элемент вне опций
end = len(m.options) //меняем на количество опций
start = end - visibleCount //перерасчет стартовой позиции
if start < 0 {
start = 0
}
}
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 if !slices.Contains(m.extensions, filepath.Ext(name)) {
res += prefix + ui.SelectFileDirDeniedStyle.Render(name)
} else {
res += prefix + ui.SelectFileStyle.Render(name)
}
}
}
return res
}
func (m *FileSelectModel) Focus() {
m.input.Focus()
}
func (m *FileSelectModel) Blur() {
m.input.Blur()
}
func (m *FileSelectModel) CaptureKeys() bool {
return m.open
}
func (m *FileSelectModel) GetLabel() string {
return m.label
}
func (m *FileSelectModel) GetValue() interface{} {
return m.file
}
func (m *FileSelectModel) RenderOptions() {
filesInDir, err := os.ReadDir(m.currentDirectory)
if err != nil {
logrus.Errorf("Ошибка чтения директории: %v", err)
return
}
m.options = nil
var allOptions []os.FileInfo
if m.currentDirectory != "/" && filepath.Clean(m.currentDirectory) != "/" {
allOptions = append(allOptions, virtualDirInfo{name: ".."})
//m.options = append(m.options, virtualDirInfo{name: ".."})
}
for _, entry := range filesInDir {
info, _ := entry.Info()
allOptions = append(allOptions, info)
//m.options = append(m.options, info)
}
filter := ""
if m.open {
inputValue := strings.TrimSpace(m.input.Value())
if inputValue != "" {
lastPart := inputValue[strings.LastIndex(inputValue, "/")+1:]
if lastPart != "" {
filter = strings.ToLower(lastPart)
}
}
}
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)
}
}
//если у нас нет опций, то и фокус нельзя куда-либо поставить, то есть надо быть осторожным,
//но после этой функции у нас задействуется view, где есть условие, отрисовка только если опций больше 0,
//так что у нас стоит защита на использование m.options[m.focus] при фокусе равном 0 и отсутствии опций.
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
}
}
type virtualDirInfo struct {
name string
}
func (v virtualDirInfo) Name() string { return v.name }
func (v virtualDirInfo) Size() int64 {
return 0
}
func (v virtualDirInfo) Mode() os.FileMode { return os.ModeDir }
func (v virtualDirInfo) ModTime() time.Time { return time.Time{} }
func (v virtualDirInfo) IsDir() bool { return true }
func (v virtualDirInfo) Sys() interface{} { return nil }

59
setup/fields/input.go Normal file
View file

@ -0,0 +1,59 @@
package fields
import (
"fmt"
"github.com/charmbracelet/bubbles/cursor"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
)
type InputModel struct {
label string
input textinput.Model
}
func NewInputModel(label, value string) *InputModel {
t := textinput.New()
t.SetValue(value)
t.Cursor.SetMode(cursor.CursorBlink)
return &InputModel{
label: label,
input: t,
}
}
func (m *InputModel) Init() tea.Cmd {
return textinput.Blink
}
func (m *InputModel) Update(msg tea.Msg) (FieldModel, tea.Cmd) {
var cmd tea.Cmd
m.input, cmd = m.input.Update(msg)
return m, cmd
}
func (m *InputModel) View() string {
res := fmt.Sprintf("%s: %v", m.label, m.input.View())
return res
}
func (m *InputModel) Focus() {
m.input.Focus()
}
func (m *InputModel) Blur() {
m.input.Blur()
}
func (m *InputModel) CaptureKeys() bool {
//return m.input.Focused()
return false
}
func (m *InputModel) GetLabel() string {
return m.label
}
func (m *InputModel) GetValue() interface{} {
return m.input.Value()
}

128
setup/fields/select.go Normal file
View file

@ -0,0 +1,128 @@
package fields
import (
"bubble/types"
"fmt"
"github.com/charmbracelet/bubbles/textinput"
tea "github.com/charmbracelet/bubbletea"
)
type SelectModel struct {
label string
options []string
focus int
open bool
chosen string
}
func NewSelectModel(label, value string, options []string) *SelectModel {
found := false
for i := range options {
if options[i] == value {
found = true
}
}
if !found {
value = ""
}
return &SelectModel{
options: options,
label: label,
focus: len(options),
chosen: value,
}
}
func (m *SelectModel) Init() tea.Cmd {
return textinput.Blink
}
func (m *SelectModel) Update(msg tea.Msg) (FieldModel, tea.Cmd) {
var cmd tea.Cmd
switch msg := msg.(type) {
case tea.KeyMsg:
if m.open {
switch msg.String() {
case "esc":
m.open = false
case "up":
m.focus--
if m.focus < 0 {
m.focus = len(m.options) - 1
}
case "down":
m.focus++
if m.focus == len(m.options) {
m.focus = 0
}
case "enter":
if len(m.options) == 0 || m.focus == len(m.options) {
return m, nil
}
m.chosen = m.options[m.focus]
selected := types.SelectedOptionMsg{
FieldIndex: m.focus,
Value: m.chosen,
}
m.open = false
m.focus = len(m.options)
return m, func() tea.Msg {
return selected
}
}
} else {
switch msg.String() {
case "enter":
m.open = true
for optionIdx := range m.options {
if m.options[optionIdx] == m.chosen {
m.focus = optionIdx
}
}
if m.chosen == "" && len(m.options) > 0 {
m.focus = 0
}
}
}
}
return m, cmd
}
func (m *SelectModel) View() string {
res := fmt.Sprintf("%s: %s", m.label, m.chosen)
if m.open && len(m.options) > 0 {
for i := range m.options {
if m.focus == i {
res += "\n\t-> " + m.options[i]
} else {
res += "\n\t " + m.options[i]
}
}
}
return res
}
func (m *SelectModel) Close() {
m.open = false
}
func (m *SelectModel) Focus() {
}
func (m *SelectModel) Blur() {
m.open = false
}
func (m *SelectModel) CaptureKeys() bool {
return m.open
}
func (m *SelectModel) GetLabel() string {
return m.label
}
func (m *SelectModel) GetValue() interface{} {
return m.chosen
}