From f9f78ac7dbf6d61e06afe2899e43cb8043d516cb Mon Sep 17 00:00:00 2001 From: esimov Date: Mon, 8 Jul 2024 18:16:26 +0300 Subject: [PATCH] Code improvements and fixing issues --- color/color.go | 8 +- io/diagrams.go | 10 +- io/file.go | 47 ++++----- main.go | 11 +- ui/console.go | 4 +- ui/editor.go | 2 +- ui/handlers.go | 60 +++++++---- ui/panels.go | 252 +++++++++++++++++++++++---------------------- ui/ui.go | 2 +- version/version.go | 18 ++-- 10 files changed, 216 insertions(+), 198 deletions(-) diff --git a/color/color.go b/color/color.go index cd7fd60..1b5de36 100644 --- a/color/color.go +++ b/color/color.go @@ -27,13 +27,9 @@ func StringFormatBoth(fg, bg int, str string, args []string) string { return fmt.Sprintf("\x1b[48;5;%dm\x1b[38;5;%d;%sm%s\x1b[0m", bg, fg, strings.Join(args, ";"), str) } -// StringRandom returns a random colored string. -func StringRandom(str string) string { - return String(Random(180, 231), str) -} - // Random color number. func Random(min, max int) int { - rand.Seed(time.Now().UTC().UnixNano()) + src := rand.NewSource(time.Now().UnixNano()) + rand := rand.New(src) return rand.Intn(max-min) + min } diff --git a/io/diagrams.go b/io/diagrams.go index 8d36b26..4e32c47 100644 --- a/io/diagrams.go +++ b/io/diagrams.go @@ -1,8 +1,8 @@ package io import ( - "io/ioutil" - "log" + "fmt" + "os" "path/filepath" ) @@ -12,13 +12,15 @@ func ListDiagrams(dir string) ([]string, error) { cwd, err := filepath.Abs(filepath.Dir("")) if err != nil { - log.Fatal(err) + return nil, fmt.Errorf("could not return the current working directory: %w", err) } + path := cwd + dir - files, err := ioutil.ReadDir(path) + files, err := os.ReadDir(path) if err != nil { return nil, err } + for _, file := range files { diagrams = append(diagrams, file.Name()) } diff --git a/io/file.go b/io/file.go index 532f10c..c33cd35 100644 --- a/io/file.go +++ b/io/file.go @@ -2,67 +2,58 @@ package io import ( "fmt" - "io/ioutil" - "log" "os" "path/filepath" ) // ReadFile read the input file. -func ReadFile(input string) []byte { - b, err := ioutil.ReadFile(input) +func ReadFile(input string) ([]byte, error) { + b, err := os.ReadFile(input) if err != nil { - return nil + return nil, fmt.Errorf("error reading the file: %w", err) } - return b + return b, nil } // SaveFile saves the diagram into the output directory. func SaveFile(fileName string, output string, data string) (*os.File, error) { var file *os.File - cwd, err := filepath.Abs(filepath.Dir("")) - if err != nil { - log.Fatal(err) - } + cwd, _ := filepath.Abs(filepath.Dir("")) + filePath := cwd + output - // Check if output directory does not exits. In this case create it. + // Check if the output directory does not exits. In this case create it. if _, err := os.Stat(filePath); os.IsNotExist(err) { os.Mkdir(filePath, os.ModePerm) } + // Create the output file. - file, err = os.Create(filepath.Join(filePath, fileName)) - if isError(err) { - return nil, err + file, err := os.Create(filepath.Join(filePath, fileName)) + if err != nil { + return nil, fmt.Errorf("error creating the file: %w", err) } + defer file.Close() file, err = os.OpenFile(filepath.Join(filePath, fileName), os.O_RDWR, 0644) - if isError(err) { - return nil, err + if err != nil { + return nil, fmt.Errorf("error opening the file: %w", err) } - defer file.Close() _, err = file.WriteString(data) - if isError(err) { + if err != nil { return nil, err } + return file, nil } // DeleteDiagram deletes a diagram. func DeleteDiagram(fileName string) error { err := os.Remove(fileName) - if isError(err) { - return err - } - return nil -} - -// isError ia a generic function to check for errors. -func isError(err error) bool { if err != nil { - fmt.Errorf("Could not save file: %v", err.Error()) + return fmt.Errorf("failed deleting the diagram: %w", err) } - return (err != nil) + + return nil } diff --git a/main.go b/main.go index c738a9f..e72db04 100644 --- a/main.go +++ b/main.go @@ -1,4 +1,4 @@ -// Package Diagram is a Go package to generate hand drawn diagrams from ASCII arts. +// Diagram is a Go library to generate hand drawn diagrams from ASCII arts. // // It's a full featured CLI application which converts the ASCII text into hand drawn diagrams. package main @@ -51,9 +51,12 @@ func main() { // In case the option parameters are used, the hand-drawn diagrams are generated without to enter into the CLI app. if (*source != "") && (*destination != "") { - input := string(io.ReadFile(*source)) + input, err := io.ReadFile("sample.txt") + if err != nil { + log.Fatalf("error reading the sample file: %v", err) + } - err := canvas.DrawDiagram(input, *destination, *fontPath) + err = canvas.DrawDiagram(string(input), *destination, *fontPath) if err != nil { log.Fatal("Error on converting the ascii art to hand drawn diagrams!") } else if *preview { @@ -67,7 +70,7 @@ func main() { log.Fatalf("Failed to read image '%s': %v\n", *destination, err) } - gui := gui.NewGUI(source, "ASCII diagram preview") + gui := gui.NewGUI(source, "Diagram preview") if err := gui.Draw(); err != nil { log.Fatalf("diagram GUI draw error: %v", err) diff --git a/ui/console.go b/ui/console.go index 30b5f19..840c45e 100644 --- a/ui/console.go +++ b/ui/console.go @@ -20,10 +20,10 @@ func (ui *UI) log(message string, isError bool) error { } else { message = decorate(message, "green") } - return ui.writeContent(LOG_PANEL, message) + return ui.writeContent(logPanel, message) } // clearLog clears the log message. func (ui *UI) clearLog() error { - return ui.writeContent(LOG_PANEL, "") + return ui.writeContent(logPanel, "") } diff --git a/ui/editor.go b/ui/editor.go index fec0545..8b224fc 100644 --- a/ui/editor.go +++ b/ui/editor.go @@ -76,7 +76,7 @@ func (e *editor) Edit(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) maxY := strings.Count(v.ViewBuffer(), "\n") - 1 v.SetCursor(maxX, maxY) case gocui.KeyCtrlX: - if v.Name() == DIAGRAM_PANEL { + if v.Name() == editorPanel { cache = []byte(v.ViewBuffer()) e.ui.ClearView(v.Name()) v.SetCursor(0, 0) diff --git a/ui/handlers.go b/ui/handlers.go index 9937c24..e36aefe 100644 --- a/ui/handlers.go +++ b/ui/handlers.go @@ -3,10 +3,10 @@ package ui import ( "bytes" "fmt" - "log" "path/filepath" "runtime" "text/tabwriter" + "time" "github.com/esimov/diagram/io" "github.com/jroimartin/gocui" @@ -35,8 +35,8 @@ var keyHandlers = &handlers{ *getDeleteHandler(), {nil, gocui.KeyCtrlX, "Ctrl+x", "Clear editor content", nil}, {nil, gocui.KeyCtrlZ, "Ctrl+z", "Restore editor content", nil}, - {nil, gocui.KeyCtrlS, "Ctrl+s", "Save diagram", onSaveDiagram}, - {nil, gocui.KeyCtrlD, "Ctrl+d", "Draw diagram", onDrawDiagram}, + {nil, gocui.KeyCtrlS, "Ctrl+s", "Save diagram", onDiagramSave}, + {nil, gocui.KeyCtrlG, "Ctrl+p", "Generate diagram", onDiagramGenerate}, {nil, gocui.KeyCtrlQ, "Ctrl+q", "Quit", onQuit}, } @@ -69,17 +69,17 @@ func onQuit(ui *UI, wrap bool) Fn { } } -// onSaveDiagram is an event listener which get triggered when a save action is performed. -func onSaveDiagram(ui *UI, wrap bool) Fn { +// onDiagramSave is an event listener which get triggered when a save action is performed. +func onDiagramSave(ui *UI, wrap bool) Fn { return func(*gocui.Gui, *gocui.View) error { - return ui.saveDiagram(DIAGRAM_PANEL) + return ui.saveDiagram(editorPanel) } } -// onDrawDiagram is an event listener which get triggered when a draw action is performed. -func onDrawDiagram(ui *UI, wrap bool) Fn { +// onDiagramGenerate is an event listener which get triggered when a draw action is performed. +func onDiagramGenerate(ui *UI, wrap bool) Fn { return func(*gocui.Gui, *gocui.View) error { - return ui.drawDiagram(DIAGRAM_PANEL) + return ui.generateDiagram(editorPanel) } } @@ -105,7 +105,7 @@ func (handlers handlers) ApplyKeyBindings(ui *UI, g *gocui.Gui) error { if cy < ui.getViewTotalRows(v)-1 { v.SetCursor(cx, cy+1) } - ui.modifyView(DIAGRAM_PANEL) + ui.modifyView(editorPanel) return nil } onUp := func(g *gocui.Gui, v *gocui.View) error { @@ -114,52 +114,68 @@ func (handlers handlers) ApplyKeyBindings(ui *UI, g *gocui.Gui) error { if cy > 0 { v.SetCursor(cx, cy-1) } - ui.modifyView(DIAGRAM_PANEL) + ui.modifyView(editorPanel) return nil } onDelete := func(g *gocui.Gui, v *gocui.View) error { + if ui.logTimer != nil { + ui.logTimer.Stop() + } + cx, cy := v.Cursor() - cv, err := ui.gui.View(SAVED_DIAGRAMS_PANEL) + cv, err := ui.gui.View(savedDiagramsPanel) if err != nil { return err } cwd, err := filepath.Abs(filepath.Dir("")) if err != nil { - log.Fatal(err) + return err } currentFile = ui.getViewRow(cv, cy)[0] - fn := cwd + "/" + DIAGRAMS_DIR + "/" + currentFile + fn := fmt.Sprintf("%s/%s/%s", cwd, mainDir, currentFile) - io.DeleteDiagram(fn) - ui.updateDiagramList(SAVED_DIAGRAMS_PANEL) + err = io.DeleteDiagram(fn) + if err != nil { + return err + } + ui.log(fmt.Sprintf("The file %s has been deleted successfully from the %s directory", currentFile, cwd), false) + ui.updateDiagramList(savedDiagramsPanel) if cy > 0 { v.SetCursor(cx, cy-1) } - ui.modifyView(DIAGRAM_PANEL) + ui.modifyView(editorPanel) + + // Hide log message after 4 seconds + ui.logTimer = time.AfterFunc(4*time.Second, func() { + ui.gui.Update(func(*gocui.Gui) error { + return ui.clearLog() + }) + }) + return nil } - if err := g.SetKeybinding(SAVED_DIAGRAMS_PANEL, gocui.KeyArrowDown, gocui.ModNone, onDown); err != nil { + if err := g.SetKeybinding(savedDiagramsPanel, gocui.KeyArrowDown, gocui.ModNone, onDown); err != nil { return err } - if err := g.SetKeybinding(SAVED_DIAGRAMS_PANEL, gocui.KeyArrowUp, gocui.ModNone, onUp); err != nil { + if err := g.SetKeybinding(savedDiagramsPanel, gocui.KeyArrowUp, gocui.ModNone, onUp); err != nil { return err } if runtime.GOOS == "darwin" { - if err := g.SetKeybinding(SAVED_DIAGRAMS_PANEL, gocui.KeyBackspace2, gocui.ModNone, onDelete); err != nil { + if err := g.SetKeybinding(savedDiagramsPanel, gocui.KeyBackspace2, gocui.ModNone, onDelete); err != nil { return err } } else { - if err := g.SetKeybinding(SAVED_DIAGRAMS_PANEL, gocui.KeyDelete, gocui.ModNone, onDelete); err != nil { + if err := g.SetKeybinding(savedDiagramsPanel, gocui.KeyDelete, gocui.ModNone, onDelete); err != nil { return err } } return g.SetKeybinding("", gocui.KeyCtrlH, gocui.ModNone, func(g *gocui.Gui, v *gocui.View) error { - return ui.toggleHelp(g, handlers.HelpContent()) + return ui.toggleHelp(handlers.HelpContent()) }) } diff --git a/ui/panels.go b/ui/panels.go index 9796ff2..a060d8a 100644 --- a/ui/panels.go +++ b/ui/panels.go @@ -3,7 +3,6 @@ package ui import ( "fmt" "image" - "log" "math" "os" "path/filepath" @@ -30,105 +29,37 @@ type panelProperties struct { editor *UI } -// MinGUIWindow is the minimum window size -const MinGUIWindow = 600 - const ( - // Panel constants - LOGO_PANEL = "logo" - SAVED_DIAGRAMS_PANEL = "saved_diagrams" - LOG_PANEL = "log" - DIAGRAM_PANEL = "diagram" - PROGRESS_PANEL = "progress" - HELP_PANEL = "help" - SAVE_MODAL = "save_modal" - PROGRESS_MODAL = "progress_modal" + // Main panels + logoPanel = "logo" + savedDiagramsPanel = "saved_diagrams" + logPanel = "log" + editorPanel = "diagram" + + // Modals + helpModal = "help" + saveModal = "save_modal" + progressModal = "progress_modal" // Log messages - ERROR_EMPTY = "The editor should not be empty!" - DIAGRAMS_DIR = "/diagrams" + logErrorEmpty = "The editor should not be empty!" + + mainDir = "/diagrams" ) // Main views -var panelViews = map[string]panelProperties{ - LOGO_PANEL: { - title: " Diagram ", - text: version.DrawLogo(), - x1: 0.0, - y1: 0.0, - x2: 0.4, - y2: 0.25, - editable: true, - cursor: false, - }, - SAVED_DIAGRAMS_PANEL: { - title: " Saved Diagrams ", - text: "", - x1: 0.0, - y1: 0.25, - x2: 0.4, - y2: 0.90, - editable: true, - cursor: false, - }, - LOG_PANEL: { - title: " Console ", - text: "", - x1: 0.0, - y1: 0.90, - x2: 0.4, - y2: 1.0, - editable: true, - cursor: false, - }, - DIAGRAM_PANEL: { - title: " Editor ", - text: string(io.ReadFile("sample.txt")), - x1: 0.4, - y1: 0.0, - x2: 1.0, - y2: 1.0, - editable: true, - cursor: true, - }, - PROGRESS_PANEL: { - title: " Progress ", - text: "", - x1: 0.0, - y1: 0.7, - x2: 1, - y2: 0.8, - editable: false, - cursor: false, - }, -} +var panelViews map[string]panelProperties // Modal views -var modalViews = map[string]panelProperties{ - HELP_PANEL: { - title: "Key Shortcuts", - text: "", - editable: false, - }, - SAVE_MODAL: { - title: "Save diagram", - text: ".txt", - editable: true, - }, - PROGRESS_MODAL: { - title: "", - text: " Generating...", - editable: false, - }, -} +var modalViews map[string]panelProperties var ( // Panel Views mainViews = []string{ - LOGO_PANEL, - SAVED_DIAGRAMS_PANEL, - LOG_PANEL, - DIAGRAM_PANEL, + logoPanel, + savedDiagramsPanel, + logPanel, + editorPanel, } modalElements = []string{"save_modal", "save", "cancel"} currentFile string @@ -136,6 +67,72 @@ var ( // Layout initialize the panel views and associates the key bindings to them. func (ui *UI) Layout(g *gocui.Gui) error { + defaultContent, err := io.ReadFile("sample.txt") + if err != nil { + fmt.Errorf("error reading the sample file: %w", err) + } + + panelViews = map[string]panelProperties{ + logoPanel: { + title: " Info ", + text: version.DrawLogo(), + x1: 0.0, + y1: 0.0, + x2: 0.4, + y2: 0.20, + editable: true, + cursor: false, + }, + savedDiagramsPanel: { + title: " Saved Diagrams ", + text: "", + x1: 0.0, + y1: 0.20, + x2: 0.4, + y2: 0.85, + editable: true, + cursor: false, + }, + logPanel: { + title: " Console ", + text: "", + x1: 0.0, + y1: 0.85, + x2: 0.4, + y2: 1.0, + editable: true, + cursor: false, + }, + editorPanel: { + title: " Editor ", + text: string(defaultContent), + x1: 0.4, + y1: 0.0, + x2: 1.0, + y2: 1.0, + editable: true, + cursor: true, + }, + } + + modalViews = map[string]panelProperties{ + helpModal: { + title: "Key Shortcuts", + text: "", + editable: false, + }, + saveModal: { + title: "Save diagram", + text: ".txt", + editable: true, + }, + progressModal: { + title: "", + text: " Generating diagram...", + editable: false, + }, + } + initPanel := func(g *gocui.Gui, v *gocui.View) error { // Disable panel views selection with mouse in case the modal is activated if ui.currentModal == "" { @@ -158,8 +155,8 @@ func (ui *UI) Layout(g *gocui.Gui) error { // Refresh the diagram panel with the new diagram content cv := ui.gui.CurrentView() - if cv.Name() == SAVED_DIAGRAMS_PANEL && len(cv.ViewBuffer()) > 0 { - ui.modifyView(DIAGRAM_PANEL) + if cv.Name() == savedDiagramsPanel && len(cv.ViewBuffer()) > 0 { + ui.modifyView(editorPanel) } return nil } @@ -179,12 +176,12 @@ func (ui *UI) Layout(g *gocui.Gui) error { // Activate the first panel on first run if v := ui.gui.CurrentView(); v == nil { - _, err := ui.gui.SetCurrentView(DIAGRAM_PANEL) + _, err := ui.gui.SetCurrentView(editorPanel) if err != gocui.ErrUnknownView { return err } } - return g.SetKeybinding(DIAGRAM_PANEL, gocui.MouseWheelDown, gocui.ModNone, ui.scrollDown) + return g.SetKeybinding(editorPanel, gocui.MouseWheelDown, gocui.ModNone, ui.scrollDown) } // scrollDown moves the cursor to the next buffer line. @@ -197,12 +194,12 @@ func (ui *UI) scrollDown(g *gocui.Gui, v *gocui.View) error { } // toggleHelp toggle the help view on key pressing. -func (ui *UI) toggleHelp(g *gocui.Gui, content string) error { +func (ui *UI) toggleHelp(content string) error { if err := ui.closeOpenedModals(modalElements); err != nil { return err } panelHeight := strings.Count(content, "\n") - if ui.currentModal == HELP_PANEL { + if ui.currentModal == helpModal { ui.gui.DeleteKeybinding("", gocui.MouseLeft, gocui.ModNone) ui.gui.DeleteKeybinding("", gocui.MouseRelease, gocui.ModNone) @@ -213,7 +210,7 @@ func (ui *UI) toggleHelp(g *gocui.Gui, content string) error { } return ui.closeModal(ui.currentModal) } - v, err := ui.openModal(HELP_PANEL, 45, panelHeight, true) + v, err := ui.openModal(helpModal, 45, panelHeight, true) if err != nil { return err } @@ -306,11 +303,11 @@ func (ui *UI) createPanelView(name string, x1, y1, x2, y2 int) (*gocui.View, err } switch name { - case DIAGRAM_PANEL: + case editorPanel: v.Highlight = false v.Autoscroll = true v.Editor = newEditor(ui, nil) - case SAVED_DIAGRAMS_PANEL: + case savedDiagramsPanel: v.Highlight = true v.SelBgColor = gocui.ColorGreen v.SelFgColor = gocui.ColorBlack @@ -406,31 +403,30 @@ func (ui *UI) saveDiagram(name string) error { return err } - // Reset log timer firing in case of new incoming message. - if ui.logTimer != nil { - ui.logTimer.Stop() - } - if len(v.ViewBuffer()) == 0 { - ui.consoleLog = ERROR_EMPTY + ui.consoleLog = logErrorEmpty if err := ui.log(ui.consoleLog, true); err != nil { return err } } - return ui.showSaveModal(SAVE_MODAL) + return ui.showSaveModal(saveModal) } -// drawDiagram converts the ASCII to the hand-drawn diagram. -func (ui *UI) drawDiagram(name string) error { +// generateDiagram converts the ASCII to the hand-drawn diagram. +func (ui *UI) generateDiagram(name string) error { var output string + if ui.logTimer != nil { + ui.logTimer.Stop() + } + v, err := ui.gui.View(name) if err != nil { return err } if len(v.ViewBuffer()) == 0 { - ui.consoleLog = ERROR_EMPTY + ui.consoleLog = logErrorEmpty if err := ui.log(ui.consoleLog, true); err != nil { return err } @@ -444,11 +440,11 @@ func (ui *UI) drawDiagram(name string) error { } // Show progress - ui.showProgressModal(PROGRESS_MODAL) + ui.showProgressModal(progressModal) cwd, err := filepath.Abs(filepath.Dir("")) if err != nil { - log.Fatal(err) + return err } filePath := cwd + "/output/" @@ -461,7 +457,7 @@ func (ui *UI) drawDiagram(name string) error { // Generate the hand-drawn diagram. err = canvas.DrawDiagram(v.Buffer(), filePath+output, ui.fontPath) if err == nil { - ui.log(fmt.Sprintf("Successfully converted the ascii diagram into %s!", output), false) + ui.log(fmt.Sprintf("Successfully converted the ascii diagram into %s.", output), false) } else { ui.log(fmt.Sprintf("Error saving the ascii diagram: %v", err), true) } @@ -470,7 +466,7 @@ func (ui *UI) drawDiagram(name string) error { ui.modalTimer = time.AfterFunc(1*time.Second, func() { ui.gui.Update(func(*gocui.Gui) error { ui.nextItem = 0 // reset modal elements counter to 0 - if err := ui.closeModal(PROGRESS_MODAL); err != nil { + if err := ui.closeModal(progressModal); err != nil { return err } @@ -491,7 +487,7 @@ func (ui *UI) drawDiagram(name string) error { return fmt.Errorf("failed to decode the image '%s': %w", diagram, err) } - gui := gui.NewGUI(source, "ASCII diagram preview") + gui := gui.NewGUI(source, "Diagram preview") go func() error { if err := gui.Draw(); err != nil { return fmt.Errorf("error drawing the diagram: %w", err) @@ -542,7 +538,10 @@ func (ui *UI) showSaveModal(name string) error { // Save event handler onSave := func(*gocui.Gui, *gocui.View) error { - diagram, _ := ui.gui.View(DIAGRAM_PANEL) + if ui.logTimer != nil { + ui.logTimer.Stop() + } + diagram, _ := ui.gui.View(editorPanel) v := modalViews[name] ui.nextItem = 0 // reset modal elements counter to 0 @@ -553,7 +552,7 @@ func (ui *UI) showSaveModal(name string) error { res := re.MatchString(buffer) if len(diagram.ViewBuffer()) == 0 { - ui.log("The diagram is empty!", true) + ui.log("Missing content on diagram save!", true) return nil } @@ -561,13 +560,14 @@ func (ui *UI) showSaveModal(name string) error { ui.log("File name should not be empty!", true) } else if res { file := buffer + v.text - _, err := io.SaveFile(file, DIAGRAMS_DIR, diagram.ViewBuffer()) + f, err := io.SaveFile(file, mainDir, diagram.ViewBuffer()) if err != nil { return err } + defer f.Close() ui.log(fmt.Sprintf("The file has been saved as: %s", file), false) } else { - ui.log("Wrong file name! The file name should contain only letters, numbers and underscores!", true) + ui.log("Error saving the diagram. The file name should contain only letters, numbers and underscores!", true) } if err := ui.closeOpenedModals(modalElements); err != nil { @@ -575,7 +575,7 @@ func (ui *UI) showSaveModal(name string) error { } // Update diagrams directory list - ui.updateDiagramList(SAVED_DIAGRAMS_PANEL) + ui.updateDiagramList(savedDiagramsPanel) return nil } @@ -642,8 +642,7 @@ func (ui *UI) showSaveModal(name string) error { // Hide log message after 4 seconds ui.logTimer = time.AfterFunc(4*time.Second, func() { ui.gui.Update(func(*gocui.Gui) error { - ui.clearLog() - return nil + return ui.clearLog() }) }) @@ -687,17 +686,24 @@ func (ui *UI) modifyView(name string) error { return err } if v != nil { - cv, err := ui.gui.View(SAVED_DIAGRAMS_PANEL) + cv, err := ui.gui.View(savedDiagramsPanel) if err != nil { return err } _, cy := cv.Cursor() cwd, err := filepath.Abs(filepath.Dir("")) if err != nil { - log.Fatal(err) + return err } + currentFile = ui.getViewRow(cv, cy)[0] - buffer := string(io.ReadFile(cwd + "/" + DIAGRAMS_DIR + "/" + currentFile)) + filePath := fmt.Sprintf("%s/%s/%s", cwd, mainDir, currentFile) + + content, err := io.ReadFile(filePath) + if err != nil { + return err + } + buffer := string(content) if err := ui.updateView(v, buffer); err != nil { return err @@ -713,7 +719,7 @@ func (ui *UI) updateDiagramList(name string) error { return err } v.Clear() - diagrams, _ := io.ListDiagrams(DIAGRAMS_DIR) + diagrams, _ := io.ListDiagrams(mainDir) for idx, diagram := range diagrams { if idx < len(diagrams)-1 { diff --git a/ui/ui.go b/ui/ui.go index 881f4e0..872de79 100644 --- a/ui/ui.go +++ b/ui/ui.go @@ -94,7 +94,7 @@ func (ui *UI) initGui(g *gocui.Gui) error { ui.gui.Cursor = true ui.gui.Mouse = true - ui.currentView = ui.findViewByName(DIAGRAM_PANEL) + ui.currentView = ui.findViewByName(editorPanel) ui.nextItem = 0 // Set Layout function diff --git a/version/version.go b/version/version.go index de2572b..d7863a6 100644 --- a/version/version.go +++ b/version/version.go @@ -6,7 +6,7 @@ import "github.com/esimov/diagram/color" const Name = "diagram" // Description of application. -const Description = "Transform ASCII arts into hand drawn diagrams" +const Description = " ...transforms your ASCII arts into hand drawn diagrams!" // Version number. const Version = "v1.0.4" @@ -15,13 +15,17 @@ const Version = "v1.0.4" func DrawLogo() string { var logo string + c := color.Random(180, 231) + logo += "\n\n" - logo += color.StringRandom(" ██████╗ ██╗ █████╗ ██████╗ ██████╗ █████╗ ███╗ ███╗\n") - logo += color.StringRandom(" ██╔══██╗██║██╔══██╗██╔════╝ ██╔══██╗██╔══██╗████╗ ████║\n") - logo += color.StringRandom(" ██║ ██║██║███████║██║ ███╗██████╔╝███████║██╔████╔██║\n") - logo += color.StringRandom(" ██║ ██║██║██╔══██║██║ ██║██╔══██╗██╔══██║██║╚██╔╝██║\n") - logo += color.StringRandom(" ██████╔╝██║██║ ██║╚██████╔╝██║ ██║██║ ██║██║ ╚═╝ ██║\n") - logo += color.StringRandom(" ╚═════╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ " + Version) + logo += color.String(c, " ██████╗ ██╗ █████╗ ██████╗ ██████╗ █████╗ ███╗ ███╗\n") + logo += color.String(c, " ██╔══██╗██║██╔══██╗██╔════╝ ██╔══██╗██╔══██╗████╗ ████║\n") + logo += color.String(c, " ██║ ██║██║███████║██║ ███╗██████╔╝███████║██╔████╔██║\n") + logo += color.String(c, " ██║ ██║██║██╔══██║██║ ██║██╔══██╗██╔══██║██║╚██╔╝██║\n") + logo += color.String(c, " ██████╔╝██║██║ ██║╚██████╔╝██║ ██║██║ ██║██║ ╚═╝ ██║\n") + logo += color.String(c, " ╚═════╝ ╚═╝╚═╝ ╚═╝ ╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝╚═╝ ╚═╝ "+Version) + logo += "\n\n\n\n" + logo += color.String(c, Description) return logo }