github.com/jmigpin/editor@v1.6.0/core/homevars.go (about)

     1  package core
     2  
     3  import (
     4  	"github.com/jmigpin/editor/core/toolbarparser"
     5  	"github.com/jmigpin/editor/util/osutil"
     6  )
     7  
     8  type HomeVars struct {
     9  	hvm *toolbarparser.HomeVarMap
    10  }
    11  
    12  func NewHomeVars() *HomeVars {
    13  	return &HomeVars{}
    14  }
    15  
    16  func (hv *HomeVars) ParseToolbarVars(strs []string, caseInsensitive bool) {
    17  	// merge strings maps
    18  	m := toolbarparser.VarMap{}
    19  	for _, str := range strs {
    20  		data := toolbarparser.Parse(str)
    21  		m2 := toolbarparser.ParseVars(data)
    22  		// merge
    23  		for k, v := range m2 {
    24  			m[k] = v
    25  		}
    26  	}
    27  	// add env home var at the end to enforce value
    28  	h := osutil.HomeEnvVar()
    29  	if h != "" {
    30  		m["~"] = h
    31  	}
    32  
    33  	hv.hvm = toolbarparser.NewHomeVarMap(m, caseInsensitive)
    34  }
    35  
    36  //----------
    37  
    38  func (hv *HomeVars) Encode(filename string) string {
    39  	return hv.hvm.Encode(filename)
    40  }
    41  
    42  func (hv *HomeVars) Decode(filename string) string {
    43  	return hv.hvm.Decode(filename)
    44  }