Go言語のGUIプログラミング続き



一応晒す。
かなり間違えてると思うが「動けば正義」だろう。

Image_computer/renren.png

Renren.zip

renren.goソース


package main

//WALK関連のライブラリ
import (
"github.com/lxn/walk"
. "github.com/lxn/walk/declarative"
)

//その他標準ライブラリ
import (
"fmt"
"io/ioutil"
"os"
"regexp"
)

//ほぼGui作成
func main() {

mw := &MyMainWindow{}

if _, err := (MainWindow{
AssignTo: &mw.MainWindow,
Title: "RenRen",
MinSize: Size{360, 400},
Font: Font{Family: "Migu 1M", Bold: true, PointSize: 12},
Layout: VBox{},
Children: []Widget{
HSplitter{
MaxSize: Size{120, 26},
Children: []Widget{
Label{Font: Font{PointSize: 12}, Text: "フォルダパス"},
HSpacer{},
},
},
LineEdit{Font: Font{PointSize: 12}, AssignTo: &mw.folderPath},

HSplitter{
MaxSize: Size{120, 26},
Children: []Widget{
Label{Font: Font{PointSize: 12}, Text: "検索ワード(正規表現)"},
LineEdit{Font: Font{PointSize: 12}, AssignTo: &mw.searchWord},
},
},
HSplitter{
MaxSize: Size{120, 26},
Children: []Widget{
Label{Font: Font{PointSize: 12}, Text: "置換ワード(正規表現)"},
LineEdit{Font: Font{PointSize: 12}, AssignTo: &mw.changeWord},
},
},
VSpacer{},
PushButton{Font: Font{PointSize: 12}, Text: "検索(実際にリネームしない)", OnClicked: mw.clicked},
PushButton{Font: Font{PointSize: 12}, Text: "リネーム実行", OnClicked: mw.clicked2},
VSpacer{},
ListBox{Font: Font{PointSize: 12}, AssignTo: &mw.results, Row: 5},
},
}.Run()); err != nil {
os.Exit(1)
}
}

//structにまとめることで、グローバル変数を作らない
type MyMainWindow struct {
*walk.MainWindow
folderPath *walk.LineEdit
searchWord *walk.LineEdit
changeWord *walk.LineEdit
results *walk.ListBox
}

//検索ボタンクリック
func (mw *MyMainWindow) clicked() {
model := []string{}
model = searchFile(mw.folderPath.Text(),mw.searchWord.Text(),mw.changeWord.Text(), false)
mw.results.SetModel(model)
}

//置換ボタンクリック
func (mw *MyMainWindow) clicked2() {
model := []string{}
model = searchFile(mw.folderPath.Text(),mw.searchWord.Text(),mw.changeWord.Text(), true)
mw.results.SetModel(model)
}

//リネーム本体
func searchFile(fpath,sword,cword string, writeflag bool) []string {

var re = regexp.MustCompile(sword)
model := []string{}
fileinfos, _ := ioutil.ReadDir(fpath)

for _, fileinfo := range fileinfos {
if !fileinfo.IsDir() {
fname := fileinfo.Name()
if m := re.MatchString(fname); m {
fname2 := re.ReplaceAllString(fname, cword)
model = append(model, fmt.Sprintln(fname, "を変換します。>", fname2))
fname = fpath + "/" + fname
fname2 = fpath + "/" + fname2
if writeflag {
err := os.Rename(fname, fname2)
//エラー処理
if err != nil {
os.Exit(1)
}
}
}
}
}
return model
}




ここに敬意を表して、
イヤッッホォォォオオォオウ!と叫んでおこう。 

 
 

コメントは日本語でお願いします。(URLは入力禁止:Do not URL writing.) :System message: コメントを受けつけています。