Skip to content
Myr

main.go

This file

Path

examples/table/main.go

185 lines · 6.1 KiB · Go

Last changed by

fix(examples): add missing `WithWidth` to table example (#1598)Shivang Srivastavabefore adoption110a919

History of this file

Take it

1package main23import (4	"fmt"5	"os"67	"charm.land/bubbles/v2/table"8	tea "charm.land/bubbletea/v2"9	"charm.land/lipgloss/v2"10)1112var baseStyle = lipgloss.NewStyle().13	BorderStyle(lipgloss.NormalBorder()).14	BorderForeground(lipgloss.Color("240"))1516type model struct {17	table table.Model18}1920func (m model) Init() tea.Cmd { return nil }2122func (m model) Update(msg tea.Msg) (tea.Model, tea.Cmd) {23	var cmd tea.Cmd24	switch msg := msg.(type) {25	case tea.KeyPressMsg:26		switch msg.String() {27		case "esc":28			if m.table.Focused() {29				m.table.Blur()30			} else {31				m.table.Focus()32			}33		case "q", "ctrl+c":34			return m, tea.Quit35		case "enter":36			return m, tea.Batch(37				tea.Printf("Let's go to %s!", m.table.SelectedRow()[1]),38			)39		}40	}41	m.table, cmd = m.table.Update(msg)42	return m, cmd43}4445func (m model) View() tea.View {46	return tea.NewView(baseStyle.Render(m.table.View()) + "\n  " + m.table.HelpView() + "\n")47}4849func main() {50	columns := []table.Column{51		{Title: "Rank", Width: 4},52		{Title: "City", Width: 10},53		{Title: "Country", Width: 10},54		{Title: "Population", Width: 10},55	}5657	rows := []table.Row{58		{"1", "Tokyo", "Japan", "37,274,000"},59		{"2", "Delhi", "India", "32,065,760"},60		{"3", "Shanghai", "China", "28,516,904"},61		{"4", "Dhaka", "Bangladesh", "22,478,116"},62		{"5", "São Paulo", "Brazil", "22,429,800"},63		{"6", "Mexico City", "Mexico", "22,085,140"},64		{"7", "Cairo", "Egypt", "21,750,020"},65		{"8", "Beijing", "China", "21,333,332"},66		{"9", "Mumbai", "India", "20,961,472"},67		{"10", "Osaka", "Japan", "19,059,856"},68		{"11", "Chongqing", "China", "16,874,740"},69		{"12", "Karachi", "Pakistan", "16,839,950"},70		{"13", "Istanbul", "Turkey", "15,636,243"},71		{"14", "Kinshasa", "DR Congo", "15,628,085"},72		{"15", "Lagos", "Nigeria", "15,387,639"},73		{"16", "Buenos Aires", "Argentina", "15,369,919"},74		{"17", "Kolkata", "India", "15,133,888"},75		{"18", "Manila", "Philippines", "14,406,059"},76		{"19", "Tianjin", "China", "14,011,828"},77		{"20", "Guangzhou", "China", "13,964,637"},78		{"21", "Rio De Janeiro", "Brazil", "13,634,274"},79		{"22", "Lahore", "Pakistan", "13,541,764"},80		{"23", "Bangalore", "India", "13,193,035"},81		{"24", "Shenzhen", "China", "12,831,330"},82		{"25", "Moscow", "Russia", "12,640,818"},83		{"26", "Chennai", "India", "11,503,293"},84		{"27", "Bogota", "Colombia", "11,344,312"},85		{"28", "Paris", "France", "11,142,303"},86		{"29", "Jakarta", "Indonesia", "11,074,811"},87		{"30", "Lima", "Peru", "11,044,607"},88		{"31", "Bangkok", "Thailand", "10,899,698"},89		{"32", "Hyderabad", "India", "10,534,418"},90		{"33", "Seoul", "South Korea", "9,975,709"},91		{"34", "Nagoya", "Japan", "9,571,596"},92		{"35", "London", "United Kingdom", "9,540,576"},93		{"36", "Chengdu", "China", "9,478,521"},94		{"37", "Nanjing", "China", "9,429,381"},95		{"38", "Tehran", "Iran", "9,381,546"},96		{"39", "Ho Chi Minh City", "Vietnam", "9,077,158"},97		{"40", "Luanda", "Angola", "8,952,496"},98		{"41", "Wuhan", "China", "8,591,611"},99		{"42", "Xi An Shaanxi", "China", "8,537,646"},100		{"43", "Ahmedabad", "India", "8,450,228"},101		{"44", "Kuala Lumpur", "Malaysia", "8,419,566"},102		{"45", "New York City", "United States", "8,177,020"},103		{"46", "Hangzhou", "China", "8,044,878"},104		{"47", "Surat", "India", "7,784,276"},105		{"48", "Suzhou", "China", "7,764,499"},106		{"49", "Hong Kong", "Hong Kong", "7,643,256"},107		{"50", "Riyadh", "Saudi Arabia", "7,538,200"},108		{"51", "Shenyang", "China", "7,527,975"},109		{"52", "Baghdad", "Iraq", "7,511,920"},110		{"53", "Dongguan", "China", "7,511,851"},111		{"54", "Foshan", "China", "7,497,263"},112		{"55", "Dar Es Salaam", "Tanzania", "7,404,689"},113		{"56", "Pune", "India", "6,987,077"},114		{"57", "Santiago", "Chile", "6,856,939"},115		{"58", "Madrid", "Spain", "6,713,557"},116		{"59", "Haerbin", "China", "6,665,951"},117		{"60", "Toronto", "Canada", "6,312,974"},118		{"61", "Belo Horizonte", "Brazil", "6,194,292"},119		{"62", "Khartoum", "Sudan", "6,160,327"},120		{"63", "Johannesburg", "South Africa", "6,065,354"},121		{"64", "Singapore", "Singapore", "6,039,577"},122		{"65", "Dalian", "China", "5,930,140"},123		{"66", "Qingdao", "China", "5,865,232"},124		{"67", "Zhengzhou", "China", "5,690,312"},125		{"68", "Ji Nan Shandong", "China", "5,663,015"},126		{"69", "Barcelona", "Spain", "5,658,472"},127		{"70", "Saint Petersburg", "Russia", "5,535,556"},128		{"71", "Abidjan", "Ivory Coast", "5,515,790"},129		{"72", "Yangon", "Myanmar", "5,514,454"},130		{"73", "Fukuoka", "Japan", "5,502,591"},131		{"74", "Alexandria", "Egypt", "5,483,605"},132		{"75", "Guadalajara", "Mexico", "5,339,583"},133		{"76", "Ankara", "Turkey", "5,309,690"},134		{"77", "Chittagong", "Bangladesh", "5,252,842"},135		{"78", "Addis Ababa", "Ethiopia", "5,227,794"},136		{"79", "Melbourne", "Australia", "5,150,766"},137		{"80", "Nairobi", "Kenya", "5,118,844"},138		{"81", "Hanoi", "Vietnam", "5,067,352"},139		{"82", "Sydney", "Australia", "5,056,571"},140		{"83", "Monterrey", "Mexico", "5,036,535"},141		{"84", "Changsha", "China", "4,809,887"},142		{"85", "Brasilia", "Brazil", "4,803,877"},143		{"86", "Cape Town", "South Africa", "4,800,954"},144		{"87", "Jiddah", "Saudi Arabia", "4,780,740"},145		{"88", "Urumqi", "China", "4,710,203"},146		{"89", "Kunming", "China", "4,657,381"},147		{"90", "Changchun", "China", "4,616,002"},148		{"91", "Hefei", "China", "4,496,456"},149		{"92", "Shantou", "China", "4,490,411"},150		{"93", "Xinbei", "Taiwan", "4,470,672"},151		{"94", "Kabul", "Afghanistan", "4,457,882"},152		{"95", "Ningbo", "China", "4,405,292"},153		{"96", "Tel Aviv", "Israel", "4,343,584"},154		{"97", "Yaounde", "Cameroon", "4,336,670"},155		{"98", "Rome", "Italy", "4,297,877"},156		{"99", "Shijiazhuang", "China", "4,285,135"},157		{"100", "Montreal", "Canada", "4,276,526"},158	}159160	t := table.New(161		table.WithColumns(columns),162		table.WithRows(rows),163		table.WithFocused(true),164		table.WithHeight(7),165		table.WithWidth(42),166	)167168	s := table.DefaultStyles()169	s.Header = s.Header.170		BorderStyle(lipgloss.NormalBorder()).171		BorderForeground(lipgloss.Color("240")).172		BorderBottom(true).173		Bold(false)174	s.Selected = s.Selected.175		Foreground(lipgloss.Color("229")).176		Background(lipgloss.Color("57")).177		Bold(false)178	t.SetStyles(s)179180	m := model{t}181	if _, err := tea.NewProgram(m).Run(); err != nil {182		fmt.Println("Error running program:", err)183		os.Exit(1)184	}185}