Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Review by tenntenn #6

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions buffer.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,19 @@ func NewLocation(l, c int) *Location {
return &Location{x: l, y: c}
}

// by tenntenn *os.Fileではなく、io.ReadSeekerの方がいいかも
func (buf *Buffer) Read(f *os.File) error {
// by tenntenn バッファリングはするべきだけど、全部のデータをメモリ上に展開するとでかいファイルをひらけなくなるのでは?エディタを作ったことがないのでなんとも言えないけど
b := make([]byte, BUFSIZE)
// by tenntenn 全部読み込むならioutil.ReadAllを使ったほうがいい。
n, err := f.Read(b)
if err != nil {
return err
}
b = b[:n]

var s []rune
// by tenntenn bufio.Scannerを使ったほうが良さそう
for _, v := range b {
if v == byte(10) { // 改行のバイト
s = append(s, rune(v))
Expand All @@ -53,6 +57,7 @@ func (buf *Buffer) Read(f *os.File) error {
}

func (buf *Buffer) open() error {
// by tenntenn os.Openなので、新規作成ができない
f, err := os.Open(buf.path)
if err != nil {
return err
Expand Down
3 changes: 3 additions & 0 deletions cursor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ package main

import "github.com/gdamore/tcell"

// by tenntenn もうちょい抽象化したほうが良さそう。tcellに依存し過ぎな気がする。
// キーコードを使うのではなく、それを対応付けたものを定義したほうがいいかな
// 例えば、ユーザが好きにキーバインドを変える機能を追加したときに変更点を減らすにはどうすればよいか?
func (buf *Buffer) CursorMove(key MoveC) {
switch key {
case tcell.KeyBackspace, tcell.KeyBackspace2, tcell.KeyLeft, 'h':
Expand Down
3 changes: 3 additions & 0 deletions event.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@ package main

import "github.com/gdamore/tcell"

// by tenntenn これはなんのための型?
type MoveC interface{}

// by tenntenn よくあるイベント駆動モデルを作ったほうが良さそう。
// 一箇所にイベントの処理を書くと大変そう。
func (v *View) EventHandle(ev *tcell.EventKey) error {
switch v.mode {
case Normal:
Expand Down
8 changes: 6 additions & 2 deletions init.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"github.com/gdamore/tcell/encoding"
)

// by tenntenn 初期化処理だとしても、極力パッケージ変数の初期化はしない
func initScreen() error {
var err error
screen, err = tcell.NewScreen()
Expand All @@ -22,12 +23,15 @@ func initScreen() error {
screen.Clear()

screenWidth, screenHeight = screen.Size()
// by tenntenn この-2はなんだろう?
screen.Resize(0, 0, screenWidth, screenHeight-2)

return nil
}

func initEvent() {
// by tenntenn このゴールーチンを終わらせるための仕組みがない
// See: https://qiita.com/tenntenn/items/dd6041d630af7feeec52
go func() {
for {
if screen == nil {
Expand All @@ -39,11 +43,11 @@ func initEvent() {
}

func initBuffer(path string) (*Buffer, error) {
buf := new(Buffer)
buf := new(Buffer) // by tenntenn var buf Bufferでも使える
if err := buf.fileManage(path); err != nil {
return nil, err
}
buf.Cursor = NewLocation(0, 0)

return buf, nil
return buf, nil // by tenntenn var buf Bufferとした場合は&buf
}
7 changes: 5 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ import (
"github.com/gdamore/tcell"
)

// by tenntenn 構造体にすることでパッケージ変数を減らせないか考えてみてくdさい
var (
Events = make(chan tcell.Event)
Events = make(chan tcell.Event)
// by tenntenn 変数は明示的に初期化しなくてもゼロ値で初期化されている
screen tcell.Screen = nil
screenWidth = 0
screenHeight = 0
Expand All @@ -28,7 +30,7 @@ func run(args []string) int {
fmt.Fprintln(os.Stderr, err)
return 1
}
defer screen.Fini()
defer screen.Fini() // by tenntenn 略さない方がいい

initEvent()

Expand All @@ -44,6 +46,7 @@ func run(args []string) int {
return 1
}

// by tenntenn 別の関数にしてreturnで抜けたほうがわかりやすいかも
loop:
for {
select {
Expand Down
1 change: 1 addition & 0 deletions mode.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ const (
Visual
)

// by tenntenn 構造体のフィールドにしてテストしやすくしよう
var (
mode Mode
)
Expand Down