Skip to content
This repository has been archived by the owner on May 3, 2024. It is now read-only.

Commit

Permalink
lint, bug fix and dep update
Browse files Browse the repository at this point in the history
  • Loading branch information
djcas9 committed Nov 24, 2016
1 parent bbfe2d9 commit 5864a63
Show file tree
Hide file tree
Showing 50 changed files with 783 additions and 400 deletions.
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,10 @@ generate:
@go generate ./...

lint:
# @go vet $$(go list ./... | grep -v /vendor/)
# @for pkg in $$(go list ./... |grep -v /vendor/ |grep -v /services/) ; do \
# golint -min_confidence=1 $$pkg ; \
# done
@go vet $$(go list ./... | grep -v /vendor/)
@for pkg in $$(go list ./... |grep -v /vendor/ |grep -v /services/) ; do \
golint -min_confidence=1 $$pkg ; \
done

deps:
@go get -u github.com/golang/lint/golint
Expand Down
7 changes: 4 additions & 3 deletions komanda.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,14 @@ import (
"gopkg.in/alecthomas/kingpin.v2"
)

// Build value
var Build = ""

var (
app = kingpin.New(komanda.Name, komanda.Description)
ssl = app.Flag("ssl", "enable ssl").Short('s').Bool()

InsecureSkipVerify = app.
insecureSkipVerify = app.
Flag("insecure", "insecure ssl - skip verify. (self-signed certs)").
Short('i').Bool()

Expand Down Expand Up @@ -85,8 +86,8 @@ func main() {
config.C.Server.SSL = *ssl
}

if *InsecureSkipVerify {
config.C.Server.Insecure = *InsecureSkipVerify
if *insecureSkipVerify {
config.C.Server.Insecure = *insecureSkipVerify
}

if len(*host) > 0 {
Expand Down
33 changes: 24 additions & 9 deletions komanda/client/channel.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,40 +11,47 @@ import (
"github.com/mephux/komanda-cli/komanda/config"
)

// RenderHandlerFunc type for Exec callbacks
type RenderHandlerFunc func(*Channel, *gocui.View) error

var (
ANSIColors = []int{34, 36, 31, 35, 33, 37, 34, 32, 36, 31, 35, 33}
)

// User struct
type User struct {
Nick string
Mode string
Color int
}

// String converts the user struct to a string
func (u *User) String(c bool) string {
if u == nil {
return ""
}

if c {
return color.Stringf(u.Color, "%s%s", u.Mode, u.Nick)
} else {
return fmt.Sprintf("%s%s", u.Mode, u.Nick)
}

return fmt.Sprintf("%s%s", u.Mode, u.Nick)
}

// NickSorter cast to an array of user pointers
type NickSorter []*User

func (a NickSorter) Len() int { return len(a) }
func (a NickSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
// Len returns the list length
func (a NickSorter) Len() int { return len(a) }

// Swap moves the position of two items in the list
func (a NickSorter) Swap(i, j int) { a[i], a[j] = a[j], a[i] }

// Less checks if i is less than j to change position
func (a NickSorter) Less(i, j int) bool { return a[i].Nick < a[j].Nick }

// Channel struct
type Channel struct {
Status bool
Ready bool
Unread bool
Highlight bool
Name string
Server *Server
MaxX int
Expand All @@ -60,6 +67,7 @@ type Channel struct {
mu sync.Mutex
}

// FindUser returns a pointer for a given user or nil
func (channel *Channel) FindUser(nick string) *User {
for _, u := range channel.Users {
if u.Nick == nick {
Expand All @@ -70,10 +78,12 @@ func (channel *Channel) FindUser(nick string) *User {
return nil
}

// View returns the channel view
func (channel *Channel) View() (*gocui.View, error) {
return channel.Server.Gui.View(channel.Name)
}

// Update will render the current channel again
func (channel *Channel) Update() (*gocui.View, error) {
channel.MaxX, channel.MaxY = channel.Server.Gui.Size()

Expand All @@ -82,6 +92,7 @@ func (channel *Channel) Update() (*gocui.View, error) {

}

// NickListString will ouput the channel users in a pretty format
func (channel *Channel) NickListString(v *gocui.View, c bool) {
sort.Sort(NickSorter(channel.Users))

Expand All @@ -98,6 +109,7 @@ func (channel *Channel) NickListString(v *gocui.View, c bool) {
fmt.Fprintf(v, "\n%s", color.String(config.C.Color.Green, "== NICK LIST END\n\n"))
}

// NickMetricsString will output channel metrics in a pretty format
// 09:41 * Irssi: #google-containers: Total of 213 nicks [0 ops, 0 halfops, 0 voices, 213 normal]
func (channel *Channel) NickMetricsString(view *gocui.View) {
var op, hop, v, n int
Expand All @@ -119,6 +131,7 @@ func (channel *Channel) NickMetricsString(view *gocui.View) {
color.String(config.C.Color.Green, "**"), channel.Name, len(channel.Users), op, hop, v, n)
}

// RemoveNick from channel list
func (channel *Channel) RemoveNick(nick string) {
for i, user := range channel.Users {
if user.Nick == nick {
Expand All @@ -130,6 +143,7 @@ func (channel *Channel) RemoveNick(nick string) {
}
}

// AddNick to channel list
func (channel *Channel) AddNick(nick string) {

if u := channel.FindUser(nick); u == nil {
Expand All @@ -145,6 +159,7 @@ func (channel *Channel) AddNick(nick string) {
}
}

// Render the current channel
func (channel *Channel) Render(update bool) error {

view, err := channel.Server.Gui.SetView(channel.Name,
Expand All @@ -164,7 +179,7 @@ func (channel *Channel) Render(update bool) error {
view.BgColor = gocui.ColorBlack

if !channel.Private {
fmt.Fprintln(view, "\n\n")
fmt.Fprint(view, "\n\n\n")
} else {
channel.Topic = fmt.Sprintf("Private Chat: %s", channel.Name)
fmt.Fprint(view, "\n\n")
Expand Down
8 changes: 5 additions & 3 deletions komanda/client/codes.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package client

// IrcCodes for message hanlders
// https://www.alien.net.au/irc/irc2numerics.html
var IrcCodes = []string{
"0",
"001",
Expand Down Expand Up @@ -42,7 +44,7 @@ var IrcCodes = []string{
"365",
"325",
// "324",
"328",
// "328", // RPL_CHANNEL_URL
// "329",
"331",
// "333",
Expand All @@ -53,8 +55,8 @@ var IrcCodes = []string{
"357",
"348",
"349",
"367",
"368",
// "367", // TODO: figure out timestamp
// "368", // TODO: add banlist
"351",
"371",
"374",
Expand Down
3 changes: 1 addition & 2 deletions komanda/client/connect.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@ import (
ircClient "github.com/fluffle/goirc/client"
)

// "github.com/thoj/go-ircevent"

// New irc connection
func New(server *Server) *ircClient.Conn {
// irccon := irc.IRC(server.Nick, server.User)
// irccon.VerboseCallbackHandler = false
Expand Down
2 changes: 2 additions & 0 deletions komanda/client/format.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import (
"github.com/mephux/komanda-cli/komanda/config"
)

// StatusMessage will formart a string and write to the status
// channel
func StatusMessage(view *gocui.View, data string) {
timestamp := time.Now().Format(config.C.Time.MessageFormat)

Expand Down
56 changes: 47 additions & 9 deletions komanda/client/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,19 @@ import (
ircClient "github.com/fluffle/goirc/client"
"github.com/hectane/go-nonblockingchan"
"github.com/jroimartin/gocui"
"github.com/mephux/komanda-cli/komanda/helpers"
)

const (
// StatusChannel default name
StatusChannel = "komanda-status"
)

var (
layoutViews = []string{"header", "input", "menu"}
)

// Server struct
type Server struct {
Gui *gocui.Gui
Client *ircClient.Conn
Expand All @@ -35,31 +42,55 @@ type Server struct {
mu sync.Mutex
}

type Handler func(*gocui.Gui, *gocui.View, *Server) error
// Handler type for Exec function returns
type Handler func(*Channel, *gocui.Gui, *gocui.View, *Server) error

// Exec callback for a given channel
func (server *Server) Exec(channel string, h Handler) {
server.Gui.Execute(func(g *gocui.Gui) error {

v, err := g.View(channel)
if helpers.Contains(layoutViews, channel) {
v, err := g.View(channel)

if err != nil {
return err
}

return h(nil, server.Gui, v, server)
}

if err != nil {
c, _, has := server.HasChannel(channel)

if has {
v, err := c.View()

if err != nil {
return err
}

return h(c, server.Gui, v, server)
}

server.NewChannel(channel, false)
server.NewChannel(channel, false)

if v, err := g.View(channel); err == nil {
newC, _, newHas := server.HasChannel(channel)

server.CurrentChannel = channel
if newHas {
v, err := newC.View()

return h(server.Gui, v, server)
if err != nil {
return err
}

return err
server.CurrentChannel = channel
return h(newC, server.Gui, v, server)
}

return h(server.Gui, v, server)
return errors.New("error creating channel")
})
}

// HasChannel returns a channel if it exists in the server channel list
func (server *Server) HasChannel(name string) (*Channel, int, bool) {
for i, channel := range server.Channels {
if name == channel.Name {
Expand All @@ -70,6 +101,7 @@ func (server *Server) HasChannel(name string) (*Channel, int, bool) {
return nil, -1, false
}

// FindChannel in server channel list
func (server *Server) FindChannel(name string) *Channel {
c, _, has := server.HasChannel(name)

Expand All @@ -80,6 +112,7 @@ func (server *Server) FindChannel(name string) *Channel {
return nil
}

// ChannelView returns a channel view by name or returns an error
func (server *Server) ChannelView(name string) (*gocui.View, error) {
if c, _, ok := server.HasChannel(name); ok {
return c.View()
Expand All @@ -89,6 +122,7 @@ func (server *Server) ChannelView(name string) (*gocui.View, error) {

}

// AddChannel to server channel list
func (server *Server) AddChannel(channel *Channel) {

if _, _, ok := server.HasChannel(channel.Name); !ok {
Expand All @@ -100,6 +134,7 @@ func (server *Server) AddChannel(channel *Channel) {
}
}

// RemoveChannel will remove a channel from the server channel list
func (server *Server) RemoveChannel(name string) (int, error) {

channel, i, ok := server.HasChannel(name)
Expand All @@ -116,6 +151,7 @@ func (server *Server) RemoveChannel(name string) (int, error) {
return 0, nil
}

// GetCurrentChannel will return the current channel in view
func (server *Server) GetCurrentChannel() *Channel {

for _, s := range server.Channels {
Expand All @@ -127,13 +163,15 @@ func (server *Server) GetCurrentChannel() *Channel {
return server.Channels[0]
}

// NewChannel will create a channel and add it to the server list
func (server *Server) NewChannel(name string, private bool) error {
maxX, maxY := server.Gui.Size()

channel := Channel{
Topic: "N/A",
Ready: false,
Unread: private,
Highlight: false,
Name: name,
MaxX: maxX,
MaxY: maxY,
Expand Down
5 changes: 5 additions & 0 deletions komanda/color/color.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,27 @@ import (
"time"
)

// Stringf returnrs a color escape string with format options
func Stringf(c int, format string, args ...interface{}) string {
return fmt.Sprintf("\x1b[38;5;%dm%s\x1b[0m", c, fmt.Sprintf(format, args...))
}

// String returns a color escape string
func String(c int, str string) string {
return fmt.Sprintf("\x1b[38;5;%dm%s\x1b[0m", c, str)
}

// StringFormat returns a color escape string with extra options
func StringFormat(c int, str string, args []string) string {
return fmt.Sprintf("\x1b[38;5;%d;%sm%s\x1b[0m", c, strings.Join(args, ";"), str)
}

// StringFormatBoth fg and bg colors
func StringFormatBoth(fg, bg int, str string, args []string) string {
return fmt.Sprintf("\x1b[48;5;%dm\x1b[38;5;%d;%sm%s\x1b[0m", bg, fg, strings.Join(args, ";"), str)
}

// StringRandom returns a random colored string
func StringRandom(str string) string {
return String(Random(22, 231), str)
}
Expand Down
Loading

0 comments on commit 5864a63

Please sign in to comment.