Autohotkey对齐文本(单词)列

时间:2017-10-30 13:29:57

标签: text autohotkey

如何在autohotkey中的单词上对齐文本?

在示例中,单词quick之前的文本和单词fox应填充空格,以便alle字垂直对齐。

oldtext = 
(
    a slow dog
    the quick red fox
    my quick brown and friendly fox
    a quick yellow fox
    a slow cat
    the fox
)
newtext = TextAlign(oldtext, "quick")
newtext = TextAlign(newtext, "fox")
(
    a slow dog
    the quick red                fox
    my  quick brown and friendly fox
    a   quick yellow             fox
    a slow cat
    the                          fox
)

2 个答案:

答案 0 :(得分:0)

oldtext = 
(
a slow dog
the quick red fox
my quick brown and friendly fox
a quick yellow fox
a slow cat
the fox
)


newtext := TextAlign(oldtext, "quick")
newtext := TextAlign(newtext, "fox")

clipboard:=newtext

TextAlign(text, x) {
    lines := StrSplit(text, "`n", "`r")

    For e, v in lines 
        CUR := v ~= "i)" x, MAX := (MAX < CUR ? CUR : Max)

    for e, v in lines {
        y := v ~= "i)" x, z := StrSplit(v, x)
        newText .= ((y &&  y < max) ? (z[1] padSpaces(max - y) x z[2]) : v) "`n"
    }

    return newText    
}

padSpaces(x) {
    loop % x
        r .= " "
    return r
}

结果:

a slow dog
the quick red                fox
my  quick brown and friendly fox
a   quick yellow             fox
a slow cat
the                          fox

答案 1 :(得分:0)

谢谢,我的版本有点长,但也有效。

StrPad(str, padchar, padlen, left=1)
{
    if (i := padlen-StrLen(str))
        VarSetCapacity(w, i, asc(padchar)),  NumPut(0, &w+i, "Char"),          VarSetCapacity(w, -1)
    return left ? w str : str w
}

TextAlign(data, xFind)
{
 xpos:=0
 data2:=""
 Loop, Parse, data, `n, `r
{ 
  line:=A_LoopField
  pos := InStr(line, xFind, false)
  if (pos > xpos) 
  {
   xpos:=pos
  }
 }
 Loop, Parse, data, `n, `r
 { 
  line:=A_LoopField
  pos := InStr(line, xFind, false)
  if ( (pos > 0) AND (pos < xpos) ) 
 {
  xSpace:=StrPad(""," ",xpos-pos)
  xRepl:=xSpace xFind
  StringReplace, line, line, %xFind%, %xRepl% 
  data2:=data2 line "`n"
  } 
  else
  {
   data2:=data2 line "`n"
  }
}
return data2
}