go fmt sources & add command-line file loading
This commit is contained in:
parent
d0bb43cc8f
commit
443070f077
21
main.go
21
main.go
@ -2,9 +2,9 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io/fs"
|
||||||
"io/ioutil"
|
"io/ioutil"
|
||||||
"os"
|
"os"
|
||||||
"io/fs"
|
|
||||||
|
|
||||||
"github.com/fivemoreminix/diesel/ui"
|
"github.com/fivemoreminix/diesel/ui"
|
||||||
"github.com/gdamore/tcell/v2"
|
"github.com/gdamore/tcell/v2"
|
||||||
@ -47,6 +47,25 @@ func main() {
|
|||||||
tabContainer.SetPos(0, 1)
|
tabContainer.SetPos(0, 1)
|
||||||
tabContainer.SetSize(sizex, sizey-1)
|
tabContainer.SetSize(sizex, sizey-1)
|
||||||
|
|
||||||
|
// Load files from command-line arguments
|
||||||
|
if len(os.Args) > 1 {
|
||||||
|
for _, path := range os.Args[1:] {
|
||||||
|
file, err := os.Open(path)
|
||||||
|
if err != nil {
|
||||||
|
panic("File could not be opened at path " + path)
|
||||||
|
}
|
||||||
|
defer file.Close()
|
||||||
|
|
||||||
|
bytes, err := ioutil.ReadAll(file)
|
||||||
|
if err != nil {
|
||||||
|
panic("Could not read all of " + path)
|
||||||
|
}
|
||||||
|
|
||||||
|
textEdit := ui.NewTextEdit(&s, path, string(bytes), &theme)
|
||||||
|
tabContainer.AddTab(path, textEdit)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
var fileSelector *ui.FileSelectorDialog // if nil, we don't draw it
|
var fileSelector *ui.FileSelectorDialog // if nil, we don't draw it
|
||||||
|
|
||||||
bar := ui.NewMenuBar(nil, &theme)
|
bar := ui.NewMenuBar(nil, &theme)
|
||||||
|
10
ui/button.go
10
ui/button.go
@ -7,21 +7,21 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
type Button struct {
|
type Button struct {
|
||||||
Text string
|
Text string
|
||||||
Callback func()
|
Callback func()
|
||||||
|
|
||||||
x, y int
|
x, y int
|
||||||
width, height int
|
width, height int
|
||||||
focused bool
|
focused bool
|
||||||
|
|
||||||
Theme *Theme
|
Theme *Theme
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewButton(text string, theme *Theme, callback func()) *Button {
|
func NewButton(text string, theme *Theme, callback func()) *Button {
|
||||||
return &Button{
|
return &Button{
|
||||||
Text: text,
|
Text: text,
|
||||||
Callback: callback,
|
Callback: callback,
|
||||||
Theme: theme,
|
Theme: theme,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -160,7 +160,7 @@ func (c *TabContainer) Draw(s tcell.Screen) {
|
|||||||
for _, tab := range c.children {
|
for _, tab := range c.children {
|
||||||
combinedTabLength += len(tab.Name) + 2 // 2 for padding
|
combinedTabLength += len(tab.Name) + 2 // 2 for padding
|
||||||
}
|
}
|
||||||
combinedTabLength += len(c.children)-1 // add for spacing between tabs
|
combinedTabLength += len(c.children) - 1 // add for spacing between tabs
|
||||||
|
|
||||||
// Draw tabs
|
// Draw tabs
|
||||||
col := c.x + c.width/2 - combinedTabLength/2 // Starting column
|
col := c.x + c.width/2 - combinedTabLength/2 // Starting column
|
||||||
@ -239,10 +239,10 @@ func (c *TabContainer) HandleEvent(event tcell.Event) bool {
|
|||||||
if c.Selected >= len(c.children) {
|
if c.Selected >= len(c.children) {
|
||||||
c.Selected = 0
|
c.Selected = 0
|
||||||
}
|
}
|
||||||
} else if ev.Modifiers() == tcell.ModCtrl & tcell.ModShift { // Ctrl + Shift + Tab was pressed
|
} else if ev.Modifiers() == tcell.ModCtrl&tcell.ModShift { // Ctrl + Shift + Tab was pressed
|
||||||
c.Selected--
|
c.Selected--
|
||||||
if c.Selected < 0 {
|
if c.Selected < 0 {
|
||||||
c.Selected = len(c.children)-1
|
c.Selected = len(c.children) - 1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -281,7 +281,7 @@ func (w *WindowContainer) Draw(s tcell.Screen) {
|
|||||||
headerStyle := w.Theme.GetOrDefault("WindowHeader")
|
headerStyle := w.Theme.GetOrDefault("WindowHeader")
|
||||||
|
|
||||||
DrawRect(s, w.x, w.y, w.width, 1, ' ', headerStyle) // Draw header
|
DrawRect(s, w.x, w.y, w.width, 1, ' ', headerStyle) // Draw header
|
||||||
DrawStr(s, w.x+w.width/2 - len(w.Title)/2, w.y, w.Title, headerStyle) // Draw title
|
DrawStr(s, w.x+w.width/2-len(w.Title)/2, w.y, w.Title, headerStyle) // Draw title
|
||||||
DrawRect(s, w.x, w.y+1, w.width, w.height-1, ' ', w.Theme.GetOrDefault("Window")) // Draw body background
|
DrawRect(s, w.x, w.y+1, w.width, w.height-1, ' ', w.Theme.GetOrDefault("Window")) // Draw body background
|
||||||
|
|
||||||
if w.Child != nil {
|
if w.Child != nil {
|
||||||
|
@ -11,15 +11,15 @@ import (
|
|||||||
type FileSelectorDialog struct {
|
type FileSelectorDialog struct {
|
||||||
MustExist bool // Whether the dialog should have a user select an existing file.
|
MustExist bool // Whether the dialog should have a user select an existing file.
|
||||||
FilesChosenCallback func([]string) // Returns slice of filenames selected. nil if user canceled.
|
FilesChosenCallback func([]string) // Returns slice of filenames selected. nil if user canceled.
|
||||||
CancelCallback func() // Called when the dialog has been canceled by the user
|
CancelCallback func() // Called when the dialog has been canceled by the user
|
||||||
|
|
||||||
container *WindowContainer
|
container *WindowContainer
|
||||||
x, y int
|
x, y int
|
||||||
width, height int
|
width, height int
|
||||||
focused bool
|
focused bool
|
||||||
|
|
||||||
tabOrder []Component
|
tabOrder []Component
|
||||||
tabOrderIdx int
|
tabOrderIdx int
|
||||||
|
|
||||||
inputField *InputField
|
inputField *InputField
|
||||||
confirmButton *Button
|
confirmButton *Button
|
||||||
@ -87,7 +87,7 @@ func (d *FileSelectorDialog) GetPos() (int, int) {
|
|||||||
func (d *FileSelectorDialog) SetPos(x, y int) {
|
func (d *FileSelectorDialog) SetPos(x, y int) {
|
||||||
d.x, d.y = x, y
|
d.x, d.y = x, y
|
||||||
d.container.SetPos(x, y)
|
d.container.SetPos(x, y)
|
||||||
d.inputField.SetPos(d.x+1, d.y+2) // Center input field
|
d.inputField.SetPos(d.x+1, d.y+2) // Center input field
|
||||||
d.cancelButton.SetPos(d.x+1, d.y+4) // Place "Cancel" button on left, bottom
|
d.cancelButton.SetPos(d.x+1, d.y+4) // Place "Cancel" button on left, bottom
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -18,9 +18,9 @@ type InputField struct {
|
|||||||
|
|
||||||
func NewInputField(screen *tcell.Screen, placeholder string, theme *Theme) *InputField {
|
func NewInputField(screen *tcell.Screen, placeholder string, theme *Theme) *InputField {
|
||||||
return &InputField{
|
return &InputField{
|
||||||
Text: placeholder,
|
Text: placeholder,
|
||||||
screen: screen,
|
screen: screen,
|
||||||
Theme: theme,
|
Theme: theme,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -40,7 +40,7 @@ func (f *InputField) SetCursorPos(offset int) {
|
|||||||
|
|
||||||
// Scrolling
|
// Scrolling
|
||||||
if offset >= f.scrollPos+f.width-2 { // If cursor position is out of view to the right...
|
if offset >= f.scrollPos+f.width-2 { // If cursor position is out of view to the right...
|
||||||
f.scrollPos = offset - f.width+2 // Scroll just enough to view that column
|
f.scrollPos = offset - f.width + 2 // Scroll just enough to view that column
|
||||||
} else if offset < f.scrollPos { // If cursor position is out of view to the left...
|
} else if offset < f.scrollPos { // If cursor position is out of view to the left...
|
||||||
f.scrollPos = offset
|
f.scrollPos = offset
|
||||||
}
|
}
|
||||||
@ -53,7 +53,7 @@ func (f *InputField) SetCursorPos(offset int) {
|
|||||||
|
|
||||||
func (f *InputField) Delete(forward bool) {
|
func (f *InputField) Delete(forward bool) {
|
||||||
if forward {
|
if forward {
|
||||||
//if f.cursorPos
|
//if f.cursorPos
|
||||||
} else {
|
} else {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -178,8 +178,8 @@ func (b *MenuBar) HandleEvent(event tcell.Event) bool {
|
|||||||
|
|
||||||
// A Menu contains one or more ItemEntry or ItemMenus.
|
// A Menu contains one or more ItemEntry or ItemMenus.
|
||||||
type Menu struct {
|
type Menu struct {
|
||||||
Name string
|
Name string
|
||||||
Items []Item
|
Items []Item
|
||||||
|
|
||||||
x, y int
|
x, y int
|
||||||
width, height int // Size may not be settable
|
width, height int // Size may not be settable
|
||||||
@ -205,7 +205,7 @@ func NewMenu(name string, theme *Theme, items []Item) Menu {
|
|||||||
func (m *Menu) Draw(s tcell.Screen) {
|
func (m *Menu) Draw(s tcell.Screen) {
|
||||||
defaultStyle := m.Theme.GetOrDefault("Menu")
|
defaultStyle := m.Theme.GetOrDefault("Menu")
|
||||||
|
|
||||||
m.GetSize() // Call this to update internal width and height
|
m.GetSize() // Call this to update internal width and height
|
||||||
DrawRect(s, m.x, m.y, m.width, m.height, ' ', defaultStyle) // Fill background
|
DrawRect(s, m.x, m.y, m.width, m.height, ' ', defaultStyle) // Fill background
|
||||||
DrawRectOutlineDefault(s, m.x, m.y, m.width, m.height, defaultStyle) // Draw outline
|
DrawRectOutlineDefault(s, m.x, m.y, m.width, m.height, defaultStyle) // Draw outline
|
||||||
|
|
||||||
|
@ -13,11 +13,11 @@ import (
|
|||||||
// tools, is autocomplete ready, and contains the various information about
|
// tools, is autocomplete ready, and contains the various information about
|
||||||
// content being edited.
|
// content being edited.
|
||||||
type TextEdit struct {
|
type TextEdit struct {
|
||||||
LineNumbers bool // Whether to render line numbers (and therefore the column)
|
LineNumbers bool // Whether to render line numbers (and therefore the column)
|
||||||
Dirty bool // Whether the buffer has been edited
|
Dirty bool // Whether the buffer has been edited
|
||||||
UseHardTabs bool // When true, tabs are '\t'
|
UseHardTabs bool // When true, tabs are '\t'
|
||||||
TabSize int // How many spaces to indent by
|
TabSize int // How many spaces to indent by
|
||||||
IsCRLF bool // Whether the file's line endings are CRLF (\r\n) or LF (\n)
|
IsCRLF bool // Whether the file's line endings are CRLF (\r\n) or LF (\n)
|
||||||
FilePath string // Will be empty if the file has not been saved yet
|
FilePath string // Will be empty if the file has not been saved yet
|
||||||
|
|
||||||
buffer []string // TODO: replace line-based buffer with gap buffer
|
buffer []string // TODO: replace line-based buffer with gap buffer
|
||||||
|
@ -19,7 +19,7 @@ func (theme *Theme) GetOrDefault(key string) tcell.Style {
|
|||||||
return val
|
return val
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if val, ok := DefaultTheme[key]; ok {
|
if val, ok := DefaultTheme[key]; ok {
|
||||||
return val
|
return val
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user