-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4171ee7
commit d2f9707
Showing
4 changed files
with
118 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package config | ||
|
||
import ( | ||
"fmt" | ||
"regexp" | ||
"strings" | ||
) | ||
|
||
// Mappings is a slice of Mapping pointers | ||
type Mappings []*Mapping | ||
|
||
// Mapping is a struct of match instructions | ||
type Mapping struct { | ||
Search string `yaml:"search"` | ||
Color *string `yaml:"color"` | ||
Format *string `yaml:"format"` | ||
|
||
re *regexp.Regexp | ||
} | ||
|
||
// Replace will replace the string with the formatted string | ||
func (m *Mapping) Replace(s string) (string, bool) { | ||
if m.Color != nil { | ||
if c, ok := Config.Colors[*m.Color]; ok { | ||
return fmt.Sprintf("\033[%s%s\033[0m", c, s), true | ||
} | ||
} | ||
|
||
if m.Format != nil { | ||
var format = *m.Format | ||
for k, v := range Config.Colors { | ||
format = strings.Replace(format, "\\"+k, "\033["+v, -1) | ||
} | ||
v := m.re.ReplaceAllString(s, format+"\033[0m") | ||
return v, true | ||
} | ||
|
||
return "", false | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
package config | ||
|
||
import ( | ||
"log" | ||
"os" | ||
"regexp" | ||
|
||
"gopkg.in/yaml.v3" | ||
) | ||
|
||
// Parse will parse the configuration. | ||
func Parse(f string) error { | ||
file, err := os.ReadFile(f) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
var config Configuration | ||
if err = yaml.Unmarshal(file, &config); err != nil { | ||
return err | ||
} | ||
|
||
for _, m := range config.Mappings { | ||
r, err := regexp.Compile(m.Search) | ||
if err != nil { | ||
log.Printf("Skipping invalid configuration for: %s", m.Search) | ||
continue | ||
} | ||
m.re = r | ||
} | ||
|
||
Config = &config | ||
return nil | ||
} |