Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Search Gists
Sign in Sign up

Instantly share code, notes, and snippets.

View sindresorhus's full-sized avatar

Sindre Sorhus sindresorhus

Full-Time Open-Sourcerer. Focused on Swift & JavaScript. Makes macOS apps, CLI tools, npm packages.
View GitHub Profile
import ansiStyles from '#ansi-styles';
import supportsColor from '#supports-color';
import { // eslint-disable-line import/order
stringReplaceAll,
stringEncaseCRLFWithFirstIndex,
} from './utilities.js';
@sindresorhus
sindresorhus /actions.md
CreatedNovember 26, 2024 21:10
Shortcuts actions provided by the Actions app. Can be useful to feed into an AI.https://sindresorhus.com/actions

Adds the input item to the given list.

Even though the description says this action accepts input of type Files, it accepts any type.

Note: If you get the error “The operation failed because Shortcuts couldn't convert from Text to NSString.”, just change the preview to show a list view instead. This is a bug in the Shortcuts app.

Parameters

extensionCaseIterable{
publicstaticfunc randomCaseIterableElement(using generator:inoutsomeRandomNumberGenerator)->Self{
allCases.randomElement(using:&generator)!
}
publicstaticfunc randomCaseIterableElement()->Self{
vargenerator=SystemRandomNumberGenerator()
returnrandomCaseIterableElement(using:&generator)
}
}
@sindresorhus
sindresorhus /NSSharingService-sharingServices-deprecation-warning-fix.swift
CreatedOctober 19, 2022 07:56
macOS 13 deprecated `NSSharingService.sharingServices()`. Here's how to silence the deprecation until you can move to the new API.
privateprotocolSilenceDeprecationNSSharingService{
func sharingServices(forItems items:[Any])->[NSSharingService]
}
privatefinalclassSilenceDeprecationNSSharingServiceImplementation:SilenceDeprecationNSSharingService{
@available(macOS, deprecated:13)
func sharingServices(forItems items:[Any])->[NSSharingService]{
NSSharingService.sharingServices(forItems: items)
}
}
@sindresorhus
sindresorhus /UIScreen-screens-deprecation-warning-fix.swift
Last activeOctober 19, 2022 21:46
iOS 16 deprecated `UIScreen#screens`. Here's how to silence the deprecation until you can move to the new API.
privateprotocolSilenceDeprecationForUIScreenWindows{
varscreens:[UIScreen]{get}
}
privatefinalclassSilenceDeprecationForUIScreenWindowsImplementation:SilenceDeprecationForUIScreenWindows{
@available(iOS, deprecated:16)
varscreens:[UIScreen]{UIScreen.screens}
}
extensionUIScreen{
@sindresorhus
sindresorhus /SwiftUI Placeholder idea.swift
Last activeAugust 22, 2022 02:59
For a macOS app, if you want an action in both the main menu and the UI, it's quite boilerplaty. You need to synchronize some value with@focusedvalue. What if we had something like this?
struct CameraCommands:Commands{
var body:someCommands{
CommandGroup(replacing:.newItem){
// This menu item will be enabled when the key view has a button with `.command(.takePhoto)`. The action logic is implemented in the button.
Placeholder("Take Photo", id:.takePhoto)
// Alternatively, it could be:
// Button("Take Photo") {}
// .placeholder(.takePhoto)
}
extensionCollection{
/**
Returns the element at the given index if any,otherwise `nil`.
```
guardlet element=array[position]else{
return
}
print(element)
@sindresorhus
sindresorhus /NSView+shake.swift
CreatedJanuary 12, 2022 17:25
Shake a NSView
extensionNSView{
/**
Shake the view.
- Note: It will do nothing if the user has enabled the “Reduce motion” accessibility preference.
*/
func shake(
duration:TimeInterval=0.3,
direction:NSUserInterfaceLayoutOrientation,
moveAmount:Double=5
extensionCollectionwhere Element:Comparable{
/**
Returns the index of the first occurrence of the lowest value in the collection.
```
[4, 2, 3, 2, 5].firstIndexOfMinElement()
//=> 1
```
*/
func firstIndexOfMinElement()->Index?{
// Using `LocalizedError` here as the `.localizedDescription` for `Error` is often missing in places like an alert and Crashlytics/Sentry.
struct UnexpectedNilError:LocalizedError{
letmessage:String?
letfile:String
letline:Int
init(
_ message:String?,
file:String= #fileID,
line:Int= #line
NewerOlder

[8]ページ先頭

©2009-2025 Movatter.jp