11package cmd
22
33import (
4+ "errors"
45 "fmt"
56 "github.com/khbminus/tscli/client"
67 "github.com/khbminus/tscli/config"
78 "github.com/khbminus/tscli/util"
89 "github.com/logrusorgru/aurora/v3"
10+ "github.com/olekukonko/tablewriter"
11+ "os"
12+ "path"
13+ "strconv"
914)
1015
1116const (
12- ConfigPath = "./ .tscli.local"
17+ ConfigName = ".tscli.local"
1318)
1419
20+ func GetConfig () (* config.Config , error ) {
21+ cwd , err := os .Getwd ()
22+ if err != nil {
23+ panic (err )
24+ }
25+ for {
26+ if cwd == "/" || cwd == "" {
27+ fmt .Println (aurora .Red ("Can't find a local config. Please run tscli local parse" ))
28+ return nil , errors .New ("no config found" )
29+ }
30+ if _ , err := os .Stat (cwd + "/" + ConfigName ); err == nil {
31+ return config .NewConfig (cwd + "/" + ConfigName )
32+ } else if os .IsNotExist (err ) {
33+ cwd = path .Dir (cwd )
34+ } else {
35+ panic (err )
36+ }
37+ }
38+ }
39+
1540func ShowLocalConfig () error {
16- cfg , err := config . NewConfig ( ConfigPath )
41+ cfg , err := GetConfig ( )
1742 if err != nil {
1843 return err
1944 }
20- var compilerName string = "Doesn't set"
45+ compilerName : = "Doesn't set"
2146 if cfg .Compilers != nil && cfg .DefaultLang != - 1 {
2247 compilerName = cfg .Compilers [cfg .DefaultLang ].CompilerName
2348 }
@@ -43,7 +68,7 @@ func ShowLocalConfig() error {
4368
4469func ParseConfig () error {
4570 fmt .Println (aurora .Yellow ("Getting new config..." ))
46- cfg , err := client .Instance .GetConfig (ConfigPath )
71+ cfg , err := client .Instance .GetConfig ("./" + ConfigName )
4772 if err != nil {
4873 return err
4974 }
@@ -64,9 +89,14 @@ func ChooseContest(contestId string) error {
6489 }
6590 index := - 1
6691 if contestId == "" {
92+ table := tablewriter .NewWriter (os .Stdout )
93+ table .SetHeader ([]string {"index" , "Id" , "Name" , "Started" , "Status" })
94+ table .SetAutoWrapText (false )
95+ table .SetAlignment (tablewriter .ALIGN_CENTER )
6796 for i , v := range contests {
68- fmt . Printf ( "%v) %v \n " , i , v )
97+ table . Append ([] string { strconv . Itoa ( i ), v . ContestId , v . ContestName , v . ContestStarted , v . ContestStatus } )
6998 }
99+ table .Render ()
70100 index = util .ChooseIndex (len (contests ))
71101 } else {
72102 for i , contest := range contests {
@@ -94,7 +124,7 @@ func ChooseContest(contestId string) error {
94124}
95125
96126func ChangeDefaultLang () error {
97- cfg , err := config . NewConfig ( ConfigPath )
127+ cfg , err := GetConfig ( )
98128 if err != nil {
99129 return err
100130 }
0 commit comments