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

import (
4
	"get-container/cmd/hytop/backend"
liming6's avatar
liming6 committed
5
	"get-container/gpu"
liming6's avatar
liming6 committed
6
	"get-container/utils"
liming6's avatar
liming6 committed
7
8
	"time"

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

const (
	DCUTopVersion = "1.0.0"
liming6's avatar
liming6 committed
15
16
17
18
)

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

type TickMsg time.Time

// ModelMain tui主模型类
type ModelMain struct {
liming6's avatar
liming6 committed
32
33
34
	width, height int // 终端尺寸
	Header        *ModelHeader
	DCUInfo       *ModelDCUInfo
liming6's avatar
liming6 committed
35
	SysLoad       *ModelSysLoad
36
37
38
	ProcessInfo   *ModelProcessInfo
	index         uint64    // 记录update次数的值
	modelMsg      *ModelMsg // 记录模型信息
liming6's avatar
liming6 committed
39
40
}

liming6's avatar
liming6 committed
41
func NewModelMain(width, height int) ModelMain {
liming6's avatar
liming6 committed
42
	result := ModelMain{}
liming6's avatar
liming6 committed
43
44
	result.width = width
	result.height = height
liming6's avatar
liming6 committed
45
	result.Header = &ModelHeader{}
liming6's avatar
liming6 committed
46
	result.DCUInfo = &ModelDCUInfo{width: width, height: height, info: make(map[int]backend.DCUInfo)}
liming6's avatar
liming6 committed
47
	result.SysLoad = NewModelSysLoad(width)
48
	result.ProcessInfo = NewModelProcessInfo(width)
liming6's avatar
liming6 committed
49
	return result
liming6's avatar
liming6 committed
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
}

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 {
liming6's avatar
liming6 committed
66
67
	modelMsg := ModelMsg{}
	if err := initModelInfo(&modelMsg); err != nil {
liming6's avatar
liming6 committed
68
69
		return tea.Quit
	}
liming6's avatar
liming6 committed
70
71
72
73
	cmds := make([]tea.Cmd, 0)
	if c := m.Header.Init(); c != nil {
		cmds = append(cmds, c)
	}
liming6's avatar
liming6 committed
74
75
76
77
	m.DCUInfo.DCUNum = len(modelMsg.DCUInfo)
	if c := m.DCUInfo.Init(); c != nil {
		cmds = append(cmds, c)
	}
liming6's avatar
liming6 committed
78
79
80
81

	if c := m.SysLoad.Init(); c != nil {
		cmds = append(cmds, c)
	}
82
83
84
	if c := m.ProcessInfo.Init(); c != nil {
		cmds = append(cmds, c)
	}
liming6's avatar
liming6 committed
85
	m.modelMsg = &modelMsg
liming6's avatar
liming6 committed
86
	// 将调用初始化方法
liming6's avatar
liming6 committed
87
	cmds = append(cmds, tickCmd(), sendMsgCmd(&modelMsg))
liming6's avatar
liming6 committed
88
89
90
91
92
93
94
95
96
97
98
99
100
	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))
liming6's avatar
liming6 committed
101
102
103
		header, _ := m.Header.Update(m.modelMsg)
		dcuInfo, _ := m.DCUInfo.Update(m.modelMsg)
		sysLoad, _ := m.SysLoad.Update(m.modelMsg)
104
		pidinfo, _ := m.ProcessInfo.Update(m.modelMsg)
liming6's avatar
liming6 committed
105
		m.Header = header.(*ModelHeader)
liming6's avatar
liming6 committed
106
		m.DCUInfo = dcuInfo.(*ModelDCUInfo)
liming6's avatar
liming6 committed
107
		m.SysLoad = sysLoad.(*ModelSysLoad)
108
		m.ProcessInfo = pidinfo.(*ModelProcessInfo)
liming6's avatar
liming6 committed
109
		return m, tickCmd()
liming6's avatar
liming6 committed
110
111
112
	case ModelMsg: // 初始化
		header, _ := m.Header.Update(m.modelMsg)
		dcuInfo, _ := m.DCUInfo.Update(m.modelMsg)
liming6's avatar
liming6 committed
113
		sysLoad, _ := m.SysLoad.Update(m.modelMsg)
114
		pidinfo, _ := m.ProcessInfo.Update(m.modelMsg)
liming6's avatar
liming6 committed
115
116
		m.Header = header.(*ModelHeader)
		m.DCUInfo = dcuInfo.(*ModelDCUInfo)
liming6's avatar
liming6 committed
117
		m.SysLoad = sysLoad.(*ModelSysLoad)
118
		m.ProcessInfo = pidinfo.(*ModelProcessInfo)
liming6's avatar
liming6 committed
119
		return m, nil
liming6's avatar
liming6 committed
120
121
122
123
124
	}
	return m, nil
}

func (m *ModelMain) View() string {
125
	return m.Header.View() + m.DCUInfo.View() + m.SysLoad.View() + m.ProcessInfo.View() + "\n"
liming6's avatar
liming6 committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
}

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

liming6's avatar
liming6 committed
144
145
146
147
148
149
150
151
152
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
liming6's avatar
liming6 committed
153
154
155
156
157
158
	if model.index%20 == 0 {
		backend.UpdateDCUInfo(true)
	} else {
		backend.UpdateDCUInfo(false)
	}
	model.DCUInfo = backend.GetDCUInfo()
liming6's avatar
liming6 committed
159
	model.systemInfo, err = utils.GetSysInfo()
160
	model.DCUPidInfo = backend.GetDCUProcessInfo()
liming6's avatar
liming6 committed
161
	return err
liming6's avatar
liming6 committed
162
163
}

liming6's avatar
liming6 committed
164
165
166
167
// updateModelInfo 更新模型信息
func updateModelInfo(modelMsg *ModelMsg, index uint64, t time.Time) {
	modelMsg.index = index
	modelMsg.t = t
liming6's avatar
liming6 committed
168
	if modelMsg.index%60 == 0 {
liming6's avatar
liming6 committed
169
170
171
172
173
		backend.UpdateDCUInfo(true)
	} else {
		backend.UpdateDCUInfo(false)
	}
	modelMsg.DCUInfo = backend.GetDCUInfo()
liming6's avatar
liming6 committed
174
	modelMsg.systemInfo, _ = utils.GetSysInfo()
175
	modelMsg.DCUPidInfo = backend.GetDCUProcessInfo()
liming6's avatar
liming6 committed
176
}