|
| 1 | +package cmd |
| 2 | + |
| 3 | +import ( |
| 4 | + "fmt" |
| 5 | + "github.com/charmbracelet/bubbles/help" |
| 6 | + "github.com/charmbracelet/bubbles/key" |
| 7 | + "github.com/charmbracelet/bubbles/progress" |
| 8 | + "github.com/charmbracelet/bubbles/table" |
| 9 | + tea "github.com/charmbracelet/bubbletea" |
| 10 | + "github.com/charmbracelet/lipgloss" |
| 11 | + "github.com/fatih/color" |
| 12 | + "regexp" |
| 13 | + "strconv" |
| 14 | + "strings" |
| 15 | + "wxapkg/util" |
| 16 | +) |
| 17 | + |
| 18 | +var baseStyle = lipgloss.NewStyle(). |
| 19 | + BorderStyle(lipgloss.ThickBorder()). |
| 20 | + BorderForeground(lipgloss.Color("240")) |
| 21 | + |
| 22 | +type scanTui struct { |
| 23 | + table table.Model |
| 24 | + raw []util.WxidInfo |
| 25 | + selected *util.WxidInfo |
| 26 | + progress progress.Model |
| 27 | +} |
| 28 | + |
| 29 | +func newScanTui(wxidInfo []util.WxidInfo) *scanTui { |
| 30 | + var prog = progress.New(progress.WithScaledGradient("#FF7CCB", "#FDFF8C")) |
| 31 | + |
| 32 | + var rows = make([]table.Row, 0, len(wxidInfo)) |
| 33 | + for _, info := range wxidInfo { |
| 34 | + rows = append(rows, []string{ |
| 35 | + info.Nickname, |
| 36 | + info.PrincipalName, |
| 37 | + info.Description, |
| 38 | + }) |
| 39 | + } |
| 40 | + |
| 41 | + var title = color.New(color.FgMagenta, color.Bold).Sprint |
| 42 | + columns := []table.Column{ |
| 43 | + {Title: title("Name"), Width: 20}, |
| 44 | + {Title: title("Developer"), Width: 30}, |
| 45 | + {Title: title("Description"), Width: 40}, |
| 46 | + } |
| 47 | + prog.Width = 0 |
| 48 | + for _, c := range columns { |
| 49 | + prog.Width += c.Width |
| 50 | + } |
| 51 | + prog.Width += len(columns) * 2 |
| 52 | + |
| 53 | + var height = 10 |
| 54 | + if len(rows) < height { |
| 55 | + height = len(rows) |
| 56 | + } |
| 57 | + t := table.New( |
| 58 | + table.WithColumns(columns), |
| 59 | + table.WithRows(rows), |
| 60 | + table.WithFocused(true), |
| 61 | + table.WithHeight(height), |
| 62 | + ) |
| 63 | + t.Rows() |
| 64 | + |
| 65 | + s := table.DefaultStyles() |
| 66 | + s.Header = s.Header. |
| 67 | + BorderStyle(lipgloss.NormalBorder()). |
| 68 | + BorderForeground(lipgloss.Color("240")). |
| 69 | + BorderBottom(true). |
| 70 | + Bold(false) |
| 71 | + s.Selected = s.Selected. |
| 72 | + Foreground(lipgloss.Color("229")). |
| 73 | + Background(lipgloss.Color("57")). |
| 74 | + Bold(false) |
| 75 | + t.SetStyles(s) |
| 76 | + |
| 77 | + return &scanTui{ |
| 78 | + table: t, |
| 79 | + raw: wxidInfo, |
| 80 | + progress: prog, |
| 81 | + } |
| 82 | +} |
| 83 | + |
| 84 | +func (s *scanTui) Init() tea.Cmd { |
| 85 | + return nil |
| 86 | +} |
| 87 | + |
| 88 | +func (s *scanTui) Update(msg tea.Msg) (tea.Model, tea.Cmd) { |
| 89 | + var cmd tea.Cmd |
| 90 | + switch msg := msg.(type) { |
| 91 | + case tea.KeyMsg: |
| 92 | + switch msg.String() { |
| 93 | + case "esc": |
| 94 | + if s.table.Focused() { |
| 95 | + s.table.Blur() |
| 96 | + } else { |
| 97 | + s.table.Focus() |
| 98 | + } |
| 99 | + case "q", "ctrl+c": |
| 100 | + return s, tea.Quit |
| 101 | + case "enter": |
| 102 | + s.selected = &s.raw[s.table.Cursor()] |
| 103 | + return s, tea.Quit |
| 104 | + } |
| 105 | + } |
| 106 | + s.table, cmd = s.table.Update(msg) |
| 107 | + return s, cmd |
| 108 | +} |
| 109 | + |
| 110 | +func (s *scanTui) renderProgress() string { |
| 111 | + var prog = s.progress.ViewAs(float64(s.table.Cursor()+1) / float64(len(s.raw))) |
| 112 | + var p = regexp.MustCompile(`\d{1,3}%`).FindString(prog) |
| 113 | + format := "%" + strconv.Itoa(len(p)) + "s" |
| 114 | + newStr := fmt.Sprintf(format, fmt.Sprintf("%d/%d", s.table.Cursor()+1, len(s.raw))) |
| 115 | + prog = strings.Replace(prog, p, newStr, 1) |
| 116 | + |
| 117 | + return prog |
| 118 | +} |
| 119 | + |
| 120 | +func (s *scanTui) renderDetail() string { |
| 121 | + var result = "" |
| 122 | + |
| 123 | + var info = s.raw[s.table.Cursor()] |
| 124 | + |
| 125 | + var link = color.New(color.Italic, color.Underline).Sprint |
| 126 | + var title = color.New(color.FgMagenta, color.Bold).Sprint |
| 127 | + var content = color.CyanString |
| 128 | + |
| 129 | + if info.Error != "" { |
| 130 | + result += title(" error: ") + color.RedString(info.Error) + "\n" |
| 131 | + } |
| 132 | + |
| 133 | + if info.Error == "" { |
| 134 | + result += title(" wxid: ") + content(info.Wxid) + "\n" |
| 135 | + result += title(" Name: ") + content(info.Nickname) + "\n" |
| 136 | + result += title(" Developer: ") + content(info.PrincipalName) + "\n" |
| 137 | + result += title(" Description: ") + content(info.Description) + "\n" |
| 138 | + } |
| 139 | + |
| 140 | + result += title(" Location: ") + content(link(info.Location)) + "\n" |
| 141 | + |
| 142 | + if info.Error == "" { |
| 143 | + result += title(" Avatar: ") + content(link(info.Avatar)) + "\n" |
| 144 | + } |
| 145 | + |
| 146 | + result += title(" All information see '") + content(".\\"+util.CachePath) + title("'") |
| 147 | + |
| 148 | + return result |
| 149 | +} |
| 150 | + |
| 151 | +func (s *scanTui) renderHelp() string { |
| 152 | + return help.New().ShortHelpView([]key.Binding{ |
| 153 | + key.NewBinding( |
| 154 | + key.WithKeys("enter"), |
| 155 | + key.WithHelp("enter", "unpack"), |
| 156 | + ), |
| 157 | + key.NewBinding( |
| 158 | + key.WithKeys("up", "k"), |
| 159 | + key.WithHelp("↑/k", "move up"), |
| 160 | + ), |
| 161 | + key.NewBinding( |
| 162 | + key.WithKeys("down", "j"), |
| 163 | + key.WithHelp("↓/j", "move down"), |
| 164 | + ), |
| 165 | + key.NewBinding( |
| 166 | + key.WithKeys("q", "ctrl+c"), |
| 167 | + key.WithHelp("q", "exit"), |
| 168 | + ), |
| 169 | + }) |
| 170 | +} |
| 171 | + |
| 172 | +func (s *scanTui) View() string { |
| 173 | + var result = "" |
| 174 | + result += "" + s.renderProgress() + "\n" |
| 175 | + result += baseStyle.Render(s.table.View()) + "\n" |
| 176 | + result += s.renderDetail() + "\n" |
| 177 | + result += "\n " + s.renderHelp() + "\n" |
| 178 | + |
| 179 | + return result |
| 180 | +} |
0 commit comments