parent
93db48015b
commit
f1548e6bac
@ -1,93 +1,97 @@ |
|||||||
// Copyright (c) 2013 by Michael Dvorkin. All Rights Reserved.
|
// Copyright (c) 2013 by Michael Dvorkin. All Rights Reserved.
|
||||||
// Use of this source code is governed by a MIT-style
|
// Use of this source code is governed by a MIT-style license that can
|
||||||
// license that can be found in the LICENSE file.
|
// be found in the LICENSE file.
|
||||||
|
|
||||||
package mop |
package mop |
||||||
|
|
||||||
import ( |
import `github.com/michaeldv/termbox-go` |
||||||
`github.com/michaeldv/termbox-go` |
|
||||||
) |
|
||||||
|
|
||||||
|
// ColumnEditor handles column sort order. When activated it highlights
|
||||||
|
// current column name in the header, then waits for arrow keys (choose
|
||||||
|
// another column), Enter (reverse sort order), or Esc (exit).
|
||||||
type ColumnEditor struct { |
type ColumnEditor struct { |
||||||
screen *Screen |
screen *Screen // Pointer to Screen so we could use screen.Draw().
|
||||||
quotes *Quotes |
quotes *Quotes // Pointer to Quotes to redraw them when the sort order changes.
|
||||||
profile *Profile |
profile *Profile // Pointer to Profile where we save newly selected sort order.
|
||||||
} |
} |
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
// Initialize sets internal variables and highlights current column name
|
||||||
func (self *ColumnEditor) Initialize(screen *Screen, quotes *Quotes) *ColumnEditor { |
// (as stored in Profile).
|
||||||
self.screen = screen |
func (editor *ColumnEditor) Initialize(screen *Screen, quotes *Quotes) *ColumnEditor { |
||||||
self.quotes = quotes |
editor.screen = screen |
||||||
self.profile = quotes.profile |
editor.quotes = quotes |
||||||
|
editor.profile = quotes.profile |
||||||
|
|
||||||
|
editor.selectCurrentColumn() |
||||||
|
|
||||||
self.select_current_column() |
return editor |
||||||
return self |
|
||||||
} |
} |
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
// Handle takes over the keyboard events and dispatches them to appropriate
|
||||||
func (self *ColumnEditor) Handle(ev termbox.Event) bool { |
// column editor handlers. It returns true when user presses Esc.
|
||||||
defer self.redraw_header() |
func (editor *ColumnEditor) Handle(event termbox.Event) bool { |
||||||
|
defer editor.redrawHeader() |
||||||
|
|
||||||
switch ev.Key { |
switch event.Key { |
||||||
case termbox.KeyEsc: |
case termbox.KeyEsc: |
||||||
return self.done() |
return editor.done() |
||||||
|
|
||||||
case termbox.KeyEnter: |
case termbox.KeyEnter: |
||||||
self.execute() |
editor.execute() |
||||||
|
|
||||||
case termbox.KeyArrowLeft: |
case termbox.KeyArrowLeft: |
||||||
self.select_left_column() |
editor.selectLeftColumn() |
||||||
|
|
||||||
case termbox.KeyArrowRight: |
case termbox.KeyArrowRight: |
||||||
self.select_right_column() |
editor.selectRightColumn() |
||||||
} |
} |
||||||
|
|
||||||
return false |
return false |
||||||
} |
} |
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
func (self *ColumnEditor) select_current_column() *ColumnEditor { |
func (editor *ColumnEditor) selectCurrentColumn() *ColumnEditor { |
||||||
self.profile.selected_column = self.profile.SortColumn |
editor.profile.selected_column = editor.profile.SortColumn |
||||||
self.redraw_header() |
editor.redrawHeader() |
||||||
return self |
return editor |
||||||
} |
} |
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
func (self *ColumnEditor) select_left_column() *ColumnEditor { |
func (editor *ColumnEditor) selectLeftColumn() *ColumnEditor { |
||||||
self.profile.selected_column-- |
editor.profile.selected_column-- |
||||||
if self.profile.selected_column < 0 { |
if editor.profile.selected_column < 0 { |
||||||
self.profile.selected_column = TotalColumns - 1 |
editor.profile.selected_column = TotalColumns - 1 |
||||||
} |
} |
||||||
return self |
return editor |
||||||
} |
} |
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
func (self *ColumnEditor) select_right_column() *ColumnEditor { |
func (editor *ColumnEditor) selectRightColumn() *ColumnEditor { |
||||||
self.profile.selected_column++ |
editor.profile.selected_column++ |
||||||
if self.profile.selected_column > TotalColumns - 1 { |
if editor.profile.selected_column > TotalColumns - 1 { |
||||||
self.profile.selected_column = 0 |
editor.profile.selected_column = 0 |
||||||
} |
} |
||||||
return self |
return editor |
||||||
} |
} |
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
func (self *ColumnEditor) execute() *ColumnEditor { |
func (editor *ColumnEditor) execute() *ColumnEditor { |
||||||
if self.profile.Reorder() == nil { |
if editor.profile.Reorder() == nil { |
||||||
self.screen.Draw(self.quotes) |
editor.screen.Draw(editor.quotes) |
||||||
} |
} |
||||||
|
|
||||||
return self |
return editor |
||||||
} |
} |
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
func (self *ColumnEditor) done() bool { |
func (editor *ColumnEditor) done() bool { |
||||||
self.profile.selected_column = -1 |
editor.profile.selected_column = -1 |
||||||
return true |
return true |
||||||
} |
} |
||||||
|
|
||||||
//-----------------------------------------------------------------------------
|
//-----------------------------------------------------------------------------
|
||||||
func (self *ColumnEditor) redraw_header() { |
func (editor *ColumnEditor) redrawHeader() { |
||||||
self.screen.DrawLine(0, 4, self.screen.layout.Header(self.profile)) |
editor.screen.DrawLine(0, 4, editor.screen.layout.Header(editor.profile)) |
||||||
termbox.Flush() |
termbox.Flush() |
||||||
} |
} |
||||||
|
|
||||||
|
Loading…
Reference in new issue