Skip to content

Commit 59ed9c2

Browse files
nwykesJanDeDobbeleer
authored andcommitted
feat: add os segment
1 parent 6e66501 commit 59ed9c2

File tree

5 files changed

+97
-0
lines changed

5 files changed

+97
-0
lines changed

docs/docs/segment-os.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
---
2+
id: os
3+
title: os
4+
sidebar_label: OS
5+
---
6+
7+
## What
8+
9+
Display OS specific info. Defaults to Icon.
10+
11+
## Sample Configuration
12+
13+
```json
14+
{
15+
"type": "os",
16+
"style": "plain",
17+
"foreground": "#26C6DA",
18+
"background": "#546E7A",
19+
"properties": {
20+
"postfix": "",
21+
"macos": "mac"
22+
}
23+
}
24+
```
25+
26+
## Properties
27+
28+
- macos: `string` - the string to use for macOS - defaults to macOS icon
29+
- linux: `string` - the icon to use for Linux - defaults to Linux icon
30+
- windows: `string` - the icon to use for Windows - defaults to Windows icon
31+

docs/sidebars.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ module.exports = {
1919
"exit",
2020
"git",
2121
"node",
22+
"os",
2223
"path",
2324
"python",
2425
"root",

segment.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,8 @@ const (
5656
ShellInfo SegmentType = "shell"
5757
//Node writes which node version is currently active
5858
Node SegmentType = "node"
59+
//OS write os specific icon
60+
Os SegmentType = "os"
5961
//Powerline writes it Powerline style
6062
Powerline SegmentStyle = "powerline"
6163
//Plain writes it without ornaments
@@ -107,6 +109,7 @@ func (segment *Segment) mapSegmentWithWriter(env environmentInfo) (*properties,
107109
Spotify: &spotify{},
108110
ShellInfo: &shell{},
109111
Node: &node{},
112+
Os: &osInfo{},
110113
}
111114
if writer, ok := functions[segment.Type]; ok {
112115
props := &properties{

segment_os.go

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package main
2+
3+
type osInfo struct {
4+
props *properties
5+
env environmentInfo
6+
}
7+
8+
const (
9+
//Macos the string to use for macOS
10+
macOS Property = "macos"
11+
//LinuxIcon the string to use for linux
12+
Linux Property = "linux"
13+
//WindowsIcon the icon to use for windows
14+
Windows Property = "windows"
15+
)
16+
17+
func (n *osInfo) enabled() bool {
18+
return true
19+
}
20+
21+
func (n *osInfo) string() string {
22+
goos := n.env.getRuntimeGOOS()
23+
switch goos {
24+
case "windows":
25+
return n.props.getString(Windows, "\uE62A")
26+
case "darwin":
27+
return n.props.getString(macOS, "\uF179")
28+
case "linux":
29+
return n.props.getString(Linux, "\uF17C")
30+
default:
31+
return ""
32+
}
33+
}
34+
35+
func (n *osInfo) init(props *properties, env environmentInfo) {
36+
n.props = props
37+
n.env = env
38+
}

segment_os_test.go

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package main
2+
3+
import (
4+
"testing"
5+
6+
"github.com/stretchr/testify/assert"
7+
)
8+
9+
func TestOsInfo(t *testing.T) {
10+
env := new(MockedEnvironment)
11+
env.On("getRuntimeGOOS", nil).Return("windows")
12+
props := &properties{
13+
values: map[Property]interface{}{Windows: "win"},
14+
foreground: "#fff",
15+
background: "#000",
16+
}
17+
osInfo := &osInfo{
18+
env: env,
19+
props: props,
20+
}
21+
want := "win"
22+
got := osInfo.string()
23+
assert.Equal(t, want, got)
24+
}

0 commit comments

Comments
 (0)