Skip to content

Commit

Permalink
Merge branch 'release/0.1.4'
Browse files Browse the repository at this point in the history
  • Loading branch information
carlocastoldi committed Apr 21, 2018
2 parents a491118 + 0537d6c commit 164c8d5
Show file tree
Hide file tree
Showing 11 changed files with 75 additions and 63 deletions.
Binary file modified .cache-main
Binary file not shown.
11 changes: 9 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,18 @@ An application with terminal GUI inspired by htop that makes Arch pacman's logs
## Installation
### Dependencies
Mandatory:
- Java (jre)
- Java (jre)

Development dependencies:
- Scala (http://www.scala-lang.org/)
- lanterna-3.0.0 (https://github.com/mabe02/lanterna)
- scala-parser-combinators (https://github.com/scala/scala-parser-combinators)

### Package Manager
#### Arch Linux
Available via AUR here: https://aur.archlinux.org/packages/pacmanlogger-git/
Available via AUR:
- https://aur.archlinux.org/packages/pacmanlogger/
- https://aur.archlinux.org/packages/pacmanlogger-git/

### Manual (sbt)
Compiling:
Expand Down
2 changes: 1 addition & 1 deletion build.sbt
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
name := "PacmanLogger"

version := "0.1.3"
version := "0.1.4"

scalaVersion := "2.12.4"

Expand Down
9 changes: 6 additions & 3 deletions src/main/scala/pacmanlogger/AbstractTable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ import com.googlecode.lanterna.graphics._
import com.googlecode.lanterna.terminal._

trait AbstractTable {
val screen: Screen
val tg: TextGraphics
var terminalSize: TerminalSize
var colWidths: Array[Int]
def getRows: List[List[String]]
def getAllRows: List[List[String]]
def updateValues: Unit
def isLastRow: Boolean
def scrollRows(n: Int): Unit
def scrollStart: Unit
def scrollEnd: Unit
def getScreen: Screen
def getFirstRow: Int
def getTextGraphics: TextGraphics
def draw(terminalSize: TerminalSize, offset: Integer)
def draw(offset: Int)
def drawHeader(offset: Int)
def drawRow(titles: List[String], column: Int, row: Int)
}
34 changes: 13 additions & 21 deletions src/main/scala/pacmanlogger/Cursor.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ import com.googlecode.lanterna._
import com.googlecode.lanterna.graphics._

trait Cursor extends AbstractTable {
var cursorAbsolutePos = 0
var cursorRelativePos = 0
val screen = getScreen
val tg = getTextGraphics
var rows_ = getRows
var nRows_ = rows_.length

abstract override def draw(terminalSize: TerminalSize, offset: Integer) {
super.draw(terminalSize, offset)
abstract override def draw(offset: Int) {
super.draw(offset)
if (terminalSize.getRows - 3 < cursorRelativePos)
cursorRelativePos = terminalSize.getRows - 3
drawCursor(offset)
}

def getCursorAbsolutePos = cursorRelativePos + getFirstRow

def moveCursor(n: Int, tg: TextGraphics, offset: Int, terminalSize: TerminalSize) {
def moveCursor(n: Int, offset: Int) {
rows_ = getRows
nRows_ = rows_.length
(cursorRelativePos + n) match {
Expand All @@ -29,44 +28,38 @@ trait Cursor extends AbstractTable {
case i if !isLastRow =>
var firstRow_ = getFirstRow
scrollRows(n)

delCursor(offset)
cursorRelativePos += n - (getFirstRow - firstRow_)

if (cursorRelativePos < 0) {
if (cursorRelativePos < 0)
cursorRelativePos = 0
}

if (cursorRelativePos > nRows_ - 1) {
else if (cursorRelativePos > nRows_ - 1)
cursorRelativePos = nRows_ - 1
}


drawCursor(offset)
draw(terminalSize, offset)
draw(offset)
case _ => ()
}
}

def moveCursorStart(tg: TextGraphics, offset: Int, terminalSize: TerminalSize) {
def moveCursorStart(offset: Int) {
delCursor(offset)
cursorRelativePos = 0
drawCursor(offset)
scrollStart
draw(terminalSize, offset)
draw(offset)
}

def moveCursorEnd(tg: TextGraphics, offset: Int, terminalSize: TerminalSize) {
def moveCursorEnd(offset: Int) {
rows_ = getRows
nRows_ = rows_.length
delCursor(offset)
cursorRelativePos = nRows_ - 1
drawCursor(offset)
scrollEnd
draw(terminalSize, offset)
draw(offset)
}

def delCursor(offset: Int) {
updateValues
rows_ = getRows
nRows_ = rows_.length
tg.setForegroundColor(TextColor.ANSI.CYAN)
Expand All @@ -80,7 +73,6 @@ trait Cursor extends AbstractTable {
}

def drawCursor(offset: Int) {
updateValues
rows_ = getRows
nRows_ = rows_.length
tg.setForegroundColor(TextColor.ANSI.BLACK)
Expand Down
3 changes: 1 addition & 2 deletions src/main/scala/pacmanlogger/Filterable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,10 @@ import com.googlecode.lanterna.graphics._

trait Filterable extends Table {
val totalTuples = tuples

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

override def updateValues = {
override def updateValues = { // Filterable must be the first to be called on a updateValues call right after Table
super.updateValues
tuples = totalTuples.filter(filter)
}
Expand Down
22 changes: 11 additions & 11 deletions src/main/scala/pacmanlogger/Logger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class Logger(var logs: List[List[String]]) {
val mainTable = new Table(titles, logs, true, screen, textGraphics) with Filterable with Sortable with Cursor
val filters = logs.map((l: List[String]) => l(1)).distinct.sortWith(_<_)
val filterTable = new FilterTable("Filter By", filters, 1, mainTable, false, screen, textGraphics) with OptionCursor
val sortByTable = new SortByTable("Sort By", 1, mainTable, false, screen, textGraphics) with OptionCursor
val sortByTable = new SortByTable("Sort By", 0, mainTable, false, screen, textGraphics) with OptionCursor

var focussedTable: Cursor = mainTable
var mainTableOffset = 0
Expand All @@ -45,17 +45,17 @@ class Logger(var logs: List[List[String]]) {
Thread.sleep(100)
}
}
mainTable.draw(terminalSize, mainTableOffset)
mainTable.draw(mainTableOffset)
var keyStroke = screen.pollInput()
while (keyStroke == null || (KeyType.F3 != keyStroke.getKeyType && 'q' != keyStroke.getCharacter) || focussedTable != mainTable) {
if (keyStroke != null) {
keyStroke.getKeyType match {
case KeyType.ArrowDown => focussedTable.moveCursor(1, textGraphics, focussedTableOffset, terminalSize)
case KeyType.ArrowUp => focussedTable.moveCursor(-1, textGraphics, focussedTableOffset, terminalSize)
case KeyType.PageDown => focussedTable.moveCursor(terminalSize.getRows - 2, textGraphics, focussedTableOffset, terminalSize)
case KeyType.Home => focussedTable.moveCursorStart(textGraphics, focussedTableOffset, terminalSize)
case KeyType.End => focussedTable.moveCursorEnd(textGraphics, focussedTableOffset, terminalSize)
case KeyType.PageUp => focussedTable.moveCursor(-(terminalSize.getRows - 2), textGraphics, focussedTableOffset, terminalSize)
case KeyType.ArrowDown => focussedTable.moveCursor(1, focussedTableOffset)
case KeyType.ArrowUp => focussedTable.moveCursor(-1, focussedTableOffset)
case KeyType.PageDown => focussedTable.moveCursor(terminalSize.getRows - 2, focussedTableOffset)
case KeyType.Home => focussedTable.moveCursorStart(focussedTableOffset)
case KeyType.End => focussedTable.moveCursorEnd(focussedTableOffset)
case KeyType.PageUp => focussedTable.moveCursor(-(terminalSize.getRows - 2), focussedTableOffset)
case KeyType.F4 => state.f4
case KeyType.F5 => state.f5
case KeyType.Escape => state.esc
Expand All @@ -72,17 +72,17 @@ class Logger(var logs: List[List[String]]) {
}

def draw(state: LoggerState) {
mainTable.draw(terminalSize, mainTableOffset)
mainTable.draw(mainTableOffset)
if (mainTableOffset > 0)
focussedTable.draw(terminalSize, 0)
focussedTable.draw(0)
drawFoot(state.getFoot, 0)
}

def drawFoot(commands: List[(String, String)], off: Int) {
var offset = off
val columns = terminalSize.getColumns()
val row = terminalSize.getRows - 1
var total = "Total "+mainTable.getAllRows.length
var total = (mainTable.getCursorAbsolutePos + 1) + "/" + mainTable.getAllRows.length
commands foreach {
case (k, c) =>
textGraphics.setForegroundColor(TextColor.ANSI.CYAN)
Expand Down
2 changes: 2 additions & 0 deletions src/main/scala/pacmanlogger/OptionTable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class FilterTable(title: String, tuples: List[String], index: Int, options: List
}
filterableTable.setFilterFunction(filterFunction)
filterableTable.updateValues
updateValues
}

def filterFunction = {
Expand All @@ -71,6 +72,7 @@ class SortByTable(title: String, var index: Int, sortableTable: Sortable, fullSc
case _ => false
}), sortableTable, fullScreen, screen, tg) {
val sortingTitles = sortableTable.getTitles
sortableTable.sortByIndex(index)

override def switchOption(r: List[String]) = {
sortingTitles.zipWithIndex foreach {
Expand Down
4 changes: 2 additions & 2 deletions src/main/scala/pacmanlogger/PacmanLogger.scala
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ object PacmanLogger {
def main(args: Array[String]) = {
val src = scala.io.Source.fromFile("/var/log/pacman.log")
val lines = src.mkString
src close
val p = new PacmanLoggerParser
val res = p.parseAll(p.logs, lines)
res match {
case p.Success(parsedLogs, _) =>
val logger = new Logger(parsedLogs)
logger.start
case x => println("ERROR: "+x.toString)
case x => System.err.println("ERROR: "+x.toString)
}
src close
}
}
14 changes: 12 additions & 2 deletions src/main/scala/pacmanlogger/Sortable.scala
Original file line number Diff line number Diff line change
@@ -1,20 +1,30 @@
package pacmanlogger

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

trait Sortable extends Table {
var sortingIndex = 0
tuples = tuples.sortWith{_(sortingIndex)<_(sortingIndex)}
updateValues

override def updateValues = {
super.updateValues
tuples = tuples.sortWith{_(sortingIndex)<_(sortingIndex)}
}

override def drawHeader(offset: Int) = {
super.drawHeader(offset)
val totalOffset = offset + colWidths.take(sortingIndex).foldLeft(0)((x,y) => x+y)
val columnTitle = titles(sortingIndex)+" "
tg.setForegroundColor(TextColor.ANSI.BLACK)
tg.setBackgroundColor(TextColor.ANSI.CYAN)
tg.putString(totalOffset, 0, columnTitle)
}

def sortByIndex(i: Int) = {
sortingIndex = i
tuples = tuples.sortWith{_(sortingIndex)<_(sortingIndex)}
updateValues
}

def getTitles = titles
Expand Down
37 changes: 18 additions & 19 deletions src/main/scala/pacmanlogger/Table.scala
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import com.googlecode.lanterna.screen._
import com.googlecode.lanterna.graphics._
import com.googlecode.lanterna.terminal._

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

var colWidths = new Array[Int](titles.length)
Expand Down Expand Up @@ -39,24 +39,25 @@ class Table(val titles: List[String], var tuples: List[List[String]], fullScreen
tuplesLength = tuples.length
}

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

def isLastRow = firstRow + nRows == tuplesLength + 1

override def draw(terminalSize: TerminalSize, offset: Integer) {
override def draw(offset: Int) {
calcColWidths
tg.setForegroundColor(TextColor.ANSI.BLACK)
tg.setBackgroundColor(TextColor.ANSI.GREEN)
drawRow(titles, offset, 0)
drawHeader(offset)
tg.setForegroundColor(TextColor.ANSI.CYAN)
tg.setBackgroundColor(TextColor.ANSI.DEFAULT)
val localRows = rows
localRows.zipWithIndex foreach {
rows.zipWithIndex foreach {
case (r, i) => drawRow(r, offset, i + 1)
}
}

def drawHeader(offset: Int) {
tg.setForegroundColor(TextColor.ANSI.BLACK)
tg.setBackgroundColor(TextColor.ANSI.GREEN)
drawRow(titles, offset, 0)
}

override def drawRow(titles: List[String], column: Int, row: Int) {
val columns = terminalSize.getColumns
Expand All @@ -75,25 +76,23 @@ class Table(val titles: List[String], var tuples: List[List[String]], fullScreen
}

override def scrollRows(n: Int) {
val totalLength = tuplesLength
val rowsLength = nRows
if (firstRow + n >= 0 && firstRow + n + rowsLength <= totalLength)
if (firstRow + n >= 0 && firstRow + n + nRows <= tuplesLength)
firstRow += n
else if (firstRow + n >= 0 && firstRow + rowsLength <= totalLength)
firstRow = totalLength - rowsLength
else if (firstRow > 0 && firstRow + n + rowsLength <= totalLength)
else if (firstRow + n >= 0 && firstRow + nRows <= tuplesLength)
firstRow = tuplesLength - nRows
else if (firstRow > 0 && firstRow + n + nRows <= tuplesLength)
firstRow = 0
rows = updateRows
}

override def scrollStart {
firstRow = 0
rows = updateRows
}

override def scrollEnd {
val totalLength = tuplesLength
val rowsLength = nRows

firstRow = totalLength - rowsLength
firstRow = tuplesLength - nRows
rows = updateRows
}

def calcColWidths {
Expand Down

0 comments on commit 164c8d5

Please sign in to comment.