optimize tui interface and interaction

This commit is contained in:
2026-02-22 20:18:12 +08:00
parent ce9af9f7d0
commit 47a2dfeda1
7 changed files with 289 additions and 183 deletions

View File

@@ -3,8 +3,10 @@ package wizard
import (
"encoding/json"
"fmt"
"net"
"os"
"path/filepath"
"sunhpc/pkg/utils"
"github.com/charmbracelet/bubbles/textinput"
)
@@ -66,31 +68,30 @@ func (m *model) saveCurrentPage() {
func (m *model) saveDataPage() {
if len(m.textInputs) >= 8 {
m.config.Hostname = m.textInputs[0].Value()
m.config.Country = m.textInputs[1].Value()
m.config.Region = m.textInputs[2].Value()
m.config.Timezone = m.textInputs[3].Value()
m.config.HomePage = m.textInputs[4].Value()
m.config.HomePage = m.textInputs[0].Value()
m.config.Hostname = m.textInputs[1].Value()
m.config.Country = m.textInputs[2].Value()
m.config.Region = m.textInputs[3].Value()
m.config.Timezone = m.textInputs[4].Value()
m.config.DBAddress = m.textInputs[5].Value()
m.config.DBName = m.textInputs[6].Value()
m.config.DataAddress = m.textInputs[7].Value()
m.config.DataAddress = m.textInputs[6].Value()
}
}
func (m *model) savePublicNetworkPage() {
if len(m.textInputs) >= 4 {
m.config.PublicInterface = m.textInputs[0].Value()
m.config.IPAddress = m.textInputs[1].Value()
m.config.Netmask = m.textInputs[2].Value()
m.config.Gateway = m.textInputs[3].Value()
m.config.PublicIPAddress = m.textInputs[1].Value()
m.config.PublicNetmask = m.textInputs[2].Value()
m.config.PublicGateway = m.textInputs[3].Value()
}
}
func (m *model) saveInternalNetworkPage() {
if len(m.textInputs) >= 3 {
m.config.InternalInterface = m.textInputs[0].Value()
m.config.InternalIP = m.textInputs[1].Value()
m.config.InternalMask = m.textInputs[2].Value()
m.config.InternalIPAddress = m.textInputs[1].Value()
m.config.InternalNetmask = m.textInputs[2].Value()
}
}
@@ -108,20 +109,18 @@ func (m *model) initPageInputs() {
switch m.currentPage {
case PageData:
fields := []struct{ label, value string }{
{"主机名:", m.config.Hostname},
{"国家:", m.config.Country},
{"地区:", m.config.Region},
{"时区:", m.config.Timezone},
{"主页:", m.config.HomePage},
{"数据库地址:", m.config.DBAddress},
{"数据库名称:", m.config.DBName},
{"Data 地址:", m.config.DataAddress},
{"Homepage", m.config.HomePage},
{"Hostname", m.config.Hostname},
{"Country", m.config.Country},
{"Region", m.config.Region},
{"Timezone", m.config.Timezone},
{"DB Path", m.config.DBAddress},
{"Software", m.config.DataAddress},
}
for _, f := range fields {
ti := textinput.New()
ti.Placeholder = ""
ti.Placeholder = f.label
//ti.Placeholder = "请输入" + f.label[:len(f.label)-1]
ti.SetValue(f.value)
ti.Width = 50
m.textInputs = append(m.textInputs, ti)
@@ -130,18 +129,19 @@ func (m *model) initPageInputs() {
if len(m.textInputs) > 0 {
m.textInputs[0].Focus()
}
m.inputLabels = []string{"Hostname", "Country", "Region", "Timezone", "Homepage", "DBPath", "DBName", "Software"}
m.inputLabels = []string{"Homepage", "Hostname", "Country", "Region", "Timezone", "DBPath", "Software"}
case PagePublicNetwork:
fields := []struct{ label, value string }{
{"公网接口:", m.config.PublicInterface},
{"IP 地址:", m.config.IPAddress},
{"子网掩码:", m.config.Netmask},
{"网关:", m.config.Gateway},
{"Public Interface", m.config.PublicInterface},
{"Public IP Address", m.config.PublicIPAddress},
{"Public Netmask", m.config.PublicNetmask},
{"Public Gateway", m.config.PublicGateway},
}
for _, f := range fields {
ti := textinput.New()
ti.Placeholder = ""
ti.Placeholder = f.label
ti.SetValue(f.value)
ti.Width = 50
m.textInputs = append(m.textInputs, ti)
@@ -150,17 +150,17 @@ func (m *model) initPageInputs() {
if len(m.textInputs) > 0 {
m.textInputs[0].Focus()
}
m.inputLabels = []string{"Wan iface", "IPAddress", "Netmask", "Gateway"}
m.inputLabels = []string{"Public Interface", "Public IP Address", "Public Netmask", "Public Gateway"}
case PageInternalNetwork:
fields := []struct{ label, value string }{
{"内网接口:", m.config.InternalInterface},
{"内网 IP:", m.config.InternalIP},
{"内网掩码:", m.config.InternalMask},
{"Internal Interface", m.config.InternalInterface},
{"Internal IP Address", m.config.InternalIPAddress},
{"Internal Netmask", m.config.InternalNetmask},
}
for _, f := range fields {
ti := textinput.New()
ti.Placeholder = ""
ti.Placeholder = f.label
ti.SetValue(f.value)
ti.Width = 50
m.textInputs = append(m.textInputs, ti)
@@ -169,16 +169,16 @@ func (m *model) initPageInputs() {
if len(m.textInputs) > 0 {
m.textInputs[0].Focus()
}
m.inputLabels = []string{"Lan iface", "IPAddress", "Netmask"}
m.inputLabels = []string{"Internal Interface", "Internal IP", "Internal Mask"}
case PageDNS:
fields := []struct{ label, value string }{
{" DNS:", m.config.DNSPrimary},
{" DNS:", m.config.DNSSecondary},
{"Primary DNS", m.config.DNSPrimary},
{"Secondary DNS", m.config.DNSSecondary},
}
for _, f := range fields {
ti := textinput.New()
ti.Placeholder = ""
ti.Placeholder = f.label
ti.SetValue(f.value)
ti.Width = 50
m.textInputs = append(m.textInputs, ti)
@@ -187,6 +187,31 @@ func (m *model) initPageInputs() {
if len(m.textInputs) > 0 {
m.textInputs[0].Focus()
}
m.inputLabels = []string{"DNSPrimary", "DNSSecondary"}
m.inputLabels = []string{"Pri DNS", "Sec DNS"}
}
}
// 获取系统网络接口
func getNetworkInterfaces() []string {
// 实现获取系统网络接口的逻辑
// 例如:使用 net.Interface() 函数获取系统网络接口
// 返回一个字符串切片,包含系统网络接口的名称
interfaces, err := net.Interfaces()
if err != nil {
return []string{utils.NoAvailableNetworkInterfaces}
}
var result []string
for _, iface := range interfaces {
// 跳过 loopback 接口
if iface.Flags&net.FlagLoopback != 0 {
continue
}
result = append(result, iface.Name)
}
if len(result) == 0 {
return []string{utils.NoAvailableNetworkInterfaces}
}
return result
}