Skip to content

Commit ad0ec82

Browse files
committed
cleaner builtin implementation
1 parent cdd640a commit ad0ec82

File tree

5 files changed

+37
-50
lines changed

5 files changed

+37
-50
lines changed

builtins.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -5,26 +5,18 @@ import (
55
"time"
66
)
77

8-
func ExecuteBuiltIn(name string, args []string) []string {
9-
switch name {
10-
case "echo", "print":
11-
return BuiltInEcho(args)
12-
case "date", "time", "datetime":
13-
return BuiltInDateTime(args)
14-
}
15-
return []string{" builtin not found"}
16-
}
17-
18-
func BuiltInEcho(args []string) []string {
19-
return []string{strings.Join(args, " ")}
20-
}
8+
var BuiltIns map[string]func([]string) []string = map[string]func([]string) []string{
9+
"echo": func(args []string) []string {
10+
return []string{strings.Join(args, " ")}
11+
},
2112

22-
func BuiltInDateTime(args []string) []string {
23-
var format string
24-
if len(args) < 1 {
25-
format = "02.01.2006 (Mon) 15:04:05"
26-
} else {
27-
format = args[0]
28-
}
29-
return []string{time.Now().Format(format)}
13+
"time": func(args []string) []string {
14+
var format string
15+
if len(args) < 1 {
16+
format = "02.01.2006 (Mon) 15:04:05"
17+
} else {
18+
format = args[0]
19+
}
20+
return []string{time.Now().Format(format)}
21+
},
3022
}

config.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,9 @@ func DefaultConfig(msg string) Config {
4949
config := Config{}
5050
config.Modules = []Module{
5151
{"msg", "", "#0000ff", "#ffff00", " ", "",
52-
"echo", []string{msg}, 60, "none", true, 9, 0},
52+
"*echo", []string{msg}, 60, "none", true, 9, 0},
5353
{"time", "", "#ffffff", "#000000", " ", "",
54-
"date", []string{"+%d.%m.%Y - %R:%S"}, 1, "none", true, 9, 0},
54+
"*time", []string{}, 1, "none", true, 9, 0},
5555
{"kernel", "", "#880088", "#ccccee", " ", "",
5656
"uname", []string{"-r"}, 60, "none", true, 9, 0}}
5757
return config

docs/Configuration.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -91,12 +91,11 @@ ForegroundColor
9191
BackgroundColor
9292
```
9393

94-
If the command starts with `*`, it is interpreted as a name of a built-in module.
95-
Those are:
94+
If the command starts with `*`, it is interpreted as a name of a built-in module. Those are:
9695

97-
- `date` / `time` / `datetime`: the current date and time. Args: `["format"]`.
96+
- `*time`: the current date and time. Args: `["format"]`.
9897
Format is in the Go time format fashion (`02.01.2006 (Mon) 15:04:05`).
99-
- `echo` / `print`: print some text. Args: `["text", "text", "text"]`
98+
- `*echo`: print some text. Args: `["text", "text", ...]`
10099

101100
Possible values: any executable on your system / any builtin.
102101

events.go

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -56,32 +56,28 @@ func ReadInput() {
5656
func ListenFor(signalNumber int, blockName string) {
5757
channel := make(chan os.Signal, 1)
5858
signal.Notify(channel, syscall.Signal(SIGRTMIN+signalNumber))
59-
select {
60-
case <-channel:
61-
mu.Lock()
62-
UpdateModuleByName(blockName, 0, []string{})
63-
draw()
64-
mu.Unlock()
65-
ListenFor(signalNumber, blockName)
66-
}
59+
<-channel
60+
mu.Lock()
61+
UpdateModuleByName(blockName, 0, []string{})
62+
draw()
63+
mu.Unlock()
64+
ListenFor(signalNumber, blockName)
6765
}
6866

6967
func ListenToReloadConfig() {
7068
channel := make(chan os.Signal, 1)
7169
signal.Notify(channel, syscall.Signal(syscall.SIGUSR1))
72-
select {
73-
case <-channel:
74-
mu.Lock()
75-
fmt.Printf(
76-
`[{"full_text": "reloading config...", "color": "%s"}],`,
77-
config.Colors["WHITE"])
78-
config = LoadConfig()
79-
for i := 0; i < len(config.Modules); i++ {
80-
UpdateModule(i, 0, []string{})
81-
}
82-
draw()
83-
mu.Unlock()
84-
85-
ListenToReloadConfig()
70+
<-channel
71+
mu.Lock()
72+
fmt.Printf(
73+
`[{"full_text": "reloading config...", "color": "%s"}],`,
74+
config.Colors["WHITE"])
75+
config = LoadConfig()
76+
for i := 0; i < len(config.Modules); i++ {
77+
UpdateModule(i, 0, []string{})
8678
}
79+
draw()
80+
mu.Unlock()
81+
82+
ListenToReloadConfig()
8783
}

update.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ func UpdateModule(mod int, counter int, env []string) {
6262

6363
var lines []string
6464
if config.Modules[mod].Command[0] == '*' {
65-
lines = ExecuteBuiltIn(config.Modules[mod].Command[1:], config.Modules[mod].Args)
65+
lines = BuiltIns[config.Modules[mod].Command[1:]](config.Modules[mod].Args)
6666
} else {
6767
lines = ExecuteCommand(config.Modules[mod].Command, config.Modules[mod].Args, env)
6868
}

0 commit comments

Comments
 (0)