Skip to content

Instantly share code, notes, and snippets.

@progrium
Last active Oct 17, 2021
Embed
What would you like to do?
Large Type CLI utility for Mac in less than 80 lines of Go

largetype

largetype screenshot

Building

Note: For now, Apple Silicon users need to be set up for x86 mode

First, download Go or brew install go. Then, put largetype.go in a directory called largetype and from there run:

$ go mod init largetype
$ go build

This will make you a 4MB binary called largetype. Now you can run it:

$ ./largetype "Hello world"
package main
import (
"flag"
"fmt"
"runtime"
"strings"
"github.com/progrium/macdriver/cocoa"
"github.com/progrium/macdriver/core"
"github.com/progrium/macdriver/objc"
)
func main() {
runtime.LockOSThread()
app := cocoa.NSApp_WithDidLaunch(func(n objc.Object) {
fontName := flag.String("font", "Helvetica", "font to use")
flag.Parse()
screen := cocoa.NSScreen_Main().Frame().Size
text := fmt.Sprintf(" %s ", strings.Join(flag.Args(), " "))
tr, fontSize := func() (rect core.NSRect, size float64) {
t := cocoa.NSTextView_Init(core.Rect(0, 0, 0, 0))
t.SetString(text)
for s := 70.0; s <= 550; s += 12 {
t.SetFont(cocoa.Font(*fontName, s))
t.LayoutManager().EnsureLayoutForTextContainer(t.TextContainer())
rect = t.LayoutManager().UsedRectForTextContainer(t.TextContainer())
size = s
if rect.Size.Width >= screen.Width*0.8 {
break
}
}
return rect, size
}()
height := tr.Size.Height * 1.5
tr.Origin.Y = (height / 2) - (tr.Size.Height / 2)
t := cocoa.NSTextView_Init(tr)
t.SetString(text)
t.SetFont(cocoa.Font(*fontName, fontSize))
t.SetEditable(false)
t.SetImportsGraphics(false)
t.SetDrawsBackground(false)
c := cocoa.NSView_Init(core.Rect(0, 0, 0, 0))
c.SetBackgroundColor(cocoa.Color(0, 0, 0, 0.75))
c.SetWantsLayer(true)
c.Layer().SetCornerRadius(32.0)
c.AddSubviewPositionedRelativeTo(t, cocoa.NSWindowAbove, nil)
tr.Size.Height = height
tr.Origin.X = (screen.Width / 2) - (tr.Size.Width / 2)
tr.Origin.Y = (screen.Height / 2) - (tr.Size.Height / 2)
w := cocoa.NSWindow_Init(core.Rect(0, 0, 0, 0),
cocoa.NSBorderlessWindowMask, cocoa.NSBackingStoreBuffered, false)
w.SetContentView(c)
w.SetTitlebarAppearsTransparent(true)
w.SetTitleVisibility(cocoa.NSWindowTitleHidden)
w.SetOpaque(false)
w.SetBackgroundColor(cocoa.NSColor_Clear())
w.SetLevel(cocoa.NSMainMenuWindowLevel + 2)
w.SetFrameDisplay(tr, true)
w.MakeKeyAndOrderFront(nil)
events := make(chan cocoa.NSEvent, 64)
go func() {
<-events
cocoa.NSApp().Terminate()
}()
cocoa.NSEvent_GlobalMonitorMatchingMask(cocoa.NSEventMaskAny, events)
})
app.ActivateIgnoringOtherApps(true)
app.Run()
}
@grillermo
Copy link

grillermo commented Jan 15, 2021

Looks nice, i blindly played around with the code:

image

@karlhorky
Copy link

karlhorky commented Jan 16, 2021

Thanks, this is useful!

Would you consider making this into a full repository for higher visibility and engagement? If not, leaving it as a Gist is also fine of course :)

@progrium
Copy link
Author

progrium commented Mar 8, 2021

It's intended as a demo/example so no repo necessary, but it is included in the examples for https://github.com/progrium/macdriver

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment