4
4
"embed"
5
5
"errors"
6
6
"fmt"
7
+ "io"
7
8
"io/ioutil"
8
9
"log"
9
10
"os"
@@ -75,6 +76,7 @@ const (
75
76
LeaderTag
76
77
LeaderUser
77
78
LeaderWindow
79
+ LeaderLoadNewer
78
80
)
79
81
80
82
type Timeline struct {
@@ -431,21 +433,31 @@ func parseStyle(cfg *ini.File) Style {
431
433
style := Style {}
432
434
theme := cfg .Section ("style" ).Key ("theme" ).String ()
433
435
if theme != "none" && theme != "" {
434
- themes , err := getThemes ()
436
+ bundled , local , err := getThemes ()
435
437
if err != nil {
436
438
log .Fatalf ("Couldn't load themes. Error: %s\n " , err )
437
439
}
438
440
found := false
439
- for _ , t := range themes {
441
+ isLocal := false
442
+ for _ , t := range local {
440
443
if filepath .Base (t ) == fmt .Sprintf ("%s.ini" , theme ) {
441
444
found = true
445
+ isLocal = true
442
446
break
443
447
}
444
448
}
449
+ if ! found {
450
+ for _ , t := range bundled {
451
+ if filepath .Base (t ) == fmt .Sprintf ("%s.ini" , theme ) {
452
+ found = true
453
+ break
454
+ }
455
+ }
456
+ }
445
457
if ! found {
446
458
log .Fatalf ("Couldn't find theme %s\n " , theme )
447
459
}
448
- tcfg , err := getTheme (theme )
460
+ tcfg , err := getTheme (theme , isLocal )
449
461
if err != nil {
450
462
log .Fatalf ("Couldn't load theme. Error: %s\n " , err )
451
463
}
@@ -696,6 +708,8 @@ func parseGeneral(cfg *ini.File) General {
696
708
case "window" :
697
709
la .Command = LeaderWindow
698
710
la .Subaction = subaction
711
+ case "newer" :
712
+ la .Command = LeaderLoadNewer
699
713
default :
700
714
fmt .Printf ("leader-action %s is invalid\n " , parts [0 ])
701
715
os .Exit (1 )
@@ -1183,7 +1197,7 @@ func parseConfig(filepath string) (Config, error) {
1183
1197
func createConfigDir () error {
1184
1198
cd , err := os .UserConfigDir ()
1185
1199
if err != nil {
1186
- log .Fatalf ("couldn't find $HOME . Err %v" , err )
1200
+ log .Fatalf ("couldn't find config dir . Err %v" , err )
1187
1201
}
1188
1202
path := cd + "/tut"
1189
1203
return os .MkdirAll (path , os .ModePerm )
@@ -1192,7 +1206,7 @@ func createConfigDir() error {
1192
1206
func checkConfig (filename string ) (path string , exists bool , err error ) {
1193
1207
cd , err := os .UserConfigDir ()
1194
1208
if err != nil {
1195
- log .Fatalf ("couldn't find $HOME . Err %v" , err )
1209
+ log .Fatalf ("couldn't find config dir . Err %v" , err )
1196
1210
}
1197
1211
dir := cd + "/tut/"
1198
1212
path = dir + filename
@@ -1218,24 +1232,58 @@ func CreateDefaultConfig(filepath string) error {
1218
1232
return nil
1219
1233
}
1220
1234
1221
- func getThemes () ([]string , error ) {
1235
+ func getThemes () (bundled []string , local [] string , err error ) {
1222
1236
entries , err := themesFS .ReadDir ("themes" )
1223
- files := []string {}
1224
1237
if err != nil {
1225
- return [] string {} , err
1238
+ return bundled , local , err
1226
1239
}
1227
1240
for _ , entry := range entries {
1228
1241
if entry .IsDir () {
1229
1242
continue
1230
1243
}
1231
1244
fp := filepath .Join ("themes/" , entry .Name ())
1232
- files = append (files , fp )
1245
+ bundled = append (bundled , fp )
1246
+ }
1247
+ _ , exists , err := checkConfig ("themes" )
1248
+ if err != nil {
1249
+ return bundled , local , err
1250
+ }
1251
+ if ! exists {
1252
+ return bundled , local , err
1253
+ }
1254
+ cd , err := os .UserConfigDir ()
1255
+ if err != nil {
1256
+ log .Fatalf ("couldn't find config dir. Err %v" , err )
1233
1257
}
1234
- return files , nil
1258
+ dir := cd + "/tut/themes"
1259
+ entries , err = os .ReadDir (dir )
1260
+ if err != nil {
1261
+ return bundled , local , err
1262
+ }
1263
+ for _ , entry := range entries {
1264
+ if entry .IsDir () {
1265
+ continue
1266
+ }
1267
+ fp := filepath .Join (dir , entry .Name ())
1268
+ local = append (local , fp )
1269
+ }
1270
+ return bundled , local , nil
1235
1271
}
1236
1272
1237
- func getTheme (fname string ) (* ini.File , error ) {
1238
- f , err := themesFS .Open (fmt .Sprintf ("themes/%s.ini" , strings .TrimSpace (fname )))
1273
+ func getTheme (fname string , isLocal bool ) (* ini.File , error ) {
1274
+ var f io.Reader
1275
+ var err error
1276
+ if isLocal {
1277
+ var cd string
1278
+ cd , err = os .UserConfigDir ()
1279
+ if err != nil {
1280
+ log .Fatalf ("couldn't find config dir. Err %v" , err )
1281
+ }
1282
+ dir := cd + "/tut/themes"
1283
+ f , err = os .Open (fmt .Sprintf ("%s/%s.ini" , dir , strings .TrimSpace (fname )))
1284
+ } else {
1285
+ f , err = themesFS .Open (fmt .Sprintf ("themes/%s.ini" , strings .TrimSpace (fname )))
1286
+ }
1239
1287
if err != nil {
1240
1288
return nil , err
1241
1289
}
0 commit comments