Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

exit code #4

Open
wants to merge 24 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added Shuttle.exe
Binary file not shown.
89 changes: 65 additions & 24 deletions prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,35 +8,60 @@ import (
"os"
"github.com/gookit/color"
)
// runtime variables
var(
osLogo string
os string
osLogo string = ""
platform string = ""
prompt string = ""
)
// config type
type config struct{
icon string
style string
seperateSegments bool
segmentSeperator string
colorBasedOnExitCode bool
showSomethingBeforePrompt bool
somethingBeforePrompt string
}
// config instance
var(
c config
)
func main() {
//configure before anything
viper.AutomaticEnv()
viper.SetConfigName("shuttle")
viper.SetConfigType("yaml")
// paths to look for the config file in
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME")
viper.AddConfigPath("$HOME/.config")
viper.AddConfigPath("$HOME/.config/shuttle")
viper.AddConfigPath("$HOME/.config")
viper.AddConfigPath("$HOME")
viper.AddConfigPath(".")
viper.ReadInConfig() // Find and read the config file
viper.SetDefault("prompt.icon", "$")
// detect enviorment
os = runtime.GOOS
switch os{
// default options
viper.SetDefault("icon", "$")
viper.SetDefault("style", "plaintext")
viper.SetDefault("seperateSegments", false)
viper.SetDefault("segmentSeperator","")
viper.SetDefault("colorBasedOnExitCode",false)
viper.SetDefault("showSomethingBeforePrompt",false)
viper.SetDefault("somethingBeforePrompt","")
err := viper.Unmarshal(&c)
if err != nil {
fmt.Println("Unable to decode config")
}
// detect env
platform = runtime.GOOS
switch platform{
case "windows":
osLogo = ""
prompt(osLogo)
case "darwin":
osLogo = ""
prompt(osLogo)
case "linux":
osLogo = ""
prompt(osLogo)
}
deploy()
}
func trimPath(cwd, home string) string {
var path string
Expand Down Expand Up @@ -64,7 +89,7 @@ func use(vals ...interface{}){
_ = val
}
}
func prompt(osLogo string) {
func deploy() {
// FG colors
red := color.FgRed.Render
green := color.FgGreen.Render
Expand All @@ -80,25 +105,41 @@ func prompt(osLogo string) {
bgBlue := color.BgBlue.Render
use(red,blue,bgGreen,bgWhite,green) // don`t complain about unused colors
osSym := bgRed(white(" "+osLogo+" "))
if(osLogo == ""){
if(platform == "windows"){
osSym = bgBlue(white(" "+osLogo+" "))
}else if(osLogo == ""){
}else if(platform == "linux"){
osSym = bgCyan(white(" "+osLogo+" "))
}
// render prompt
var prompt string
prompt = cyan("")
if(viper.Get("prompt.segments.os" == true){
if(c.showSomethingBeforePrompt){
prompt = cyan(string(c.somethingBeforePrompt))
}
if(viper.Get("segments.os") == true){
prompt = prompt + osSym
}
if(viper.Get("prompt.segments.cwd") == true){
if(viper.Get("segments.cwd") == true){
cwd , _ := os.Getwd()
homeVar := viper.Get("HOME")
prompt = prompt + bgYellow(white(" :"+""+trimPath(cwd,homeVar.(string))+" "))\
homeVar := os.Getenv("HOME")
prompt = prompt + bgYellow(white(" :"+""+trimPath(cwd,string(homeVar)+" ")))
}
code := os.Getenv("?")
if(c.colorBasedOnExitCode){
if(platform == "windows"){
if(code == "False"){
prompt = prompt + "" + red(c.icon) + ""
}else{
prompt = prompt + "" + c.icon + " "
}
}else{
if(code != "0"){
prompt = prompt + "" + red(c.icon) + " "
}else {
prompt = prompt + "" + c.icon + " "
}
}
}else{
prompt = prompt + "" + c.icon + " "
}
// icon
var icon string = viper.GetString("prompt.icon")
prompt = prompt + "" + cyan(icon) + " "
// print it out
fmt.Println(prompt)
}
24 changes: 13 additions & 11 deletions shuttle.yml
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
prompt:
style: "plaintext"
icon: ">"
seperateSegments: false
segmentSeperator: "this string won't be used at all , since seoerateSegents is false"
colorBasedOnExitCode: false
# show the prompt icon as red if the last code was non-zero , or $? is False in powershell(windows)
segments:
os: true
user : true
cwd : true
style: "plaintext"
icon: ">"
seperateSegments: false
segmentSeperator: "this string won't be used at all , since separateSegents is false"
# if seperateSegemnts is true , the seperator will be printed in cyan!!
colorBasedOnExitCode: false
# show the prompt icon as red if the last code was non-zero , or $? is False in powershell(windows)
showSomethingBeforePrompt: false
somethingBeforePrompt: "this string won't be used at all , since showSomethingBeforePrompt is false"
segments:
os: true
user : true
cwd : true