From 9b17c483a821d87bd208624a3f40e41251dbb815 Mon Sep 17 00:00:00 2001 From: Jan De Dobbeleer Date: Tue, 7 Jun 2022 07:20:32 +0200 Subject: [PATCH] feat: install font directly relates to #2338 --- src/cli/font.go | 153 +++++++++++-------------------------------- src/font/download.go | 1 - src/font/install.go | 10 +++ 3 files changed, 49 insertions(+), 115 deletions(-) diff --git a/src/cli/font.go b/src/cli/font.go index 715f7c8b..11f47a6e 100644 --- a/src/cli/font.go +++ b/src/cli/font.go @@ -3,129 +3,54 @@ package cli import ( "fmt" "oh-my-posh/font" + "os" "github.com/spf13/cobra" ) -// fontCmd can work with fonts -var fontCmd = &cobra.Command{ - Use: "font [install|configure]", - Short: "Manage fonts", - Long: `Manage fonts. +var ( + fontName string + + // fontCmd can work with fonts + fontCmd = &cobra.Command{ + Use: "font [install|configure]", + Short: "Manage fonts", + Long: `Manage fonts. This command is used to install fonts and configure the font in your terminal. - install: oh-my-posh font install https://github.com/ryanoasis/nerd-fonts/releases/download/v2.1.0/3270.zip`, - ValidArgs: []string{ - "install", - "configure", - }, - Run: func(cmd *cobra.Command, args []string) { - if len(args) == 0 { - _ = cmd.Help() - return - } - switch args[0] { - case "install": - font.Run() - case "configure": - fmt.Println("not implemented") - default: - _ = cmd.Help() - } - }, -} + ValidArgs: []string{ + "install", + "configure", + }, + Run: func(cmd *cobra.Command, args []string) { + if len(args) == 0 { + _ = cmd.Help() + return + } + switch args[0] { + case "install": + if len(fontName) == 0 { + font.Run() + return + } + err := font.Install(fontName) + if err != nil { + fmt.Println(err.Error()) + os.Exit(1) + } + return + case "configure": + fmt.Println("not implemented") + default: + _ = cmd.Help() + } + }, + } +) func init() { // nolint:gochecknoinits + fontCmd.Flags().StringVarP(&fontName, "font", "f", "", "the font name to install") rootCmd.AddCommand(fontCmd) } - -// type fontsModel struct { -// fonts []*font.Asset // the list of choices -// cursor int // which item our cursor is pointing at -// selected map[int]*font.Asset // which items are selected -// } - -// func initFontsModel() (*fontsModel, error) { -// nerdFonts, err := font.Nerds() -// if err != nil { -// return nil, err -// } -// return &fontsModel{ -// fonts: nerdFonts, -// selected: make(map[int]*font.Asset), -// }, nil -// } - -// func (f fontsModel) Init() tea.Cmd { -// // Just return `nil`, which means "no I/O right now, please." -// return nil -// } - -// func (f fontsModel) Update(msg tea.Msg) (tea.Model, tea.Cmd) { -// switch msg := msg.(type) { -// // Is it a key press? -// case tea.KeyMsg: -// // Cool, what was the actual key pressed? -// switch msg.String() { -// // These keys should exit the program. -// case "ctrl+c", "q": -// return f, tea.Quit - -// // The "up" and "k" keys move the cursor up -// case "up", "k": -// if f.cursor > 0 { -// f.cursor-- -// } - -// // The "down" and "j" keys move the cursor down -// case "down", "j": -// if f.cursor < len(f.fonts)-1 { -// f.cursor++ -// } - -// // The "enter" key and the spacebar (a literal space) toggle -// // the selected state for the item that the cursor is pointing at. -// case "enter", " ": -// _, ok := f.selected[f.cursor] -// if ok { -// delete(f.selected, f.cursor) -// } else { -// f.selected[f.cursor] = f.fonts[f.cursor] -// } -// } -// } - -// // Return the updated model to the Bubble Tea runtime for processing. -// // Note that we're not returning a command. -// return f, nil -// } - -// func (f fontsModel) View() string { -// // The header -// s := "Which font do you want to install?\n\n" - -// // Iterate over our choices -// for i, choice := range f.fonts { -// // Is the cursor pointing at this choice? -// cursor := " " // no cursor -// if f.cursor == i { -// cursor = ">" // cursor! -// } - -// // Is this choice selected? -// checked := " " // not selected -// if _, ok := f.selected[i]; ok { -// checked = "x" // selected! -// } - -// // Render the row -// s += fmt.Sprintf("%s [%s] %s\n", cursor, checked, choice.Name) -// } - -// // The footer -// s += "\nPress q to quit.\n" - -// // Send the UI for rendering -// return s -// } diff --git a/src/font/download.go b/src/font/download.go index 45f3042a..7264b8bf 100644 --- a/src/font/download.go +++ b/src/font/download.go @@ -35,7 +35,6 @@ func isZipFile(data []byte) bool { } func getRemoteFile(location string) (data []byte, err error) { - print("Downloading %s", location) var client = http.Client{} resp, err := client.Get(location) diff --git a/src/font/install.go b/src/font/install.go index f6f14e3e..efe858d4 100644 --- a/src/font/install.go +++ b/src/font/install.go @@ -5,12 +5,22 @@ package font import ( "archive/zip" "bytes" + "fmt" "io" "path" "runtime" "strings" ) +func Install(font string) error { + location := fmt.Sprintf("https://github.com/ryanoasis/nerd-fonts/releases/latest/download/%s.zip", font) + zipFile, err := Download(location) + if err != nil { + return err + } + return InstallZIP(zipFile) +} + func InstallZIP(data []byte) (err error) { bytesReader := bytes.NewReader(data)