feat: add GetManPage function and integrate parent process name in ProcessDetail

This commit is contained in:
Jonathan Atta
2026-03-11 17:26:51 +01:00
parent 9ba9b80b8b
commit c2aecae867
6 changed files with 227 additions and 35 deletions

View File

@@ -211,6 +211,7 @@ type ProcessDetail struct {
Username string `json:"username"`
CreatedAt int64 `json:"created_at"` // unix ms
ParentPID int32 `json:"parent_pid"`
ParentName string `json:"parent_name"`
Nice int32 `json:"nice"`
NumThreads int32 `json:"num_threads"`
NumFDs int32 `json:"num_fds"`
@@ -258,6 +259,13 @@ func FetchProcessDetail(pid int32) (*ProcessDetail, error) {
}
if v, e := p.Ppid(); e == nil {
d.ParentPID = v
if d.ParentPID > 0 {
if pp, err2 := ps.NewProcess(d.ParentPID); err2 == nil {
if pname, err2 := pp.Name(); err2 == nil {
d.ParentName = pname
}
}
}
}
if v, e := p.Nice(); e == nil {
d.Nice = v
@@ -304,4 +312,30 @@ func KillProcess(pid int32, force bool) error {
return p.Kill()
}
return p.Terminate()
}
// GetManPage returns the plain-text man page for the given command name.
// Returns an empty string if man is not installed or the page does not exist.
func GetManPage(name string) string {
ctx, cancel := context.WithTimeout(context.Background(), 4*time.Second)
defer cancel()
cmd := exec.CommandContext(ctx, "man", "-P", "cat", name)
out, err := cmd.Output()
if err != nil {
return ""
}
// Strip nroff backspace-based bold/underline formatting (char + backspace + char)
raw := []byte(out)
cleaned := make([]byte, 0, len(raw))
for i := 0; i < len(raw); i++ {
if i+1 < len(raw) && raw[i+1] == '\b' {
i++ // skip formatting char and backspace; the real char follows
} else {
cleaned = append(cleaned, raw[i])
}
}
text := strings.TrimSpace(string(cleaned))
if len(text) > 12000 {
text = text[:12000] + "\n…[truncated]"
}
return text
}