Skip to content

Commit

Permalink
Implement HUD
Browse files Browse the repository at this point in the history
  • Loading branch information
fengye87 committed May 23, 2022
1 parent 7541180 commit 05d7020
Show file tree
Hide file tree
Showing 10 changed files with 382 additions and 2 deletions.
17 changes: 17 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: build

on:
- push
- pull_request

jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- uses: actions/setup-go@v2
with:
go-version: 1.17.8

- run: make
18 changes: 18 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
name: release

on:
push:
tags:
- v*

jobs:
release:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2

- run: go build -o hud main.go

- uses: softprops/action-gh-release@v1
with:
files: hud
22 changes: 22 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# https://raw.githubusercontent.com/github/gitignore/master/Go.gitignore
# If you prefer the allow list template instead of the deny list, see community template:
# https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore
#
# Binaries for programs and plugins
*.exe
*.exe~
*.dll
*.so
*.dylib

# Test binary, built with `go test -c`
*.test

# Output of the go coverage tool, specifically when used with LiteIDE
*.out

# Dependency directories (remove the comment below to include it)
# vendor/

# Go workspace file
go.work
7 changes: 7 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
all: test

fmt:
go fmt ./...

test:
go test ./... -coverprofile cover.out
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
# hud
Head-up Display for Linux
# HUD: Head-up Display for Linux

[![build](https://github.com/fengye87/hud/actions/workflows/build.yml/badge.svg)](https://github.com/fengye87/hud/actions/workflows/build.yml)
26 changes: 26 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module github.com/fengye87/hud

go 1.17

require (
github.com/docker/libnetwork v0.8.0-dev.2.0.20220315221942-339b972b464e
github.com/gdamore/tcell/v2 v2.5.1
github.com/rivo/tview v0.0.0-20220307222120-9994674d60a8
github.com/vishvananda/netlink v1.1.0
gopkg.in/ini.v1 v1.66.4
)

require (
github.com/docker/docker v20.10.16+incompatible // indirect
github.com/gdamore/encoding v1.0.0 // indirect
github.com/ishidawataru/sctp v0.0.0-20210707070123-9a39160e9062 // indirect
github.com/lucasb-eyer/go-colorful v1.2.0 // indirect
github.com/mattn/go-runewidth v0.0.13 // indirect
github.com/rivo/uniseg v0.2.0 // indirect
github.com/sirupsen/logrus v1.8.1 // indirect
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df // indirect
golang.org/x/sys v0.0.0-20220318055525-2edf467146b5 // indirect
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d // indirect
golang.org/x/text v0.3.7 // indirect
gotest.tools/v3 v3.2.0 // indirect
)
73 changes: 73 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/docker/docker v20.10.16+incompatible h1:2Db6ZR/+FUR3hqPMwnogOPHFn405crbpxvWzKovETOQ=
github.com/docker/docker v20.10.16+incompatible/go.mod h1:eEKB0N0r5NX/I1kEveEz05bcu8tLC/8azJZsviup8Sk=
github.com/docker/libnetwork v0.8.0-dev.2.0.20220315221942-339b972b464e h1:WdqMKJqJB5cI6C3RXQdrS33DreJy8Aio6Zi0CX+/oQY=
github.com/docker/libnetwork v0.8.0-dev.2.0.20220315221942-339b972b464e/go.mod h1:93m0aTqz6z+g32wla4l4WxTrdtvBRmVzYRkYvasA5Z8=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell/v2 v2.4.1-0.20210905002822-f057f0a857a1/go.mod h1:Az6Jt+M5idSED2YPGtwnfJV0kXohgdCBPmHGSYc1r04=
github.com/gdamore/tcell/v2 v2.5.1 h1:zc3LPdpK184lBW7syF2a5C6MV827KmErk9jGVnmsl/I=
github.com/gdamore/tcell/v2 v2.5.1/go.mod h1:wSkrPaXoiIWZqW/g7Px4xc79di6FTcpB8tvaKJ6uGBo=
github.com/google/go-cmp v0.5.5 h1:Khx7svrCpmxxtHBq5j2mp/xVjsi8hQMfNLvJFAlrGgU=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/ishidawataru/sctp v0.0.0-20210707070123-9a39160e9062 h1:G1+wBT0dwjIrBdLy0MIG0i+E4CQxEnedHXdauJEIH6g=
github.com/ishidawataru/sctp v0.0.0-20210707070123-9a39160e9062/go.mod h1:co9pwDoBCm1kGxawmb4sPq0cSIOOWNPT4KnHotMP1Zg=
github.com/lucasb-eyer/go-colorful v1.2.0 h1:1nnpGOrhyZZuNyfu1QjKiUICQ74+3FNCN69Aj6K7nkY=
github.com/lucasb-eyer/go-colorful v1.2.0/go.mod h1:R4dSotOR9KMtayYi1e77YzuveK+i7ruzyGqttikkLy0=
github.com/mattn/go-runewidth v0.0.13 h1:lTGmDsbAYt5DmK6OnoV7EuIF1wEIFAcxld6ypU4OSgU=
github.com/mattn/go-runewidth v0.0.13/go.mod h1:Jdepj2loyihRzMpdS35Xk/zdY8IAYHsh153qUoGf23w=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/rivo/tview v0.0.0-20220307222120-9994674d60a8 h1:xe+mmCnDN82KhC010l3NfYlA8ZbOuzbXAzSYBa6wbMc=
github.com/rivo/tview v0.0.0-20220307222120-9994674d60a8/go.mod h1:WIfMkQNY+oq/mWwtsjOYHIZBuwthioY2srOmljJkTnk=
github.com/rivo/uniseg v0.2.0 h1:S1pD9weZBuJdFmowNwbpi7BJ8TNftyUImj/0WQi72jY=
github.com/rivo/uniseg v0.2.0/go.mod h1:J6wj4VEh+S6ZtnVlnTBMWIodfgj8LQOQFoIToxlJtxc=
github.com/sirupsen/logrus v1.8.1 h1:dJKuHgqk1NNQlqoA6BTlM1Wf9DOH3NBjQyu0h9+AZZE=
github.com/sirupsen/logrus v1.8.1/go.mod h1:yWOB1SBYBC5VeMP7gHvWumXLIWorT60ONWic61uBYv0=
github.com/spf13/pflag v1.0.3/go.mod h1:DYY7MBk1bdzusC3SYhjObp+wFpr4gzcvqqNjLnInEg4=
github.com/stretchr/testify v1.2.2 h1:bSDNvY7ZPG5RlJ8otE/7V6gMiyenm9RtJ7IUVIAoJ1w=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/vishvananda/netlink v1.1.0 h1:1iyaYNBLmP6L0220aDnYQpo1QEV4t4hJ+xEEhhJH8j0=
github.com/vishvananda/netlink v1.1.0/go.mod h1:cTgwzPIzzgDAYoQrMm0EdrjRUBkTqKYppBueQtXaqoE=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df h1:OviZH7qLw/7ZovXvuNyL3XQl8UFofeikI1NW1Gypu7k=
github.com/vishvananda/netns v0.0.0-20191106174202-0a2b9b5464df/go.mod h1:JP3t17pCcGlemwknint6hfoeCVQrEMVwxRLRjXpq+BU=
github.com/yuin/goldmark v1.2.1/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/mod v0.3.0/go.mod h1:s0Qsj1ACt9ePp/hMypM3fl4fZqREWJwdYDEqhRiZZUA=
golang.org/x/net v0.0.0-20190404232315-eb5bcb51f2a3/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/net v0.0.0-20190620200207-3b0461eec859/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s=
golang.org/x/net v0.0.0-20201021035429-f5854403a974/go.mod h1:sp8m0HH+o8qH0wwXwYZr8TS3Oi6o0r6Gce1SSxlDquU=
golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20190606203320-7fc4e5ec1444/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20200930185726-fdedc70b468f/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210119212857-b64e53b001e4/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210309074719-68d13333faf2/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20220318055525-2edf467146b5 h1:saXMvIOKvRFwbOMicHXr0B1uwoxq9dGmLe5ExMES6c4=
golang.org/x/sys v0.0.0-20220318055525-2edf467146b5/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/term v0.0.0-20201210144234-2321bbc49cbf/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d h1:SZxvLBoTP5yHO3Frd4z4vrF+DBX9vMVanchswa69toE=
golang.org/x/term v0.0.0-20210220032956-6a3ed077a48d/go.mod h1:bj7SfCRtBDWHUb9snDiAeCFNEtKQo2Wmx5Cou7ajbmo=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.6/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
gopkg.in/ini.v1 v1.66.4 h1:SsAcf+mM7mRZo2nJNGt8mZCjG8ZRaNGMURJw7BsIST4=
gopkg.in/ini.v1 v1.66.4/go.mod h1:pNLf8WUiyNEtQjuu5G5vTm06TEv9tsIgeAvK8hOrP4k=
gotest.tools/v3 v3.2.0 h1:I0DwBVMGAx26dttAj1BtJLAkVGncrkkUXfJLC4Flt/I=
gotest.tools/v3 v3.2.0/go.mod h1:Mcr9QNxkg0uMvy/YElmo4SpXgJKWgQvYrT7Kw5RzJ1A=
10 changes: 10 additions & 0 deletions hud.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[Service]
ExecStart=/usr/local/bin/hud
StandardInput=tty
StandardOutput=tty
TTYPath=/dev/tty1
TTYReset=yes
Restart=always

[Install]
WantedBy=multi-user.target
8 changes: 8 additions & 0 deletions install.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash

wget -O /usr/local/bin/hud https://github.com/fengye87/hud/releases/download/v0.1.0/hud
chmod +x /usr/local/bin/hud
wget -O /etc/systemd/system/hud.service https://raw.githubusercontent.com/fengye87/hud/main/hud.service

systemctl disable --now [email protected]
systemctl enable --now hud.service
198 changes: 198 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@
package main

import (
"fmt"
"net"
"os"
"os/exec"
"os/signal"
"strconv"
"strings"
"syscall"
"time"

"github.com/docker/libnetwork/resolvconf"
"github.com/docker/libnetwork/types"
"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
"github.com/vishvananda/netlink"
"gopkg.in/ini.v1"
)

const JournalMaxLines = 120

func main() {
release, err := getOSRelease()
if err != nil {
panic(err)
}

app := tview.NewApplication()
app.SetInputCapture(func(event *tcell.EventKey) *tcell.EventKey { return event })
if err := setupSignalHandler(app); err != nil {
panic(err)
}

headerTV := tview.NewTextView().SetTextAlign(tview.AlignCenter).SetTextColor(tcell.ColorTeal).SetChangedFunc(func() { app.Draw() })
headerTV.SetText("\n" + release)

infoTV := tview.NewTextView().SetDynamicColors(true).SetChangedFunc(func() { app.Draw() })

journalTV := tview.NewTextView().ScrollToEnd().SetChangedFunc(func() { app.Draw() })
journalTV.SetMaxLines(JournalMaxLines)
journalctl := exec.Command("journalctl", "--no-hostname", "-n", strconv.Itoa(JournalMaxLines), "-f")
journalctl.Stdout = journalTV
if err := journalctl.Start(); err != nil {
panic(err)
}

flex := tview.NewFlex().SetDirection(tview.FlexRow).AddItem(headerTV, 3, 0, false).AddItem(infoTV, 0, 0, false).AddItem(journalTV, 0, 1, false)

go func() {
hostnameInfo := []string{"Hostname", ""}
ipInfo := []string{"IP address", ""}
gatewayInfo := []string{"Gateway address", ""}
dnsInfo := []string{"DNS nameservers", ""}
timeInfo := []string{"Time", ""}
infoItems := [][]string{hostnameInfo, ipInfo, gatewayInfo, dnsInfo, timeInfo}
lastUpdateTime := time.Time{}

for {
if time.Now().After(lastUpdateTime.Add(time.Minute)) {
hostname, _ := getHostname()
hostnameInfo[1] = hostname

link, _ := getDefaultLink()
if link != nil {
addr, _ := getLinkAddr(link)
if addr != nil {
ipInfo[1] = addr.IPNet.String()
}

gateway, _ := getLinkGateway(link)
if gateway != nil {
gatewayInfo[1] = gateway.String()
}
}

nameservers, _ := getNameservers()
dnsInfo[1] = strings.Join(nameservers, " ")
}

timeInfo[1] = time.Now().Format(time.RFC1123Z)

_, _, infoTVWidth, _ := infoTV.GetRect()
var infoLines []string
for _, info := range infoItems {
label := info[0]
paddingLen := infoTVWidth/2 - len(label) - 2
if paddingLen < 0 {
paddingLen = 0
}

value := info[1]
if value == "" {
value = "[:red]NOT SET[:-]"
}

infoLines = append(infoLines, fmt.Sprintf("%s[::b]%s[::-]: %s", strings.Repeat(" ", paddingLen), label, value))
}

infoTV.SetText(strings.Join(infoLines, "\n"))
flex.ResizeItem(infoTV, len(infoLines)+1, 0)

time.Sleep(time.Second)
}
}()

if err := app.SetRoot(flex, true).Run(); err != nil {
panic(err)
}
}

func setupSignalHandler(app *tview.Application) error {
printkPath := "/proc/sys/kernel/printk"
printk, err := os.ReadFile(printkPath)
if err != nil {
return fmt.Errorf("get printk: %s", err)
}

sigs := make(chan os.Signal, 1)
signal.Notify(sigs, syscall.SIGINT, syscall.SIGTERM, os.Interrupt)
go func() {
<-sigs

os.WriteFile(printkPath, printk, 0644)
app.Stop()
}()

if err := os.WriteFile(printkPath, []byte("0 0 0 0"), 0644); err != nil {
return fmt.Errorf("set printk: %s", err)
}
return nil
}

func getOSRelease() (string, error) {
info, err := ini.Load("/etc/os-release")
if err != nil {
return "", fmt.Errorf("read OS info: %s", err)
}
return info.Section("").Key("PRETTY_NAME").String(), nil
}

func getHostname() (string, error) {
return os.Hostname()
}

func getDefaultLink() (netlink.Link, error) {
links, err := netlink.LinkList()
if err != nil {
return nil, fmt.Errorf("list links: %s", err)
}

for _, link := range links {
routes, err := netlink.RouteList(link, netlink.FAMILY_V4)
if err != nil {
return nil, fmt.Errorf("list routes of link %q: %s", link.Attrs().Name, err)
}

for _, route := range routes {
if route.Dst == nil {
return link, nil
}
}
}
return nil, nil
}

func getLinkAddr(link netlink.Link) (*netlink.Addr, error) {
addrs, err := netlink.AddrList(link, netlink.FAMILY_V4)
if err != nil {
return nil, fmt.Errorf("list addrs: %s", err)
}

if len(addrs) > 0 {
return &addrs[0], nil
}
return nil, nil
}

func getLinkGateway(link netlink.Link) (net.IP, error) {
routes, err := netlink.RouteList(link, netlink.FAMILY_V4)
if err != nil {
return nil, fmt.Errorf("list routes: %s", err)
}

if len(routes) > 0 {
return routes[0].Gw, nil
}
return nil, nil
}

func getNameservers() ([]string, error) {
rc, err := resolvconf.Get()
if err != nil {
return nil, fmt.Errorf("get resolvconf: %s", err)
}
return resolvconf.GetNameservers(rc.Content, types.IPv4), nil
}

0 comments on commit 05d7020

Please sign in to comment.