diff options
| -rw-r--r-- | pkg/i3status/battery.go | 4 | ||||
| -rw-r--r-- | pkg/i3status/main.go | 29 | ||||
| -rw-r--r-- | pkg/i3status/net.go | 58 |
3 files changed, 82 insertions, 9 deletions
diff --git a/pkg/i3status/battery.go b/pkg/i3status/battery.go index 0b6979d..ec2aa88 100644 --- a/pkg/i3status/battery.go +++ b/pkg/i3status/battery.go @@ -57,10 +57,10 @@ func getBatteryStatus() []Block { color = "#AA0000" } - return section(Block{ + return section("bat", Block{ Name: "battery", Position: PositionBattery, - FullText: fmt.Sprintf("⚡ %d%%", energyPerc), + FullText: fmt.Sprintf("%d%%", energyPerc), Urgent: energyPerc < 10 && !charging, Color: color, }) diff --git a/pkg/i3status/main.go b/pkg/i3status/main.go index a0c4342..4af726a 100644 --- a/pkg/i3status/main.go +++ b/pkg/i3status/main.go @@ -43,8 +43,8 @@ func Exec(args []string) int { //prepare clock (this is inline instead of split into a separate function //because the wallclock also drives the main loop's clock, see below) now := time.Now() - currentBlocks["clock"] = section( - Block{ + currentBlocks["clock"] = []Block{ + { Name: "clock", Instance: "date", Position: PositionClock, @@ -53,16 +53,17 @@ func Exec(args []string) int { Color: "#AAAAAA", SeparatorBlockWidth: 6, }, - Block{ + { Name: "clock", Instance: "time", Position: PositionClock, FullText: now.Format("15:04:05"), }, - ) + } //prepare other blocks currentBlocks["battery"] = getBatteryStatus() + currentBlocks["network"] = getNetworkStatus() //put blocks in rendering order var allBlocks []Block @@ -116,6 +117,12 @@ func (b byPositionAndInstance) Len() int { return len(b) } func (b byPositionAndInstance) Swap(i, j int) { b[i], b[j] = b[j], b[i] } func (b byPositionAndInstance) Less(i, j int) bool { if b[i].Position == b[j].Position { + if b[i].Instance == "_caption" { + return true + } + if b[j].Instance == "_caption" { + return false + } return b[i].Instance < b[j].Instance } return b[i].Position < b[j].Position @@ -137,12 +144,13 @@ type Position int //Acceptable values for Position, from left to right. const ( PositionNone Position = iota + PositionNetwork PositionBattery PositionClock ) -//Order the given blocks byPositionAndInstance, then add a separator to the last one. -func section(blocks ...Block) []Block { +//Order the given blocks byPositionAndInstance, then add a separator to the last one, then add a caption block of the same style in front. +func section(caption string, blocks ...Block) []Block { if len(blocks) == 0 { return nil } @@ -150,5 +158,12 @@ func section(blocks ...Block) []Block { last := len(blocks) - 1 blocks[last].Separator = true blocks[last].SeparatorBlockWidth = 15 - return blocks + + return append([]Block{{ + Name: blocks[0].Name, + Position: blocks[0].Position, + Instance: "_caption", + FullText: caption, + Color: blocks[0].Color, + }}, blocks...) } diff --git a/pkg/i3status/net.go b/pkg/i3status/net.go new file mode 100644 index 0000000..ebdacf8 --- /dev/null +++ b/pkg/i3status/net.go @@ -0,0 +1,58 @@ +/******************************************************************************* +* +* Copyright 2017 Stefan Majewsky <majewsky@gmx.net> +* +* This program is free software: you can redistribute it and/or modify it under +* the terms of the GNU General Public License as published by the Free Software +* Foundation, either version 3 of the License, or (at your option) any later +* version. +* +* This program is distributed in the hope that it will be useful, but WITHOUT ANY +* WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR +* A PARTICULAR PURPOSE. See the GNU General Public License for more details. +* +* You should have received a copy of the GNU General Public License along with +* this program. If not, see <http://www.gnu.org/licenses/>. +* +*******************************************************************************/ + +package i3status + +import ( + "net" + "regexp" + "strings" +) + +var networkPortRx = regexp.MustCompile(`:\d+$`) +var networkMaskRx = regexp.MustCompile(`/\d+$`) + +func getNetworkStatus() []Block { + addrs, err := net.InterfaceAddrs() + if err != nil { + return nil + } + addrStrs := make([]string, 0, len(addrs)) + for _, addr := range addrs { + str := addr.String() + //remove uninteresting parts + str = networkPortRx.ReplaceAllString(str, "") + str = networkMaskRx.ReplaceAllString(str, "") + //ignore IPv6 for now + if strings.ContainsRune(str, ':') { + continue + } + //ignore uninteresting addrs + if strings.HasPrefix(str, "127.") { + continue + } + addrStrs = append(addrStrs, str) + } + + return section("ip", Block{ + Name: "network", + Position: PositionNetwork, + FullText: strings.Join(addrStrs, " "), + Color: "#00AAAA", + }) +} |
