mirror of https://github.com/Rudi9719/kbtui.git
Gregory 'Rudi' Rudolph
5 years ago
16 changed files with 388 additions and 332 deletions
@ -0,0 +1,88 @@
@@ -0,0 +1,88 @@
|
||||
// +build !rm_basic_commands allcommands setcmd
|
||||
|
||||
package main |
||||
|
||||
import ( |
||||
"fmt" |
||||
"io/ioutil" |
||||
"os" |
||||
|
||||
"github.com/pelletier/go-toml" |
||||
) |
||||
|
||||
func init() { |
||||
command := Command{ |
||||
Cmd: []string{"config"}, |
||||
Description: "Change various settings", |
||||
Help: "", |
||||
Exec: cmdConfig, |
||||
} |
||||
|
||||
RegisterCommand(command) |
||||
} |
||||
|
||||
func cmdConfig(cmd []string) { |
||||
var err error |
||||
switch { |
||||
case len(cmd) == 2: |
||||
if cmd[1] == "load" { |
||||
config, err = readConfig() |
||||
if err != nil { |
||||
printError(err.Error()) |
||||
return |
||||
} |
||||
printInfoF("Config file loaded: $TEXT", config.Colors.Message.Attachment.stylize(config.filepath)) |
||||
return |
||||
} |
||||
case len(cmd) > 2: |
||||
if cmd[1] == "load" { |
||||
config, err = readConfig(cmd[3]) |
||||
if err != nil { |
||||
printError(err.Error()) |
||||
return |
||||
} |
||||
printInfoF("Config file loaded: $TEXT", config.Colors.Message.Attachment.stylize(config.filepath)) |
||||
return |
||||
} |
||||
} |
||||
printError("Must pass a valid command") |
||||
} |
||||
|
||||
func readConfig(filepath ...string) (*Config, error) { |
||||
var result = new(Config) |
||||
var configFile string |
||||
var env bool |
||||
|
||||
// Load default config first, this way any values missing from the provided config file will remain the default value
|
||||
d := []byte(defaultConfig) |
||||
toml.Unmarshal(d, result) |
||||
|
||||
switch len(filepath) { |
||||
case 0: |
||||
configFile, env = os.LookupEnv("KBTUI_CFG") |
||||
if !env { |
||||
configFile = "~/.config/kbtui.toml" |
||||
if _, err := os.Stat(configFile); os.IsNotExist(err) { |
||||
configFile = "kbtui.toml" |
||||
} |
||||
} |
||||
default: |
||||
configFile = filepath[0] |
||||
if _, err := os.Stat(configFile); os.IsNotExist(err) { |
||||
return result, fmt.Errorf("Unable to load config: %s not found", configFile) |
||||
} |
||||
} |
||||
|
||||
f, err := ioutil.ReadFile(configFile) |
||||
if err != nil { |
||||
f = []byte(defaultConfig) |
||||
} |
||||
|
||||
err = toml.Unmarshal(f, result) |
||||
if err != nil { |
||||
return result, err |
||||
} |
||||
|
||||
result.filepath = configFile |
||||
return result, nil |
||||
} |
@ -1,125 +0,0 @@
@@ -1,125 +0,0 @@
|
||||
// +build !rm_basic_commands allcommands setcmd
|
||||
|
||||
package main |
||||
|
||||
import ( |
||||
"fmt" |
||||
"os" |
||||
"strings" |
||||
|
||||
"github.com/pelletier/go-toml" |
||||
) |
||||
|
||||
func init() { |
||||
command := Command{ |
||||
Cmd: []string{"set", "config"}, |
||||
Description: "Change various settings", |
||||
Help: "", |
||||
Exec: cmdSet, |
||||
} |
||||
|
||||
RegisterCommand(command) |
||||
} |
||||
func printSetting(cmd []string) { |
||||
switch cmd[1] { |
||||
case "load": |
||||
loadFromToml() |
||||
case "downloadPath": |
||||
printInfo(fmt.Sprintf("Setting for %s -> %s", cmd[1], downloadPath)) |
||||
case "outputFormat": |
||||
printInfo(fmt.Sprintf("Setting for %s -> %s", cmd[1], outputFormat)) |
||||
case "dateFormat": |
||||
printInfo(fmt.Sprintf("Setting for %s -> %s", cmd[1], dateFormat)) |
||||
case "timeFormat": |
||||
printInfo(fmt.Sprintf("Setting for %s -> %s", cmd[1], timeFormat)) |
||||
case "cmdPrefix": |
||||
printInfo(fmt.Sprintf("Setting for %s -> %s", cmd[1], cmdPrefix)) |
||||
default: |
||||
printError(fmt.Sprintf("Unknown config value %s", cmd[1])) |
||||
} |
||||
} |
||||
func cmdSet(cmd []string) { |
||||
if len(cmd) < 2 { |
||||
printError("No config value specified") |
||||
return |
||||
} |
||||
if len(cmd) < 3 { |
||||
printSetting(cmd) |
||||
return |
||||
} |
||||
switch cmd[1] { |
||||
case "downloadPath": |
||||
if len(cmd) != 3 { |
||||
printError("Invalid download path.") |
||||
} |
||||
downloadPath = cmd[2] |
||||
case "outputFormat": |
||||
outputFormat = strings.Join(cmd[1:], " ") |
||||
case "dateFormat": |
||||
dateFormat = strings.Join(cmd[1:], " ") |
||||
case "timeFormat": |
||||
timeFormat = strings.Join(cmd[1:], " ") |
||||
case "cmdPrefix": |
||||
cmdPrefix = cmd[2] |
||||
default: |
||||
printError(fmt.Sprintf("Unknown config value %s", cmd[1])) |
||||
} |
||||
|
||||
} |
||||
func loadFromToml() { |
||||
configFile, env := os.LookupEnv("KBTUI_CFG") |
||||
if !env { |
||||
configFile = "~/.config/kbtui.toml" |
||||
if _, err := os.Stat(configFile); os.IsNotExist(err) { |
||||
configFile = "kbtui.toml" |
||||
} |
||||
|
||||
} |
||||
printInfoF("Loading config from toml: $TEXT", messageAttachmentColor.stylize(configFile)) |
||||
config, err := toml.LoadFile(configFile) |
||||
if err != nil { |
||||
printError(fmt.Sprintf("Could not read config file: %+v", err)) |
||||
return |
||||
} |
||||
colorless = config.GetDefault("Basics.colorless", false).(bool) |
||||
if config.Has("Basics.colorless") { |
||||
colorless = config.Get("Basics.colorless").(bool) |
||||
} |
||||
if config.Has("Basics.downloadPath") { |
||||
downloadPath = config.Get("Basics.downloadPath").(string) |
||||
} |
||||
if config.Has("Basics.cmdPrefix") { |
||||
cmdPrefix = config.Get("Basics.cmdPrefix").(string) |
||||
} |
||||
if config.Has("Formatting.outputFormat") { |
||||
outputFormat = config.Get("Formatting.outputFormat").(string) |
||||
} |
||||
if config.Has("Formatting.dateFormat") { |
||||
dateFormat = config.Get("Formatting.dateFormat").(string) |
||||
} |
||||
if config.Has("Formatting.timeFormat") { |
||||
timeFormat = config.Get("Formatting.timeFormat").(string) |
||||
} |
||||
channelsColor = styleFromConfig(config, "channels.basic") |
||||
|
||||
channelsHeaderColor = styleFromConfig(config, "channels.header") |
||||
channelUnreadColor = styleFromConfig(config, "channels.unread") |
||||
|
||||
mentionColor = styleFromConfig(config, "message.mention") |
||||
messageHeaderColor = styleFromConfig(config, "message.header") |
||||
messageIDColor = styleFromConfig(config, "message.id") |
||||
messageTimeColor = styleFromConfig(config, "message.time") |
||||
messageSenderDefaultColor = styleFromConfig(config, "message.sender_default") |
||||
messageSenderDeviceColor = styleFromConfig(config, "message.sender_device") |
||||
messageBodyColor = styleFromConfig(config, "message.body") |
||||
messageAttachmentColor = styleFromConfig(config, "message.attachment") |
||||
messageLinkURLColor = styleFromConfig(config, "message.link_url") |
||||
messageLinkKeybaseColor = styleFromConfig(config, "message.link_keybase") |
||||
messageReactionColor = styleFromConfig(config, "message.reaction") |
||||
messageCodeColor = styleFromConfig(config, "message.code") |
||||
|
||||
feedColor = styleFromConfig(config, "feed.basic") |
||||
errorColor = styleFromConfig(config, "feed.error") |
||||
|
||||
RunCommand("clean") |
||||
} |
@ -0,0 +1,72 @@
@@ -0,0 +1,72 @@
|
||||
package main |
||||
|
||||
var defaultConfig = ` |
||||
[basics] |
||||
download_path = "/tmp/" |
||||
colorless = false |
||||
unicode_emojis = true |
||||
|
||||
# The prefix before evaluating a command |
||||
cmd_prefix = "/" |
||||
|
||||
[formatting] |
||||
# BASH-like PS1 variable equivalent |
||||
output_format = "┌──[$USER@$DEVICE] [$ID] [$DATE - $TIME]\n└╼ $MSG" |
||||
output_stream_format = "┌──[$USER@$DEVICE] [$ID] [$DATE - $TIME]\n└╼ $MSG" |
||||
output_mention_format = "┌──[$USER@$DEVICE] [$ID] [$DATE - $TIME]\n└╼ $MSG" |
||||
pm_format = "PM from $USER@$DEVICE: $MSG" |
||||
|
||||
# 02 = Day, Jan = Month, 06 = Year |
||||
date_format = "02Jan06" |
||||
|
||||
# 15 = hours, 04 = minutes, 05 = seconds |
||||
time_format = "15:04" |
||||
|
||||
|
||||
[colors] |
||||
[colors.channels] |
||||
[colors.channels.basic] |
||||
foreground = "normal" |
||||
[colors.channels.header] |
||||
foreground = "magenta" |
||||
bold = true |
||||
[colors.channels.unread] |
||||
foreground = "green" |
||||
italic = true |
||||
|
||||
[colors.message] |
||||
[colors.message.body] |
||||
foreground = "normal" |
||||
[colors.message.header] |
||||
foreground = "grey" |
||||
[colors.message.mention] |
||||
foreground = "green" |
||||
italic = true |
||||
bold = true |
||||
[colors.message.id] |
||||
foreground = "yellow" |
||||
[colors.message.time] |
||||
foreground = "magenta" |
||||
[colors.message.sender_default] |
||||
foreground = "cyan" |
||||
bold = true |
||||
[colors.message.sender_device] |
||||
foreground = "cyan" |
||||
[colors.message.attachment] |
||||
foreground = "red" |
||||
[colors.message.link_url] |
||||
foreground = "yellow" |
||||
[colors.message.link_keybase] |
||||
foreground = "yellow" |
||||
[colors.message.reaction] |
||||
foreground = "magenta" |
||||
bold = true |
||||
[colors.message.code] |
||||
foreground = "cyan" |
||||
background = "grey" |
||||
[colors.feed] |
||||
[colors.feed.basic] |
||||
foreground = "grey" |
||||
[colors.feed.error] |
||||
foreground = "red" |
||||
` |
@ -1,63 +1,68 @@
@@ -1,63 +1,68 @@
|
||||
[Basics] |
||||
downloadPath = "/tmp/" |
||||
[basics] |
||||
download_path = "/tmp/" |
||||
colorless = false |
||||
unicode_emojis = true |
||||
|
||||
# The prefix before evaluating a command |
||||
cmdPrefix = "/" |
||||
cmd_prefix = "/" |
||||
|
||||
[Formatting] |
||||
[formatting] |
||||
# BASH-like PS1 variable equivalent |
||||
outputFormat = "┌──[$USER@$DEVICE] [$ID] [$DATE - $TIME]\n└╼ $MSG" |
||||
output_format = "┌──[$USER@$DEVICE] [$ID] [$DATE - $TIME]\n└╼ $MSG" |
||||
output_stream_format = "┌──[$USER@$DEVICE] [$ID] [$DATE - $TIME]\n└╼ $MSG" |
||||
output_mention_format = "┌──[$USER@$DEVICE] [$ID] [$DATE - $TIME]\n└╼ $MSG" |
||||
pm_format = "PM from $USER@$DEVICE: $MSG" |
||||
|
||||
# 02 = Day, Jan = Month, 06 = Year |
||||
dateFormat = "02Jan06" |
||||
date_format = "02Jan06" |
||||
|
||||
# 15 = hours, 04 = minutes, 05 = seconds |
||||
timeFormat = "15:04" |
||||
time_format = "15:04" |
||||
|
||||
|
||||
[Colors] |
||||
[Colors.channels] |
||||
[Colors.channels.basic] |
||||
[colors] |
||||
[colors.channels] |
||||
[colors.channels.basic] |
||||
foreground = "normal" |
||||
[Colors.channels.header] |
||||
[colors.channels.header] |
||||
foreground = "magenta" |
||||
bold = true |
||||
[Colors.channels.unread] |
||||
[colors.channels.unread] |
||||
foreground = "green" |
||||
italic = true |
||||
|
||||
[Colors.message] |
||||
[Colors.message.body] |
||||
[colors.message] |
||||
[colors.message.body] |
||||
foreground = "normal" |
||||
[Colors.message.header] |
||||
[colors.message.header] |
||||
foreground = "grey" |
||||
[Colors.message.mention] |
||||
[colors.message.mention] |
||||
foreground = "green" |
||||
italic = true |
||||
bold = true |
||||
[Colors.message.id] |
||||
[colors.message.id] |
||||
foreground = "yellow" |
||||
[Colors.message.time] |
||||
[colors.message.time] |
||||
foreground = "magenta" |
||||
[Colors.message.sender_default] |
||||
[colors.message.sender_default] |
||||
foreground = "cyan" |
||||
bold = true |
||||
[Colors.message.sender_device] |
||||
[colors.message.sender_device] |
||||
foreground = "cyan" |
||||
[Colors.message.attachment] |
||||
[colors.message.attachment] |
||||
foreground = "red" |
||||
[Colors.message.link_url] |
||||
[colors.message.link_url] |
||||
foreground = "yellow" |
||||
[Colors.message.link_keybase] |
||||
[colors.message.link_keybase] |
||||
foreground = "yellow" |
||||
[Colors.message.reaction] |
||||
[colors.message.reaction] |
||||
foreground = "magenta" |
||||
bold = true |
||||
[Colors.message.code] |
||||
[colors.message.code] |
||||
foreground = "cyan" |
||||
background = "grey" |
||||
[Colors.feed] |
||||
[Colors.feed.basic] |
||||
[colors.feed] |
||||
[colors.feed.basic] |
||||
foreground = "grey" |
||||
[Colors.feed.error] |
||||
foreground = "red" |
||||
[colors.feed.error] |
||||
foreground = "red" |
||||
|
@ -1,40 +0,0 @@
@@ -1,40 +0,0 @@
|
||||
package main |
||||
|
||||
// Path where Downloaded files will default to
|
||||
var downloadPath = "/tmp/" |
||||
|
||||
var colorless bool = false |
||||
var channelsColor = basicStyle |
||||
var channelUnreadColor = channelsColor.withForeground(green).withItalic() |
||||
var channelsHeaderColor = channelsColor.withForeground(magenta).withBold() |
||||
|
||||
var mentionColor = basicStyle.withForeground(green) |
||||
var messageHeaderColor = basicStyle.withForeground(grey) |
||||
var messageIDColor = basicStyle.withForeground(yellow) |
||||
var messageTimeColor = basicStyle.withForeground(magenta) |
||||
var messageSenderDefaultColor = basicStyle.withForeground(cyan) |
||||
var messageSenderDeviceColor = messageSenderDefaultColor |
||||
var messageBodyColor = basicStyle |
||||
var messageAttachmentColor = basicStyle.withForeground(red) |
||||
var messageLinkURLColor = basicStyle.withForeground(yellow) |
||||
var messageLinkKeybaseColor = basicStyle.withForeground(yellow) |
||||
var messageReactionColor = basicStyle.withForeground(magenta) |
||||
var messageCodeColor = basicStyle.withBackground(grey).withForeground(cyan) |
||||
|
||||
var feedColor = basicStyle.withForeground(grey) |
||||
var errorColor = basicStyle.withForeground(red) |
||||
|
||||
// BASH-like PS1 variable equivalent
|
||||
var outputFormat = "┌──[$USER@$DEVICE] [$ID] [$DATE - $TIME]\n└╼ $MSG" |
||||
var outputStreamFormat = "┌──[$TEAM] [$USER@$DEVICE] [$ID] [$DATE - $TIME]\n└╼ $MSG" |
||||
var mentionFormat = outputStreamFormat |
||||
var pmFormat = "PM from $USER@$DEVICE: $MSG" |
||||
|
||||
// 02 = Day, Jan = Month, 06 = Year
|
||||
var dateFormat = "02Jan06" |
||||
|
||||
// 15 = hours, 04 = minutes, 05 = seconds
|
||||
var timeFormat = "15:04" |
||||
|
||||
// The prefix before evaluating a command
|
||||
var cmdPrefix = "/" |
Loading…
Reference in new issue