1package termui
2
3import (
4 "fmt"
5 "github.com/MichaelMure/git-bug/bug"
6 "github.com/MichaelMure/git-bug/bug/operations"
7 "github.com/MichaelMure/git-bug/cache"
8 "github.com/MichaelMure/git-bug/util"
9 "github.com/jroimartin/gocui"
10)
11
12const showBugView = "showBugView"
13const showBugSidebarView = "showBugSidebarView"
14const showBugInstructionView = "showBugInstructionView"
15
16const timeLayout = "Jan _2 2006"
17
18type showBug struct {
19 cache cache.RepoCacher
20 bug *bug.Snapshot
21}
22
23func newShowBug(cache cache.RepoCacher) *showBug {
24 return &showBug{
25 cache: cache,
26 }
27}
28
29func (sb *showBug) layout(g *gocui.Gui) error {
30 maxX, maxY := g.Size()
31
32 v, err := g.SetView(showBugView, 0, 0, maxX*2/3, maxY-2)
33
34 if err != nil {
35 if err != gocui.ErrUnknownView {
36 return err
37 }
38
39 v.Frame = false
40 }
41
42 v.Clear()
43 sb.renderMain(v)
44
45 v, err = g.SetView(showBugSidebarView, maxX*2/3+1, 0, maxX-1, maxY-2)
46
47 if err != nil {
48 if err != gocui.ErrUnknownView {
49 return err
50 }
51
52 v.Frame = false
53 }
54
55 v.Clear()
56 sb.renderSidebar(v)
57
58 v, err = g.SetView(showBugInstructionView, -1, maxY-2, maxX, maxY)
59
60 if err != nil {
61 if err != gocui.ErrUnknownView {
62 return err
63 }
64
65 v.Frame = false
66 v.BgColor = gocui.ColorBlue
67
68 fmt.Fprintf(v, "[q] Return")
69 }
70
71 _, err = g.SetCurrentView(showBugView)
72 return err
73}
74
75func (sb *showBug) keybindings(g *gocui.Gui) error {
76 // Return
77 if err := g.SetKeybinding(showBugView, 'q', gocui.ModNone, sb.back); err != nil {
78 return err
79 }
80
81 if err := g.SetKeybinding(showBugView, gocui.KeyPgup, gocui.ModNone,
82 sb.scrollUp); err != nil {
83 return err
84 }
85 if err := g.SetKeybinding(showBugView, gocui.KeyPgdn, gocui.ModNone,
86 sb.scrollDown); err != nil {
87 return err
88 }
89
90 return nil
91}
92
93func (sb *showBug) disable(g *gocui.Gui) error {
94 if err := g.DeleteView(showBugView); err != nil {
95 return err
96 }
97 if err := g.DeleteView(showBugSidebarView); err != nil {
98 return err
99 }
100 if err := g.DeleteView(showBugInstructionView); err != nil {
101 return err
102 }
103 return nil
104}
105
106func (sb *showBug) renderMain(v *gocui.View) {
107 maxX, _ := v.Size()
108
109 header1 := fmt.Sprintf("[%s] %s", sb.bug.HumanId(), sb.bug.Title)
110 fmt.Fprintf(v, util.LeftPaddedString(header1, maxX, 2)+"\n\n")
111
112 header2 := fmt.Sprintf("[%s] %s opened this bug on %s",
113 sb.bug.Status, sb.bug.Author.Name, sb.bug.CreatedAt.Format(timeLayout))
114 fmt.Fprintf(v, util.LeftPaddedString(header2, maxX, 2)+"\n\n")
115
116 for _, op := range sb.bug.Operations {
117 switch op.(type) {
118
119 case operations.CreateOperation:
120 create := op.(operations.CreateOperation)
121 fmt.Fprintf(v, util.LeftPaddedString(create.Message, maxX, 6)+"\n\n\n")
122
123 case operations.AddCommentOperation:
124 comment := op.(operations.AddCommentOperation)
125 header := fmt.Sprintf("%s commented on %s",
126 comment.Author.Name, comment.Time().Format(timeLayout))
127 fmt.Fprintf(v, util.LeftPaddedString(header, maxX, 6)+"\n\n")
128 fmt.Fprintf(v, util.LeftPaddedString(comment.Message, maxX, 6)+"\n\n\n")
129 }
130 }
131
132}
133
134func (sb *showBug) renderSidebar(v *gocui.View) {
135 maxX, _ := v.Size()
136
137 title := util.LeftPaddedString("LABEL", maxX, 2)
138 fmt.Fprintf(v, title+"\n\n")
139
140 for _, label := range sb.bug.Labels {
141 fmt.Fprintf(v, util.LeftPaddedString(label.String(), maxX, 2))
142 fmt.Fprintln(v)
143 }
144}
145
146func (sb *showBug) back(g *gocui.Gui, v *gocui.View) error {
147 sb.bug = nil
148 ui.activateWindow(ui.bugTable)
149 return nil
150}
151
152func (sb *showBug) scrollUp(g *gocui.Gui, v *gocui.View) error {
153 return nil
154}
155
156func (sb *showBug) scrollDown(g *gocui.Gui, v *gocui.View) error {
157 return nil
158}