Skip to content

Instantly share code, notes, and snippets.

View tomasen's full-sized avatar
😞

SHEN SHENG tomasen

😞
View GitHub Profile
@tomasen
tomasen / list_usb_devices_with_speeds.sh
Last active October 13, 2024 04:17
A one line command that lists connected USB devices along with their speeds on macOS. USB Hubs are filtered out.
system_profiler SPUSBDataType | awk -F": " '/^[[:space:]]*[^[:space:]].*:$/ { device=$1; sub(/^[[:space:]]*/, "", device); sub(/:$/, "", device) } /^[[:space:]]+Speed:/ { gsub(/^[[:space:]]+Speed: /, "", $0); if (device !~ /[Hh]ub/) print device " - " $0 }'
@tomasen
tomasen / guide-to-reading-mathematical-expressions-aloud.md
Last active August 8, 2024 13:44
How to Read Mathematical Expressions Out Loud

Basic

Expression How to Read This
$$a + b$$ a plus b
$$a - b$$ a minus b
$$a \times b$$ a times b
$$a / b$$ a over b
$$a > b$$ a is greater than b
$$a < b$$ a is less than b
@tomasen
tomasen / WatchTabView.swift
Last active January 16, 2022 04:47
WatchTabView inside NavigationView demo on WatchOS
// Created by SHEN SHENG on 1/16/22.
// This code is a part of the Wordbook iOS app.
// Wordbook is an App to help memorize and learn new English words.
// website: https://www.wordbook.cool
struct WatchMasterView: View {
@FocusState private var focusTab: Int?
@State private var currentTab = 0
var body: some View {
@tomasen
tomasen / iOSLookAlikeTabView.swift
Last active January 16, 2022 04:20
Look-alike TabView inside NavigationView in iOS
// Created by SHEN SHENG on 1/16/22.
// This code is a part of the Wordbook iOS app.
// Wordbook is an App to help memorize and learn new English words.
// website: https://www.wordbook.cool
@State private var tabSelection = 1
var body: some View {
NavigationView {
VStack{
@tomasen
tomasen / simularWordSizes.swift
Created December 25, 2021 17:19
simularWordSizes for WordCloudView
func simularWordSizes(a: [CGSize], b: [CGSize]) -> Bool {
if a == b {
return true
}
if a.count != b.count {
return false
}
var diff : CGFloat = 0
for i in 0...(a.count-1) {
@tomasen
tomasen / WordCloudPositionCache.swift
Last active December 25, 2021 17:19
WordCloudStateCache for WordCloudView
private var positionCache = WordCloudPositionCache()
func calcPositions(canvasSize: CGSize, itemSizes: [CGSize]) -> [CGPoint] {
var pos = [CGPoint](repeating: CGPoint.zero, count: itemSizes.count)
if canvasSize.height == 0 {
return pos
}
if positionCache.canvasSize == canvasSize {
return positionCache.positions
@tomasen
tomasen / calcPositions.swift
Last active December 25, 2021 16:59
calcPositions for WordCloudView
func checkIntersects(rect: CGRect, rects: [CGRect]) -> Bool {
for r in rects {
if rect.intersects(r) {
return true
}
}
return false
}
func checkOutsideBoundry(canvasSize: CGSize, rect: CGRect) -> Bool {
@tomasen
tomasen / findSafePlace.swift
Created December 25, 2021 16:46
find a place by brutal force for WordCloudView
func findSafePlace(for size: CGSize, avoid occupiedAreas: [CGRect]) -> CGRect {
// keep trying random places until it fit
for _ in 0...100 {
let randomRect = CGRect(origin: CGPoint(x: CGFloat.random(in: 0...canvasSize.width),
y: CGFloat.random(in: 0...canvasSize.height)),
size: size)
var collision = false
for rect in occupiedAreas {
if rect.intersects(randomRect) {
collision = true
@tomasen
tomasen / WordSizeGetter.swift
Last active December 25, 2021 16:36
WordSizeGetter in WordCloudView demo
struct WordCloudView: View {
private let words: [WordElement] = [WordElement].generate(forSwiftUI: true)
@State private var canvasRect = CGRect()
@State private var wordSizes: [CGSize]
init() {
self._wordSizes = State(initialValue:[CGSize](repeating: CGSize.zero, count: words.count))
}
@tomasen
tomasen / RectGetter.swift
Created December 25, 2021 16:05
SwiftUI RectGetter example
struct RectGetterDemoView: View {
@State private var canvasRect = CGRect()
var body: some View {
Text("Demo").background(RectGetter($canvasRect))
}
}
struct RectGetter: View {