Skip to content

Commit

Permalink
fix a bug on selecting empty list and remove useless code (#53)
Browse files Browse the repository at this point in the history
* fix a bug on selecting empty list and remove useless code

* bump version
  • Loading branch information
isacikgoz authored Jun 18, 2019
1 parent ed83094 commit 22ae27f
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 25 deletions.
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ func evalArgs() string {
pin.Command("status", "Show working-tree status. Also stage and commit changes.")
pin.Command("branch", "Show list of branches.")

pin.Version("gitin version 0.2.0")
pin.Version("gitin version 0.2.1")

pin.UsageTemplate(pin.DefaultUsageTemplate + additionalHelp() + "\n")
pin.CommandLine.HelpFlag.Short('h')
Expand Down
8 changes: 7 additions & 1 deletion prompt/branch.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package prompt

import (
"fmt"
"os/exec"

"github.com/fatih/color"
Expand Down Expand Up @@ -39,7 +40,6 @@ func (b *Branch) Start(opts *Options) error {
b.prompt = &prompt{
list: list,
opts: opts,
layout: branch,
selection: b.onSelect,
keys: b.onKey,
info: b.branchInfo,
Expand All @@ -51,6 +51,9 @@ func (b *Branch) Start(opts *Options) error {

func (b *Branch) onSelect() bool {
items, idx := b.prompt.list.Items()
if idx == NotFound {
return false
}
branch := items[idx].(*git.Branch)
args := []string{"checkout", branch.Name}
cmd := exec.Command("git", args...)
Expand Down Expand Up @@ -92,6 +95,9 @@ func (b *Branch) branchInfo(item Item) [][]term.Cell {

func (b *Branch) deleteBranch(mode string) error {
items, idx := b.prompt.list.Items()
if idx == NotFound {
return fmt.Errorf("there is no item to delete")
}
branch := items[idx].(*git.Branch)
cmd := exec.Command("git", "branch", "-"+mode, branch.Name)
cmd.Dir = b.Repo.Path()
Expand Down
16 changes: 14 additions & 2 deletions prompt/log.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package prompt

import (
"fmt"
"strconv"
"strings"

Expand Down Expand Up @@ -45,7 +46,6 @@ func (l *Log) Start(opts *Options) error {
l.prompt = &prompt{
list: list,
opts: opts,
layout: log,
keys: l.onKey,
selection: l.onSelect,
info: l.logInfo,
Expand All @@ -59,6 +59,9 @@ func (l *Log) Start(opts *Options) error {
func (l *Log) onSelect() bool {
// s.showDiff()
items, idx := l.prompt.list.Items()
if idx == NotFound {
return false
}
item := items[idx]
switch item.(type) {
case *git.Commit:
Expand Down Expand Up @@ -94,7 +97,10 @@ func (l *Log) onSelect() bool {

func (l *Log) onKey(key rune) bool {
items, idx := l.prompt.list.Items()
item := items[idx]
var item Item
if idx != NotFound {
item = items[idx]
}
switch item.(type) {
case *git.Commit:
switch key {
Expand All @@ -120,13 +126,19 @@ func (l *Log) onKey(key rune) bool {

func (l *Log) showDiff() error {
items, idx := l.prompt.list.Items()
if idx == NotFound {
return fmt.Errorf("there is no item to show diff")
}
commit := items[idx].(*git.Commit)
args := []string{"show", commit.Hash}
return popGitCommand(l.Repo, args)
}

func (l *Log) showStat() error {
items, idx := l.prompt.list.Items()
if idx == NotFound {
return fmt.Errorf("there is no item to show diff")
}
commit := items[idx].(*git.Commit)
args := []string{"show", "--stat", commit.Hash}
return popGitCommand(l.Repo, args)
Expand Down
24 changes: 5 additions & 19 deletions prompt/prompt.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,6 @@ import (
"github.com/isacikgoz/gitin/term"
)

type promptType int

const (
status promptType = iota
log
file
branch
stash
)

type keyEvent struct {
ch rune
err error
Expand All @@ -48,8 +38,6 @@ type promptState struct {
}

type prompt struct {
layout promptType

list *List
keys onKey
selection onSelect
Expand Down Expand Up @@ -112,12 +100,11 @@ func (p *prompt) start() error {
return err
}

// if p.exitMsg != nil {
for _, cells := range p.exitMsg {
p.writer.WriteCells(cells)
}
p.writer.Flush()
// }

return nil
}

Expand Down Expand Up @@ -160,13 +147,15 @@ mainloop:
func (p *prompt) render() {
// lock screen mutex
p.mx.Lock()
defer p.mx.Unlock()
defer func() {
p.writer.Flush()
p.mx.Unlock()
}()

if p.helpMode {
for _, line := range genHelp(p.allControls()) {
p.writer.WriteCells(line)
}
p.writer.Flush()
return
}

Expand All @@ -188,9 +177,6 @@ func (p *prompt) render() {
} else {
p.writer.WriteCells(term.Cprint("Not found.", color.FgRed))
}

// finally, discharge to terminal
p.writer.Flush()
}

func (p *prompt) assignKey(key rune) bool {
Expand Down
5 changes: 3 additions & 2 deletions prompt/status.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,6 @@ func (s *Status) Start(opts *Options) error {
s.prompt = &prompt{
list: l,
opts: opts,
layout: status,
keys: s.onKey,
selection: s.onSelect,
info: s.branchInfo,
Expand All @@ -70,7 +69,6 @@ func (s *Status) onSelect() bool {

func (s *Status) onKey(key rune) bool {
var reqReload bool

switch key {
case ' ':
reqReload = true
Expand Down Expand Up @@ -178,6 +176,9 @@ func (s *Status) hunkStage() error {
// pop git diff
func (s *Status) showDiff() error {
items, idx := s.prompt.list.Items()
if idx == NotFound {
return fmt.Errorf("there is no item to show diff")
}
entry := items[idx].(*git.StatusEntry)
return popGitCommand(s.Repo, fileStatArgs(entry))
}
Expand Down

0 comments on commit 22ae27f

Please sign in to comment.