Skip to content

Commit bc60707

Browse files
committed
Add HTML format export feature.
1 parent c30b67c commit bc60707

File tree

8 files changed

+2527
-8
lines changed

8 files changed

+2527
-8
lines changed

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Options:
7373
7474
Output options:
7575
-o, --output FILE Save output to specified file
76+
-f, --format FORMAT Report format (text, html)
7677
--compact Use compact mode (fewer columns)
7778
--quiet Quiet mode, reduce screen output
7879
@@ -228,6 +229,7 @@ sudo mv disk-health-monitor /usr/local/bin/
228229
229230
输出选项:
230231
-o, --output 文件名 将输出保存到指定文件
232+
-f, --format FORMAT 指定输出格式 (text, html)
231233
--compact 使用紧凑模式(减少显示列数)
232234
--quiet 安静模式,减少屏幕输出
233235

cmd/monitor/main.go

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ func main() {
4141
func parseFlags() (*model.Config, map[string]interface{}, error) {
4242
// Reset flag parsing
4343
flag.CommandLine = flag.NewFlagSet(os.Args[0], flag.ExitOnError)
44-
44+
4545
// Create default config
4646
config := model.NewDefaultConfig()
4747

@@ -98,19 +98,21 @@ func parseFlags() (*model.Config, map[string]interface{}, error) {
9898
// Apply flags to config
9999
config.Debug = *debug || *flagD
100100
config.Verbose = *verbose
101-
101+
102102
if *output != "" {
103103
config.OutputFile = *output
104104
} else if *flagO != "" {
105105
config.OutputFile = *flagO
106106
}
107-
107+
108108
if *format != "" {
109109
switch *format {
110110
case "text", "txt":
111111
config.OutputFormat = model.OutputFormatText
112112
case "pdf":
113113
config.OutputFormat = model.OutputFormatPDF
114+
case "html":
115+
config.OutputFormat = model.OutputFormatHTML
114116
default:
115117
return nil, nil, fmt.Errorf("不支持的输出格式: %s", *format)
116118
}
@@ -120,19 +122,21 @@ func parseFlags() (*model.Config, map[string]interface{}, error) {
120122
config.OutputFormat = model.OutputFormatText
121123
case "pdf":
122124
config.OutputFormat = model.OutputFormatPDF
125+
case "html":
126+
config.OutputFormat = model.OutputFormatHTML
123127
default:
124128
return nil, nil, fmt.Errorf("不支持的输出格式: %s", *flagF)
125129
}
126130
}
127-
131+
128132
if *dataFile != "" {
129133
config.DataFile = *dataFile
130134
}
131-
135+
132136
if *logFile != "" {
133137
config.LogFile = *logFile
134138
}
135-
139+
136140
config.NoGroup = *noGroup
137141
config.NoController = *noController
138142
config.ControllerOnly = *controllerOnly
@@ -171,7 +175,7 @@ func printHelp() {
171175
172176
输出选项:
173177
-o, --output FILE 输出到指定文件
174-
-f, --format FORMAT 指定输出格式 (text, pdf)
178+
-f, --format FORMAT 指定输出格式 (text, html)
175179
--quiet 静默模式,减少屏幕输出
176180
177181
显示选项:

disk-health-monitor

820 KB
Binary file not shown.

internal/model/config.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ const (
1717
OutputFormatText OutputFormat = "text"
1818
// OutputFormatJSON JSON格式输出
1919
OutputFormatJSON OutputFormat = "json"
20+
// OutputFormatHTML HTML格式输出
21+
OutputFormatHTML OutputFormat = "html"
2022
)
2123

2224
// Config 应用配置
@@ -91,7 +93,7 @@ func (c *Config) Validate() error {
9193

9294
// 验证输出格式
9395
switch c.OutputFormat {
94-
case OutputFormatPDF, OutputFormatText, OutputFormatJSON:
96+
case OutputFormatPDF, OutputFormatText, OutputFormatJSON, OutputFormatHTML:
9597
// 有效的格式
9698
default:
9799
return fmt.Errorf("不支持的输出格式: %s", c.OutputFormat)
@@ -136,5 +138,7 @@ func (c *Config) SetupOutputFile() {
136138
c.OutputFile = fmt.Sprintf("disk_health_%s.txt", timeStr)
137139
case OutputFormatJSON:
138140
c.OutputFile = fmt.Sprintf("disk_health_%s.json", timeStr)
141+
case OutputFormatHTML:
142+
c.OutputFile = fmt.Sprintf("disk_health_%s.html", timeStr)
139143
}
140144
}

internal/output/formatter.go

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,11 @@ const (
6060
OptionOrientation = "orientation" // 方向
6161
OptionIncludeCover = "include_cover" // 是否包含封面
6262
OptionFontSize = "font_size" // 字体大小
63+
64+
// HTML格式特定选项
65+
OptionTemperatureBar = "temperature_bar" // 显示视觉温度指示器
66+
OptionEnableInteractivity = "enable_interactivity" // 启用交互式功能(排序、过滤)
67+
OptionHtmlTitle = "html_title" // HTML页面标题
6368
)
6469

6570
// 边框样式常量
@@ -312,13 +317,44 @@ func NewFormatter(format string, options map[string]interface{}) (OutputFormatte
312317
return nil, fmt.Errorf("PDF格式输出暂未实现,请使用文本格式输出")
313318
case "text", "txt", "t":
314319
return NewTextFormatter(options), nil
320+
case "html", "h":
321+
return NewHTMLFormatter(options), nil
315322
default:
316323
return nil, fmt.Errorf("不支持的输出格式: %s", format)
317324
}
318325
}
319326

327+
// FormatSciNotation 将科学计数法转换为人类可读格式
328+
func FormatSciNotation(size string) string {
329+
// 尝试解析为科学计数法
330+
var value float64
331+
if _, err := fmt.Sscanf(size, "%e", &value); err == nil {
332+
// 转换为适当的单位
333+
units := []string{"B", "KB", "MB", "GB", "TB", "PB"}
334+
unitIndex := 0
335+
336+
for value >= 1024 && unitIndex < len(units)-1 {
337+
value /= 1024
338+
unitIndex++
339+
}
340+
341+
// 根据数值大小选择合适的精度
342+
if value < 10 {
343+
return fmt.Sprintf("%.2f %s", value, units[unitIndex])
344+
} else if value < 100 {
345+
return fmt.Sprintf("%.1f %s", value, units[unitIndex])
346+
} else {
347+
return fmt.Sprintf("%.0f %s", value, units[unitIndex])
348+
}
349+
}
350+
351+
// 如果不是科学计数法,返回原始值
352+
return size
353+
}
354+
320355
// 这些是将在各个具体格式化器中实现的函数声明
321356
var (
322357
NewPDFFormatter func(options map[string]interface{}) OutputFormatter
323358
NewTextFormatter func(options map[string]interface{}) OutputFormatter
359+
NewHTMLFormatter func(options map[string]interface{}) OutputFormatter
324360
)

0 commit comments

Comments
 (0)