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
|
||||
}
|
||||
|
||||
func (self *TextTemplate) initVarMap() {
|
||||
self.varMap["dt"] = "Current timestamp as YYYY-MM-DD_HH-MM-SS"
|
||||
self.varMap["ymd"] = "Current date as YYYY-MM-DD"
|
||||
self.varMap["hm"] = "Current time as HH-MM"
|
||||
self.varMap["h"] = "Current time as HH"
|
||||
self.varMap["hms"] = "Current time as HH-MM-SS"
|
||||
self.varMap["month"] = "Month name"
|
||||
self.varMap["month-num"] = "Month as number (mm)"
|
||||
self.varMap["week-day"] = "Week day name"
|
||||
self.varMap["week-day-num"] = "Week day as number"
|
||||
self.varMap["yesterday"] = "Yesterday date"
|
||||
self.varMap["uid"] = self.Uid
|
||||
self.varMap["username"] = self.UserName
|
||||
self.varMap["home"] = self.HomeDir
|
||||
self.varMap["progdir"] = self.ProgramDir
|
||||
self.varMap["hostname"], _ = os.Hostname()
|
||||
func (tt *TextTemplate) initVarMap() {
|
||||
tt.varMap["dt"] = "Current timestamp as YYYY-MM-DD_HH-MM-SS"
|
||||
tt.varMap["ymd"] = "Current date as YYYY-MM-DD"
|
||||
tt.varMap["hm"] = "Current time as HH-MM"
|
||||
tt.varMap["h"] = "Current time as HH"
|
||||
tt.varMap["hms"] = "Current time as HH-MM-SS"
|
||||
tt.varMap["month"] = "Month name"
|
||||
tt.varMap["month-num"] = "Month as number (mm)"
|
||||
tt.varMap["week-day"] = "Week day name"
|
||||
tt.varMap["week-day-num"] = "Week day as number"
|
||||
tt.varMap["yesterday"] = "Yesterday date"
|
||||
tt.varMap["uid"] = tt.Uid
|
||||
tt.varMap["username"] = tt.UserName
|
||||
tt.varMap["home"] = tt.HomeDir
|
||||
tt.varMap["progdir"] = tt.ProgramDir
|
||||
tt.varMap["hostname"], _ = os.Hostname()
|
||||
}
|
||||
|
||||
func (self *TextTemplate) AddVar(name, value string) {
|
||||
self.varMap[name] = value
|
||||
func (tt *TextTemplate) AddVar(name, value string) {
|
||||
tt.varMap[name] = value
|
||||
}
|
||||
|
||||
// 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()
|
||||
|
||||
self.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())
|
||||
self.varMap["hm"] = fmt.Sprintf("%02d-%02d", now.Hour(), now.Minute())
|
||||
self.varMap["h"] = fmt.Sprintf("%02d", now.Hour())
|
||||
self.varMap["month"] = now.Format("January")
|
||||
self.varMap["month-num"] = fmt.Sprintf("%02d", now.Month())
|
||||
self.varMap["week-day-num"] = fmt.Sprintf("%d", 1+now.Weekday())
|
||||
self.varMap["week-day"] = now.Format("Monday")
|
||||
self.varMap["dt"] = self.varMap["ymd"] + "_" + self.varMap["hms"]
|
||||
tt.varMap["ymd"] = fmt.Sprintf("%04d-%02d-%02d", now.Year(), now.Month(), now.Day())
|
||||
tt.varMap["hms"] = fmt.Sprintf("%02d-%02d-%02d", now.Hour(), now.Minute(), now.Second())
|
||||
tt.varMap["hm"] = fmt.Sprintf("%02d-%02d", now.Hour(), now.Minute())
|
||||
tt.varMap["h"] = fmt.Sprintf("%02d", now.Hour())
|
||||
tt.varMap["month"] = now.Format("January")
|
||||
tt.varMap["month-num"] = fmt.Sprintf("%02d", now.Month())
|
||||
tt.varMap["week-day-num"] = fmt.Sprintf("%d", 1+now.Weekday())
|
||||
tt.varMap["week-day"] = now.Format("Monday")
|
||||
tt.varMap["dt"] = tt.varMap["ymd"] + "_" + tt.varMap["hms"]
|
||||
|
||||
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"] = self.Uid
|
||||
// self.varMap["username"] = self.UserName
|
||||
@ -114,12 +114,12 @@ func (self *TextTemplate) updateVarMap() {
|
||||
// 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 {
|
||||
value, ok := self.varMap[name]
|
||||
value, ok := tt.varMap[name]
|
||||
if ok {
|
||||
return value
|
||||
} else {
|
||||
@ -130,11 +130,11 @@ func (self *TextTemplate) Expand(template string) string {
|
||||
return result
|
||||
}
|
||||
|
||||
func (self *TextTemplate) ExpandEnv(template string) (result string) {
|
||||
func (tt *TextTemplate) ExpandEnv(template string) (result string) {
|
||||
if len(template) > 0 {
|
||||
self.updateVarMap()
|
||||
tt.updateVarMap()
|
||||
result = os.Expand(template, func(name string) string {
|
||||
value, ok := self.varMap[name]
|
||||
value, ok := tt.varMap[name]
|
||||
if ok {
|
||||
return value
|
||||
} else {
|
||||
@ -159,22 +159,22 @@ func (self *TextTemplate) ExpandEnv(template string) (result string) {
|
||||
return result
|
||||
}
|
||||
|
||||
func (self *TextTemplate) Value(key string) (value string, ok bool) {
|
||||
value, ok = self.varMap[key]
|
||||
func (tt *TextTemplate) Value(key string) (value string, ok bool) {
|
||||
value, ok = tt.varMap[key]
|
||||
return
|
||||
}
|
||||
|
||||
func (self *TextTemplate) String() string {
|
||||
func (tt *TextTemplate) String() string {
|
||||
var sb strings.Builder
|
||||
|
||||
keys := make([]string, 0, len(self.varMap))
|
||||
for k := range self.varMap {
|
||||
keys := make([]string, 0, len(tt.varMap))
|
||||
for k := range tt.varMap {
|
||||
keys = append(keys, k)
|
||||
}
|
||||
sort.Strings(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 {
|
||||
// sb.WriteString(fmt.Sprintf("%s=%#v\n", k, v))
|
||||
|
||||
Loading…
Reference in New Issue
Block a user