Skip to content

Commit

Permalink
Table and Cursor refactoring. Minor bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
carlocastoldi committed Apr 4, 2018
1 parent 4330ca7 commit 1cfa221
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 82 deletions.
Binary file modified .cache-main
Binary file not shown.
1 change: 1 addition & 0 deletions src/main/scala/pacmanlogger/AbstractTable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import com.googlecode.lanterna.terminal._

trait AbstractTable {
def getRows: List[List[String]]
// def setAllRows(newTuples: List[List[String]]): Unit
def getAllRows: List[List[String]]
def updateValues: Unit
def isLastRow: Boolean
Expand Down
13 changes: 11 additions & 2 deletions src/main/scala/pacmanlogger/Cursor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,6 @@ trait Cursor extends AbstractTable {
def moveCursorEnd(tg: TextGraphics, offset: Int, terminalSize: TerminalSize) {
rows_ = getRows
nRows_ = rows_.length

delCursor(offset)
cursorRelativePos = nRows_ - 1
drawCursor(offset)
Expand All @@ -72,6 +71,11 @@ trait Cursor extends AbstractTable {
nRows_ = rows_.length
tg.setForegroundColor(TextColor.ANSI.CYAN)
tg.setBackgroundColor(TextColor.ANSI.DEFAULT)
nRows_ match {
// case 0 => // Crashes!
case n if n <= cursorRelativePos => cursorRelativePos = n-1
case _ => ()
}
drawRow(rows_(cursorRelativePos), offset, cursorRelativePos + 1)
}

Expand All @@ -81,10 +85,15 @@ trait Cursor extends AbstractTable {
nRows_ = rows_.length
tg.setForegroundColor(TextColor.ANSI.BLACK)
tg.setBackgroundColor(TextColor.ANSI.CYAN)
nRows_ match {
// case 0 => // Crashes!
case n if n <= cursorRelativePos => cursorRelativePos = n-1
case _ => ()
}
drawRow(rows_(cursorRelativePos), offset, cursorRelativePos + 1)
}
}

trait OptionCursor extends OptionTable with Cursor {
def getSelectedRow = rows_(cursorRelativePos)
def click = switchOption(rows_(cursorRelativePos))
}
21 changes: 21 additions & 0 deletions src/main/scala/pacmanlogger/FilterableTable.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package pacmanlogger

import com.googlecode.lanterna.screen._
import com.googlecode.lanterna.graphics._

class FilterableTable(titles: List[String], totalTuples: List[List[String]], fullScreen: Boolean, screen: Screen, tg: TextGraphics)
extends Table(titles, totalTuples, fullScreen, screen, tg) {

var filter = (t: List[String]) => true
tuples = totalTuples.filter(filter)

override def updateValues = {
super.updateValues
tuples = totalTuples.filter(filter)
}

def setFilter(f: List[String] => Boolean) = {
filter = f
tuples = totalTuples.filter(filter)
}
}
6 changes: 3 additions & 3 deletions src/main/scala/pacmanlogger/Logger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,10 @@ class Logger(var logs: List[List[String]]) {
var terminalSize = screen.getTerminalSize

val titles = List("N ", "Date", "Action", "Version1", "Version2", "Packet")
val mainTable = new FilteredTable(titles, logs, true, screen, textGraphics) with Cursor

val mainTable = new FilterableTable(titles, logs, true, screen, textGraphics) with Cursor
val filters = logs.map((l: List[String]) => l(2)).distinct
val filterTable: OptionCursor = new OptionTable("Filter By", filters, List(true, true, true, true, true), mainTable, false, screen, textGraphics) with OptionCursor
val filterTable = new FilterTable("Filter By", filters, List(true, true, true, true, true), mainTable, false, screen, textGraphics) with OptionCursor

var focussedTable: Cursor = mainTable
var mainTableOffset = 0
var focussedTableOffset = mainTableOffset
Expand Down
3 changes: 1 addition & 2 deletions src/main/scala/pacmanlogger/LoggerState.scala
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,7 @@ class FilterTableState(logger: Logger, val table: OptionCursor, screen: Screen)
screen.clear()
}
override def enter {
table.switchOption(table.getSelectedRow)
table.updateValues
table.click
screen.clear()
}
}
60 changes: 60 additions & 0 deletions src/main/scala/pacmanlogger/OptionTable.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
package pacmanlogger

import scala.collection.immutable.HashMap
import com.googlecode.lanterna.screen._
import com.googlecode.lanterna.graphics._

abstract class OptionTable(title: String, var optionTuples: List[String], options: List[Boolean], subjectTable: FilterableTable, fullScreen: Boolean, screen: Screen, tg: TextGraphics)
extends Table(List("", title),
optionTuples.zip(options).map(t =>
t match {
case (s, true) => List("[X]", s)
case (s, false) => List("[ ]", s)
}), fullScreen, screen, tg) {

def switchOption(r: List[String]): Unit

var settings: HashMap[String, Boolean] = {
var m = new HashMap[String, Boolean]

optionTuples.zip(options) foreach {
case (action, option) => m = m.updated(action, option)
}
m
}

override def updateValues = {
nRows = terminalSize.getRows - 2
tuples = {
settings.toList map { (t: (String, Boolean)) =>
t match {
case (s, true) => List("[X]", s)
case (s, false) => List("[ ]", s)
}
}
}
rows = updateRows
}
}

class FilterTable(title: String, tuples: List[String], options: List[Boolean], filteredTable: FilterableTable, fullScreen: Boolean, screen: Screen, tg: TextGraphics)
extends OptionTable(title, tuples, options, filteredTable, fullScreen, screen, tg) {

override def switchOption(r: List[String]) = {
settings.toList foreach {
case (s, true) if r(1) == s =>
val values: List[Boolean] = settings.toList.unzip._2
if (values.indexOf(true) != values.lastIndexOf(true))
settings = settings.updated(s, false)
case (s, false) if r(1) == s =>
settings = settings.updated(s, true)
case _ => ()
}
filteredTable.filter = setFilterFunction
}

def setFilterFunction = {
t: List[String] =>
settings.getOrElse(t(2), false)
}
}
93 changes: 18 additions & 75 deletions src/main/scala/pacmanlogger/Table.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,50 +5,43 @@ import com.googlecode.lanterna.screen._
import com.googlecode.lanterna.graphics._
import com.googlecode.lanterna.terminal._

class FilteredTable(titles: List[String], tuples: List[List[String]], fullScreen: Boolean, screen: Screen, tg: TextGraphics)
class Table(titles: List[String], var tuples: List[List[String]], fullScreen: Boolean, screen: Screen, tg: TextGraphics)
extends AbstractTable {

var colWidths = new Array[Int](titles.length)
var terminalSize = screen.getTerminalSize
var firstRow = 0
var filter = (t: List[String]) => true
var filteredRows: List[List[String]] = tuples
var nRows: Int = screen.getTerminalSize.getRows - 2
var rows: List[List[String]] = nRows match {
case n if n > filteredRows.length =>
filteredRows
case n if n > (filteredRows.length - firstRow) =>
filteredRows.drop(terminalSize.getRows)
case _ => filteredRows.drop(firstRow).take(nRows)
}
var rows: List[List[String]] = updateRows

def getRows = rows
def getAllRows = filteredRows
def getAllRows = tuples

def updateRows =
rows = nRows match {
case n if n > filteredRows.length =>
filteredRows
case n if n > (filteredRows.length - firstRow) =>
filteredRows.drop(terminalSize.getRows)
case _ => filteredRows.drop(firstRow).take(nRows)
nRows match {
case n if n > tuples.length =>
tuples
case n if n > (tuples.length - firstRow) =>
tuples.drop(terminalSize.getRows)
case _ => tuples.drop(firstRow).take(nRows)
}

def updateSize = {
terminalSize = screen.getTerminalSize
updateRows
nRows = terminalSize.getRows - 2
rows = updateRows
}

def updateValues = {
nRows = screen.getTerminalSize.getRows - 2
filteredRows = tuples.filter(filter)
updateRows
nRows = terminalSize.getRows - 2
rows = updateRows
}

override def getScreen = screen
override def getTextGraphics = tg
override def getFirstRow = firstRow

def isLastRow = firstRow + nRows == filteredRows.length + 1
def isLastRow = firstRow + nRows == tuples.length + 1

override def draw(terminalSize: TerminalSize, offset: Integer) {
calcColWidths
Expand Down Expand Up @@ -80,7 +73,7 @@ class FilteredTable(titles: List[String], tuples: List[List[String]], fullScreen
}

override def scrollRows(n: Int) {
val totalLength = filteredRows.length
val totalLength = tuples.length
val rowsLength = nRows
if (firstRow + n >= 0 && firstRow + n + rowsLength <= totalLength)
firstRow += n
Expand All @@ -95,7 +88,7 @@ class FilteredTable(titles: List[String], tuples: List[List[String]], fullScreen
}

override def scrollEnd {
val totalLength = filteredRows.length
val totalLength = tuples.length
val rowsLength = nRows

firstRow = totalLength - rowsLength
Expand All @@ -112,54 +105,4 @@ class FilteredTable(titles: List[String], tuples: List[List[String]], fullScreen
}
}
}
}

import scala.collection.immutable.HashMap

class OptionTable(titles: String, tuples: List[String], options: List[Boolean], filteredT: FilteredTable, fullScreen: Boolean, screen: Screen, tg: TextGraphics)
extends FilteredTable(List("", titles),
tuples.zip(options).map(t =>
t match {
case (s, true) => List("[X]", s)
case (s, false) => List("[ ]", s)
}), fullScreen, screen, tg) {

var settings: HashMap[String, Boolean] = {
var m = new HashMap[String, Boolean]
tuples.zip(options) foreach {
case (action, option) => m = m.updated(action, option)
}
m
}

override def updateValues = {
nRows = terminalSize.getRows - 2
filteredRows = {
settings.toList map { (t: (String, Boolean)) =>
t match {
case (s, true) => List("[X]", s)
case (s, false) => List("[ ]", s)
}
}
}
updateRows
}

def switchOption(r: List[String]) = {
settings.toList foreach {
case (s, true) if r(1) == s =>
val values: List[Boolean] = settings.toList.unzip._2
if (values.indexOf(true) != values.lastIndexOf(true))
settings = settings.updated(s, false)
case (s, false) if r(1) == s =>
settings = settings.updated(s, true)
case _ => ()
}
filteredT.filter = setFilterFunction
}

def setFilterFunction = {
t: List[String] =>
settings.getOrElse(t(2), false)
}
}
}

0 comments on commit 1cfa221

Please sign in to comment.