mirror of
https://github.com/RWejlgaard/org.git
synced 2026-05-06 04:34:45 +00:00
feat: Add capture flag -c/--capture with pipe support
This commit is contained in:
parent
8ff2b254a4
commit
e7b057ff75
2 changed files with 53 additions and 8 deletions
|
|
@ -1,8 +1,10 @@
|
||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"bufio"
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
||||||
|
|
@ -15,14 +17,44 @@ import (
|
||||||
func main() {
|
func main() {
|
||||||
var filePath string
|
var filePath string
|
||||||
var multiMode bool
|
var multiMode bool
|
||||||
|
var captureMode bool
|
||||||
flag.BoolVar(&multiMode, "multi", false, "Load all org files in current directory as top-level items")
|
flag.BoolVar(&multiMode, "multi", false, "Load all org files in current directory as top-level items")
|
||||||
flag.BoolVar(&multiMode, "m", false, "Load all org files in current directory (shorthand)")
|
flag.BoolVar(&multiMode, "m", false, "Load all org files in current directory (shorthand)")
|
||||||
|
flag.BoolVar(&captureMode, "capture", false, "Start in capture mode")
|
||||||
|
flag.BoolVar(&captureMode, "c", false, "Start in capture mode (shorthand)")
|
||||||
flag.Parse()
|
flag.Parse()
|
||||||
|
|
||||||
// Check for positional argument
|
// Check for positional argument or capture text
|
||||||
|
var captureText string
|
||||||
|
if len(flag.Args()) > 0 {
|
||||||
|
if captureMode {
|
||||||
|
// First argument is capture text when in capture mode
|
||||||
|
captureText = flag.Args()[0]
|
||||||
|
// Second argument (if present) is the file path
|
||||||
|
if len(flag.Args()) > 1 {
|
||||||
|
filePath = flag.Args()[1]
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
// First argument is file path in normal mode
|
||||||
|
filePath = flag.Args()[0]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Check if input is being piped
|
||||||
|
stat, _ := os.Stdin.Stat()
|
||||||
|
if (stat.Mode() & os.ModeCharDevice) == 0 {
|
||||||
|
// Data is being piped to stdin
|
||||||
|
reader := bufio.NewReader(os.Stdin)
|
||||||
|
pipedText, err := io.ReadAll(reader)
|
||||||
|
if err == nil && len(pipedText) > 0 {
|
||||||
|
captureMode = true
|
||||||
|
captureText = string(pipedText)
|
||||||
|
// If no file path was provided via args, check if last arg could be a path
|
||||||
if filePath == "" && len(flag.Args()) > 0 {
|
if filePath == "" && len(flag.Args()) > 0 {
|
||||||
filePath = flag.Args()[0]
|
filePath = flag.Args()[0]
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Load configuration
|
// Load configuration
|
||||||
cfg, err := config.LoadConfig()
|
cfg, err := config.LoadConfig()
|
||||||
|
|
@ -81,7 +113,7 @@ func main() {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Run the UI
|
// Run the UI
|
||||||
if err := ui.RunUI(orgFile, cfg); err != nil {
|
if err := ui.RunUI(orgFile, cfg, captureMode, captureText); err != nil {
|
||||||
fmt.Fprintf(os.Stderr, "Error running UI: %v\n", err)
|
fmt.Fprintf(os.Stderr, "Error running UI: %v\n", err)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -55,7 +55,7 @@ type uiModel struct {
|
||||||
captureCursor int // Store cursor position when entering capture mode
|
captureCursor int // Store cursor position when entering capture mode
|
||||||
}
|
}
|
||||||
|
|
||||||
func InitialModel(orgFile *model.OrgFile, cfg *config.Config) uiModel {
|
func InitialModel(orgFile *model.OrgFile, cfg *config.Config, captureMode bool, captureText string) uiModel {
|
||||||
ta := textarea.New()
|
ta := textarea.New()
|
||||||
ta.Placeholder = "Enter notes here (code blocks supported)..."
|
ta.Placeholder = "Enter notes here (code blocks supported)..."
|
||||||
ta.ShowLineNumbers = false
|
ta.ShowLineNumbers = false
|
||||||
|
|
@ -67,10 +67,16 @@ func InitialModel(orgFile *model.OrgFile, cfg *config.Config) uiModel {
|
||||||
h := help.New()
|
h := help.New()
|
||||||
h.ShowAll = false
|
h.ShowAll = false
|
||||||
|
|
||||||
|
mode := modeList
|
||||||
|
if captureMode {
|
||||||
|
mode = modeCapture
|
||||||
|
ti.SetValue(strings.TrimSpace(captureText))
|
||||||
|
}
|
||||||
|
|
||||||
return uiModel{
|
return uiModel{
|
||||||
orgFile: orgFile,
|
orgFile: orgFile,
|
||||||
cursor: 0,
|
cursor: 0,
|
||||||
mode: modeList,
|
mode: mode,
|
||||||
help: h,
|
help: h,
|
||||||
keys: newKeyMapFromConfig(cfg),
|
keys: newKeyMapFromConfig(cfg),
|
||||||
styles: newStyleMapFromConfig(cfg),
|
styles: newStyleMapFromConfig(cfg),
|
||||||
|
|
@ -81,6 +87,9 @@ func InitialModel(orgFile *model.OrgFile, cfg *config.Config) uiModel {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (m uiModel) Init() tea.Cmd {
|
func (m uiModel) Init() tea.Cmd {
|
||||||
|
if m.mode == modeCapture {
|
||||||
|
return textinput.Blink
|
||||||
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -140,8 +149,12 @@ func (m *uiModel) updateScrollOffset(availableHeight int) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// RunUI starts the terminal UI
|
// RunUI starts the terminal UI
|
||||||
func RunUI(orgFile *model.OrgFile, cfg *config.Config) error {
|
func RunUI(orgFile *model.OrgFile, cfg *config.Config, captureMode bool, captureText string) error {
|
||||||
p := tea.NewProgram(InitialModel(orgFile, cfg), tea.WithAltScreen())
|
m := InitialModel(orgFile, cfg, captureMode, captureText)
|
||||||
|
if captureMode {
|
||||||
|
m.textinput.Focus()
|
||||||
|
}
|
||||||
|
p := tea.NewProgram(m, tea.WithAltScreen())
|
||||||
_, err := p.Run()
|
_, err := p.Run()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue