Skip to content

Commit

Permalink
add go.mod and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
robatipoor committed Mar 20, 2019
1 parent 3c12ea6 commit 0dfe1db
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 26 deletions.
9 changes: 9 additions & 0 deletions go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module github.com/GeertJohan/gomatrix.git

go 1.12

require (
github.com/davecgh/go-spew v1.1.1
github.com/gdamore/tcell v1.1.1
github.com/jessevdk/go-flags v1.4.0
)
15 changes: 15 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/gdamore/encoding v1.0.0 h1:+7OoQ1Bc6eTm5niUzBa0Ctsh6JbMW6Ra+YNuAtDBdko=
github.com/gdamore/encoding v1.0.0/go.mod h1:alR0ol34c49FCSBLjhosxzcPHQbf2trDkoo5dl+VrEg=
github.com/gdamore/tcell v1.1.1 h1:U73YL+jMem2XfhvaIUfPO6MpJawaG92B2funXVb9qLs=
github.com/gdamore/tcell v1.1.1/go.mod h1:K1udHkiR3cOtlpKG5tZPD5XxrF7v2y7lDq7Whcj+xkQ=
github.com/jessevdk/go-flags v1.4.0 h1:4IU2WS7AumrZ/40jfhf4QVDMsQwqA7VEHozFRrGARJA=
github.com/jessevdk/go-flags v1.4.0/go.mod h1:4FA24M0QyGHXBuZZK/XkWh8h0e1EYbRYJSGM75WSRxI=
github.com/lucasb-eyer/go-colorful v0.0.0-20181028223441-12d3b2882a08 h1:5MnxBC15uMxFv5FY/J/8vzyaBiArCOkMdFT9Jsw78iY=
github.com/lucasb-eyer/go-colorful v0.0.0-20181028223441-12d3b2882a08/go.mod h1:NXg0ArsFk0Y01623LgUqoqcouGDB+PwCCQlrwrG6xJ4=
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
gopkg.in/DATA-DOG/go-sqlmock.v1 v1.3.0/go.mod h1:OdE7CF6DbADk7lN8LIKRzRJTTZXIjtWgA5THM5lhBAw=
51 changes: 25 additions & 26 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,25 +62,24 @@ var characters []rune
// streamDisplays by column number
var streamDisplaysByColumn = make(map[int]*StreamDisplay)

// current sizes
var curSizes sizes

// channel used to notify StreamDisplayManager
var sizesUpdateCh = make(chan sizes)

// struct sizes contains terminal sizes (in amount of characters)
type sizes struct {
width int
height int
width int
height int
curStreamsPerStreamDisplay int // current amount of streams per display allowed
}

var curSizes sizes // current sizes
var curStreamsPerStreamDisplay = 0 // current amount of streams per display allowed
var sizesUpdateCh = make(chan sizes) // channel used to notify StreamDisplayManager

// set the sizes and notify StreamDisplayManager
func setSizes(width int, height int) {
s := sizes{
width: width,
height: height,
}
curSizes = s
curStreamsPerStreamDisplay = 1 + height/10
sizesUpdateCh <- s
func (s *sizes) setSizes(width int, height int) {
s.width = width
s.height = height
s.curStreamsPerStreamDisplay = 1 + height/10
}

func main() {
Expand All @@ -107,20 +106,21 @@ func main() {
fmt.Println("Error: option --fps not within range 1-60")
os.Exit(1)
}

// Start profiling (if required)
if len(opts.Profile) > 0 {
f, err := os.Create(opts.Profile)
if err != nil {
fmt.Printf("Error opening profiling file: %s\n", err)
os.Exit(1)
}
pprof.StartCPUProfile(f)
err = pprof.StartCPUProfile(f)
if err != nil {
fmt.Printf("Error start profiling : %s\n", err)
os.Exit(1)
}
}

// Use a println for fun..
fmt.Println("Opening connection to The Matrix.. Please stand by..")

// setup logging with logfile /dev/null or ~/.gomatrix-log
filename := os.DevNull
if opts.Logging {
Expand All @@ -135,14 +135,13 @@ func main() {
log.SetOutput(logfile)
log.Println("-------------")
log.Println("Starting gomatrix. This logfile is for development/debug purposes.")

characters = allTheCharacters
if opts.Ascii {
characters = alphaNumerics
} else if opts.Kana {
characters = halfWidthKana
} else {
characters = allTheCharacters
}

// seed the rand package with time
rand.Seed(time.Now().UnixNano())

Expand Down Expand Up @@ -218,7 +217,8 @@ func main() {
}()

// set initial sizes
setSizes(screen.Size())
curSizes.setSizes(screen.Size())
sizesUpdateCh <- curSizes

// flusher flushes the termbox every x milliseconds
curFPS := opts.FPS
Expand All @@ -242,8 +242,7 @@ func main() {

// register signals to channel
sigChan := make(chan os.Signal)
signal.Notify(sigChan, os.Interrupt)
signal.Notify(sigChan, os.Kill)
signal.Notify(sigChan, os.Interrupt, os.Kill)

// handle tcell events and unix signals
EVENTS:
Expand Down Expand Up @@ -297,8 +296,8 @@ EVENTS:
}
case *tcell.EventResize: // set sizes
w, h := ev.Size()
setSizes(w, h)

curSizes.setSizes(w, h)
sizesUpdateCh <- curSizes
case *tcell.EventError: // quit
log.Fatalf("Quitting because of tcell error: %v", ev.Error())
}
Expand Down

0 comments on commit 0dfe1db

Please sign in to comment.