text-util.go: sostituita la variabile self con nome specifico tt per il tipo TextTemplate
This commit is contained in:
parent
90510128c4
commit
632f68a572
82
text-util.go
82
text-util.go
@ -59,26 +59,26 @@ func NewTextTemplate(args ...string) (instance *TextTemplate) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *TextTemplate) initVarMap() {
|
func (tt *TextTemplate) initVarMap() {
|
||||||
self.varMap["dt"] = "Current timestamp as YYYY-MM-DD_HH-MM-SS"
|
tt.varMap["dt"] = "Current timestamp as YYYY-MM-DD_HH-MM-SS"
|
||||||
self.varMap["ymd"] = "Current date as YYYY-MM-DD"
|
tt.varMap["ymd"] = "Current date as YYYY-MM-DD"
|
||||||
self.varMap["hm"] = "Current time as HH-MM"
|
tt.varMap["hm"] = "Current time as HH-MM"
|
||||||
self.varMap["h"] = "Current time as HH"
|
tt.varMap["h"] = "Current time as HH"
|
||||||
self.varMap["hms"] = "Current time as HH-MM-SS"
|
tt.varMap["hms"] = "Current time as HH-MM-SS"
|
||||||
self.varMap["month"] = "Month name"
|
tt.varMap["month"] = "Month name"
|
||||||
self.varMap["month-num"] = "Month as number (mm)"
|
tt.varMap["month-num"] = "Month as number (mm)"
|
||||||
self.varMap["week-day"] = "Week day name"
|
tt.varMap["week-day"] = "Week day name"
|
||||||
self.varMap["week-day-num"] = "Week day as number"
|
tt.varMap["week-day-num"] = "Week day as number"
|
||||||
self.varMap["yesterday"] = "Yesterday date"
|
tt.varMap["yesterday"] = "Yesterday date"
|
||||||
self.varMap["uid"] = self.Uid
|
tt.varMap["uid"] = tt.Uid
|
||||||
self.varMap["username"] = self.UserName
|
tt.varMap["username"] = tt.UserName
|
||||||
self.varMap["home"] = self.HomeDir
|
tt.varMap["home"] = tt.HomeDir
|
||||||
self.varMap["progdir"] = self.ProgramDir
|
tt.varMap["progdir"] = tt.ProgramDir
|
||||||
self.varMap["hostname"], _ = os.Hostname()
|
tt.varMap["hostname"], _ = os.Hostname()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *TextTemplate) AddVar(name, value string) {
|
func (tt *TextTemplate) AddVar(name, value string) {
|
||||||
self.varMap[name] = value
|
tt.varMap[name] = value
|
||||||
}
|
}
|
||||||
|
|
||||||
// func MakeVarMap() map[string]string {
|
// func MakeVarMap() map[string]string {
|
||||||
@ -92,21 +92,21 @@ func (self *TextTemplate) AddVar(name, value string) {
|
|||||||
// }
|
// }
|
||||||
// }
|
// }
|
||||||
|
|
||||||
func (self *TextTemplate) updateVarMap() {
|
func (tt *TextTemplate) updateVarMap() {
|
||||||
var now = time.Now()
|
var now = time.Now()
|
||||||
|
|
||||||
self.varMap["ymd"] = fmt.Sprintf("%04d-%02d-%02d", now.Year(), now.Month(), now.Day())
|
tt.varMap["ymd"] = fmt.Sprintf("%04d-%02d-%02d", now.Year(), now.Month(), now.Day())
|
||||||
self.varMap["hms"] = fmt.Sprintf("%02d-%02d-%02d", now.Hour(), now.Minute(), now.Second())
|
tt.varMap["hms"] = fmt.Sprintf("%02d-%02d-%02d", now.Hour(), now.Minute(), now.Second())
|
||||||
self.varMap["hm"] = fmt.Sprintf("%02d-%02d", now.Hour(), now.Minute())
|
tt.varMap["hm"] = fmt.Sprintf("%02d-%02d", now.Hour(), now.Minute())
|
||||||
self.varMap["h"] = fmt.Sprintf("%02d", now.Hour())
|
tt.varMap["h"] = fmt.Sprintf("%02d", now.Hour())
|
||||||
self.varMap["month"] = now.Format("January")
|
tt.varMap["month"] = now.Format("January")
|
||||||
self.varMap["month-num"] = fmt.Sprintf("%02d", now.Month())
|
tt.varMap["month-num"] = fmt.Sprintf("%02d", now.Month())
|
||||||
self.varMap["week-day-num"] = fmt.Sprintf("%d", 1+now.Weekday())
|
tt.varMap["week-day-num"] = fmt.Sprintf("%d", 1+now.Weekday())
|
||||||
self.varMap["week-day"] = now.Format("Monday")
|
tt.varMap["week-day"] = now.Format("Monday")
|
||||||
self.varMap["dt"] = self.varMap["ymd"] + "_" + self.varMap["hms"]
|
tt.varMap["dt"] = tt.varMap["ymd"] + "_" + tt.varMap["hms"]
|
||||||
|
|
||||||
yday := now.AddDate(0, 0, -1)
|
yday := now.AddDate(0, 0, -1)
|
||||||
self.varMap["yesterday"] = fmt.Sprintf("%04d-%02d-%02d", yday.Year(), yday.Month(), yday.Day())
|
tt.varMap["yesterday"] = fmt.Sprintf("%04d-%02d-%02d", yday.Year(), yday.Month(), yday.Day())
|
||||||
// self.varMap["uid"] = strconv.Itoa(int(self.Uid))
|
// self.varMap["uid"] = strconv.Itoa(int(self.Uid))
|
||||||
// self.varMap["uid"] = self.Uid
|
// self.varMap["uid"] = self.Uid
|
||||||
// self.varMap["username"] = self.UserName
|
// self.varMap["username"] = self.UserName
|
||||||
@ -114,12 +114,12 @@ func (self *TextTemplate) updateVarMap() {
|
|||||||
// self.varMap["home"] = self.HomeDir
|
// self.varMap["home"] = self.HomeDir
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *TextTemplate) Expand(template string) string {
|
func (tt *TextTemplate) Expand(template string) string {
|
||||||
|
|
||||||
self.updateVarMap()
|
tt.updateVarMap()
|
||||||
|
|
||||||
result := os.Expand(template, func(name string) string {
|
result := os.Expand(template, func(name string) string {
|
||||||
value, ok := self.varMap[name]
|
value, ok := tt.varMap[name]
|
||||||
if ok {
|
if ok {
|
||||||
return value
|
return value
|
||||||
} else {
|
} else {
|
||||||
@ -130,11 +130,11 @@ func (self *TextTemplate) Expand(template string) string {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *TextTemplate) ExpandEnv(template string) (result string) {
|
func (tt *TextTemplate) ExpandEnv(template string) (result string) {
|
||||||
if len(template) > 0 {
|
if len(template) > 0 {
|
||||||
self.updateVarMap()
|
tt.updateVarMap()
|
||||||
result = os.Expand(template, func(name string) string {
|
result = os.Expand(template, func(name string) string {
|
||||||
value, ok := self.varMap[name]
|
value, ok := tt.varMap[name]
|
||||||
if ok {
|
if ok {
|
||||||
return value
|
return value
|
||||||
} else {
|
} else {
|
||||||
@ -159,22 +159,22 @@ func (self *TextTemplate) ExpandEnv(template string) (result string) {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *TextTemplate) Value(key string) (value string, ok bool) {
|
func (tt *TextTemplate) Value(key string) (value string, ok bool) {
|
||||||
value, ok = self.varMap[key]
|
value, ok = tt.varMap[key]
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
func (self *TextTemplate) String() string {
|
func (tt *TextTemplate) String() string {
|
||||||
var sb strings.Builder
|
var sb strings.Builder
|
||||||
|
|
||||||
keys := make([]string, 0, len(self.varMap))
|
keys := make([]string, 0, len(tt.varMap))
|
||||||
for k := range self.varMap {
|
for k := range tt.varMap {
|
||||||
keys = append(keys, k)
|
keys = append(keys, k)
|
||||||
}
|
}
|
||||||
sort.Strings(keys)
|
sort.Strings(keys)
|
||||||
|
|
||||||
for _, k := range keys {
|
for _, k := range keys {
|
||||||
sb.WriteString(fmt.Sprintf("%s=%#v\n", k, self.varMap[k]))
|
sb.WriteString(fmt.Sprintf("%s=%#v\n", k, tt.varMap[k]))
|
||||||
}
|
}
|
||||||
// for k, v := range self.varMap {
|
// for k, v := range self.varMap {
|
||||||
// sb.WriteString(fmt.Sprintf("%s=%#v\n", k, v))
|
// sb.WriteString(fmt.Sprintf("%s=%#v\n", k, v))
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user