main.go 2.82 KB
Newer Older
liming6's avatar
liming6 committed
1
2
3
4
package tui

import (
	"get-container/gpu"
liming6's avatar
liming6 committed
5
6
	"time"

liming6's avatar
liming6 committed
7
8
	tea "github.com/charmbracelet/bubbletea"
	"github.com/charmbracelet/lipgloss"
liming6's avatar
liming6 committed
9
10
11
12
)

const (
	DCUTopVersion = "1.0.0"
liming6's avatar
liming6 committed
13
14
15
16
)

// ModelMsg 模型信息,在父组件和各个子组件间共享信息
type ModelMsg struct {
liming6's avatar
liming6 committed
17
18
19
20
21
22
	t         time.Time          // 当前时间
	index     uint64             // update次数
	Version   *gpu.HYVersionInfo // gpu版本相关信息
	MyVersion string
	// DCUInfo    []gpu.DCUInfo      // DCU全量信息
	// DCUPidInfo []gpu.DCUPidInfo   // 使用dcu的进程信息
liming6's avatar
liming6 committed
23
24
25
26
27
28
}

type TickMsg time.Time

// ModelMain tui主模型类
type ModelMain struct {
liming6's avatar
liming6 committed
29
30
31
32
33
34
35
36
37
38
39
40
	Header *ModelHeader
	// GPUInfo     *ModelDCUInfo
	// SysLoad     *ModelSysLoad
	// ProcessInfo *ModelProcess
	index    uint64    // 记录update次数的值
	modelMsg *ModelMsg // 记录模型信息
}

func NewModelMain() ModelMain {
	result := ModelMain{}
	result.Header = &ModelHeader{}
	return result
liming6's avatar
liming6 committed
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
}

func tickCmd() tea.Cmd {
	return tea.Every(time.Second, func(t time.Time) tea.Msg {
		return TickMsg(t)
	})
}

func sendMsgCmd(modelMsg *ModelMsg) tea.Cmd {
	return func() tea.Msg {
		return *modelMsg
	}
}

// Init 初始化信息
func (m *ModelMain) Init() tea.Cmd {
	modelMsg := &ModelMsg{}
liming6's avatar
liming6 committed
58
59
60
	if err := initModelInfo(modelMsg); err != nil {
		return tea.Quit
	}
liming6's avatar
liming6 committed
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
	cmds := make([]tea.Cmd, 0)
	if c := m.Header.Init(); c != nil {
		cmds = append(cmds, c)
	}
	m.modelMsg = modelMsg
	// 将调用初始化方法
	cmds = append(cmds, tickCmd(), sendMsgCmd(modelMsg))
	return tea.Batch(cmds...)
}

func (m *ModelMain) Update(inputMsg tea.Msg) (tea.Model, tea.Cmd) {
	switch msg := inputMsg.(type) {
	case tea.KeyMsg: // 键盘事件
		switch msg.String() {
		case "ctrl+c", "q":
			return m, tea.Quit
		}
	case TickMsg: // 定时事件
		m.index++
		updateModelInfo(m.modelMsg, m.index, time.Time(msg))
		cmds := make([]tea.Cmd, 0)
		header, cmdHeader := m.Header.Update(m.modelMsg)
		m.Header = header.(*ModelHeader)
		cmds = append(cmds, cmdHeader, tickCmd())
		return m, tea.Batch(cmds...)
	}
	return m, nil
}

func (m *ModelMain) View() string {
	return m.Header.View()
}

liming6's avatar
liming6 committed
94
95
96
// type ModelDCUInfo struct{}
// type ModelSysLoad struct{}
// type ModelProcess struct{}
liming6's avatar
liming6 committed
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113

var myBorder = lipgloss.Border{
	Top:          "═",
	TopLeft:      "╒",
	TopRight:     "╕",
	Bottom:       "═",
	BottomLeft:   "╘",
	BottomRight:  "╛",
	Left:         "│",
	Right:        "│",
	MiddleLeft:   "├",
	MiddleRight:  "┤",
	Middle:       "┼",
	MiddleTop:    "┬",
	MiddleBottom: "┴",
}

liming6's avatar
liming6 committed
114
115
116
117
118
119
120
121
122
123
124
125
func initModelInfo(model *ModelMsg) error {
	model.MyVersion = DCUTopVersion
	model.index = 0
	model.t = time.Now()
	ver, err := gpu.GetHYVersionInfo()
	if err != nil {
		return err
	}
	model.Version = ver
	return nil
}

liming6's avatar
liming6 committed
126
127
128
129
130
// updateModelInfo 更新模型信息
func updateModelInfo(modelMsg *ModelMsg, index uint64, t time.Time) {
	modelMsg.index = index
	modelMsg.t = t
}